#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const long long INFll = 1ll * 1000000000 * 1000000000;
const long double PI =
    3.141592653589793238462643383279502884197169399375105820974944;
int mul(int a, int b, int mod = 1000003) { return int(a * 1ll * b % mod); }
int norm(int a, int mod = 1000003) {
  while (a >= mod) a -= mod;
  while (a < 0) a += mod;
  return a;
}
int powmod(int x, int y, int mod = 1000003) {
  int res = 1;
  while (y > 0) {
    if (y & 1) res = mul(res, x, mod);
    x = mul(x, x, mod);
    y = y >> 1;
  }
  return res;
}
int inv(int a, int mod = 1000003) { return powmod(a, mod - 2); }
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), p(n + 1);
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
  }
  int pos = -1, k = 0, disrupt = 0, ans = 0;
  queue<int> q;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (b[i] == 1) {
      pos = i;
    }
    q.push(b[i]);
    p[b[i]] = i + 1;
  }
  if (pos != -1) {
    int i;
    k = 2;
    for (i = pos + 1; i < n; i++) {
      if (b[i] == k) {
        k++;
      } else {
        break;
      }
    }
    if (i == n) {
      k = k - 1;
    } else {
      k = 0;
      disrupt = 1;
    }
  }
  if (k != 0) {
    while ((k + 1) <= n) {
      if (disrupt == 1) {
        while (q.front() != 1) {
          m[q.front()]++;
          q.pop();
          m[0]--;
          q.push(0);
          ans++;
        }
        m[q.front()]++;
        q.pop();
        m[0]--;
        q.push(0);
        ans++;
        k = 0;
        disrupt = 0;
        continue;
      }
      if (m[k + 1] == 1) {
        m[k + 1]--;
        m[q.front()]++;
        q.pop();
        q.push(k + 1);
        ans++;
        k++;
      } else {
        disrupt = 1;
      }
    }
  } else {
    ans = n;
    int res = 0;
    for (int i = 1; i <= n; i++) {
      if (m[i]) continue;
      if (p[i] > i - 1) {
        res = max(res, p[i] - (i - 1));
      }
    }
    ans += res;
  }
  cout << ans;
  return 0;
}
