#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
typedef long long ll;
int a[N], b[N];
deque<int> qa, qb;
int main()
{
    for (int n; ~scanf("%d", &n);)
    {
        int ans = 0;
        qa.clear(), qb.clear();
        for (int i = 0; i < n; i++) scanf("%d", a + i);
        for (int i = 0; i < n; i++) scanf("%d", b + i);
        for (int i = 0; i < n; i++)
        {
            while (!qa.empty() && a[qa.back()] < a[i]) qa.pop_back();
            while (!qb.empty() && a[qb.back()] < b[i]) qb.pop_back();
            if (qa.empty() && qb.empty())
                qa.push_back(i), qb.push_back(i), ans = i + 1;
            else if (!qa.empty() && !qb.empty() && qa.back() == qb.back())
                ans = i + 1;
            else
                break;
        }
        printf("%d\n", ans);
    }
    return 0;
}