#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 5e4 + 10;
int t, n, m, k, l, r, op, x, y;
int h[N], z[N];
unordered_map<int,int>p;
void build(int hl, int hr, int zl, int zr) {
int k = p[h[hr]];
x = h[hr];
int lcnt = k - zl, rcnt = zr - k;
if (lcnt > 0)build(hl, hl + lcnt - 1, zl, k - 1);
if (rcnt > 0)build(hl + lcnt, hr - 1, k + 1, zr);
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
for (int i = 1; i <= n; i++) {
cin >> z[i];
p[z[i]] = i;
}
build(1, n, 1, n);
cout << x;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}