#include <bits/stdc++.h>
using namespace std;
bool app[200005];
int a[200005], b[200005], n;
int check(int p) {
  int ret = p;
  memset(app, 0, sizeof(app));
  for (int i = 1; i <= n; i++) app[a[i]] = 1;
  for (int i = 1; i <= p; i++) app[b[i]] = 1;
  int cnt = p + 1;
  for (int i = 1; i <= n; i++) {
    if (!app[i]) return -1;
    app[b[cnt++]] = 1;
    ret++;
  }
  return ret;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int j = 1; j <= n; j++) cin >> b[j];
  bool key = 0;
  for (int i = n; i >= 1; i--) {
    if (i > 1 && b[i] != b[i - 1] + 1) break;
    if (b[i] == 1) {
      key = 1;
      break;
    }
  }
  if (key) {
    memset(app, 0, sizeof(app));
    for (int i = 1; i <= n; i++) app[a[i]] = 1;
    int dd = 1, pp = 0, cnt = 1;
    for (int i = b[n] + 1; i <= n; i++) {
      if (!app[i]) {
        dd = 0;
        break;
      }
      pp++;
      app[b[cnt++]] = 1;
    }
    if (dd) {
      cout << pp << endl;
      return 0;
    }
  }
  int l = 0, r = n, ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    int tmp = check(mid);
    if (tmp != -1)
      ans = tmp, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
}
