#include <bits/stdc++.h>
using namespace std;
int a[200005];
int b[200005];
map<int, int> mp;
map<int, int> mpp;
int main() {
  int n, m, i, j, k, ans = 0, num = 0, sum, z, q;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), mp[a[i]] = 1, mpp[a[i]] = 1;
  for (i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (b[i] == 1) num = i;
  }
  int sym = 1;
  if (num != 0) {
    for (i = num + 1; i <= n; i++) {
      if (b[i] != b[i - 1] + 1) {
        sym = 0;
        break;
      }
    }
  } else
    sym = 0;
  int tot;
  if (sym) {
    ans = 0;
    i = j = 1;
    tot = b[n] + 1;
    while (1) {
      if (tot == n + 1) break;
      if (mpp[tot] == 1)
        tot++;
      else {
        sym = 0;
        break;
      }
      mpp[b[j]] = 1;
      ans++;
      j++;
    }
  }
  if (sym) {
    printf("%d\n", ans);
    return 0;
  }
  ans = 0;
  tot = 1;
  i = j = 1;
  while (1) {
    if (mp[tot] == 1) tot++;
    mp[b[j]] = 1;
    ans++;
    j++;
    if (tot == n + 1) break;
  }
  printf("%d\n", ans);
  return 0;
}
