#include <bits/stdc++.h>
using namespace std;
bool mp[200010];
int n;
int a[200010], b[200010];
bool ok;
bool check(int mid) {
  memset(mp, 0, sizeof(mp));
  for (int i = 1; i <= n; i++) mp[a[i]] = 1;
  for (int i = 1; i <= mid; i++) mp[b[i]] = 1;
  for (int i = 1; i <= n; i++) {
    if (!mp[i]) return false;
    mp[b[i + mid]] = 1;
  }
  return true;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i], mp[a[i]] = 1;
  for (int i = 1; i <= n; i++) cin >> b[i];
  ok = 1;
  for (int i = 1; i <= n; i++) {
    if (b[i] != i) {
      ok = 0;
      break;
    }
  }
  if (ok == 1) {
    putchar('0');
    return 0;
  }
  ok = 1;
  int x = b[n];
  for (int i = n - 1; i > n - x; i--) {
    if (b[i] != x - n + i) {
      ok = 0;
      break;
    }
  }
  if (ok == 1) {
    for (int i = x + 1; i <= n; i++) {
      if (mp[i] != 1) {
        ok = 0;
        break;
      }
      mp[b[i - x]] = 1;
    }
  }
  if (ok) {
    cout << n - x;
    return 0;
  }
  int l = 1, r = n, ans = 0;
  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans + n;
}
