#include <bits/stdc++.h>
using namespace std;
int n, a[200010], b[200010], psz, vis[200010], ans, tmp = 1, ed;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), vis[a[i]] = 1;
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
  for (int i = n; i > 1 && b[i - 1] && b[i - 1] + 1 == b[i]; --i, ++tmp)
    ;
  tmp *= (b[n] == tmp);
  if (tmp) {
    if (tmp == n) return puts("0"), 0;
    psz = tmp;
    while (vis[psz + 1]) ++psz, vis[b[psz - tmp]] = 1;
    if (psz == n) return printf("%d", n - tmp), 0;
    psz = 0, memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; ++i) vis[a[i]] = 1;
  }
  while (vis[psz + 1]) ++psz, vis[b[psz]] = 1;
  for (ans = 1; ans + psz <= n; ++ans) {
    vis[b[((ans + psz) > n ? 0 : (ans + psz))]] = 1;
    while (vis[psz + 1]) ++psz, vis[b[((ans + psz) > n ? 0 : (ans + psz))]] = 1;
  }
  --ans;
  printf("%d", ans + n);
  return 0;
}
