#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cout.tie(0);     int n;     cin >> n;     vector<bool> in(n + 1);     for (int i = 0; i < n; i++) {       int a;       cin >> a;       in[a] = true;     }     vector<int> b(n);     for (int i = 0; i < n; i++) {       cin >> b[i];     }     int ind = 0;     int next_ = 1;     vector<int> c = b;     for (; next_ <= n; ind++) {       int t = ind - n - 1;       if (t >= 0 && t < n) {         in[b[ind - n - 1]] = true;       }       if (c[ind % n] == next_) {         next_++;         c[ind % n] = -1;         continue;       }       if (c[ind % n] == -1 && in[next_]) {         next_++;         continue;       }       c[ind % n] = -1;       if (ind - next_ + 1 < n) {         next_ = 1;       }     }     cout << ind - n << endl;   }   