#include <bits/stdc++.h>
using namespace std;
const int N = 4200100;
int pile[N], n;
bool exist[N], now_exist[N];
bool checkline(int id) {
  for (int i = id; i <= n; i++) {
    if (pile[i] != i - id + 1) {
      return 0;
    }
  }
  return 1;
}
bool possible(int from, bool exist[], int lp) {
  for (int i = from; i <= n; i++) {
    if (!exist[i]) {
      return 0;
    }
    exist[pile[i - from + lp]] = 1;
  }
  return 1;
}
bool check(int lp) {
  memset(now_exist, 0, sizeof(now_exist));
  for (int i = 1; i <= n; i++) now_exist[i] = exist[i];
  for (int i = 1; i <= lp - 1; i++) now_exist[pile[i]] = 1;
  return possible(1, now_exist, lp);
}
int main() {
  int i, j, k, pos, l, r, ans, x, mid;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (x) exist[x] = 1;
  }
  for (i = 1; i <= n; i++) scanf("%d", &pile[i]);
  pos = 0;
  for (i = 1; i <= n; i++)
    if (pile[i] == 1) {
      pos = i;
      break;
    }
  if (checkline(pos)) {
    if (possible(n - pos + 2, exist, 1)) {
      printf("%d\n", pos - 1);
      return 0;
    }
  }
  l = 1;
  r = n;
  ans = n + 1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", ans + n - 1);
  return 0;
}
