#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9, mod = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') f = (c == '-') ? -1 : 1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
int a[1000001], b[1000001];
int vis[10000001], n, o;
bool check(int x) {
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) vis[a[i]] = 1;
  for (int i = 1; i < x; i++) vis[b[i]] = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) return 0;
    vis[b[x++]] = 1;
  }
  return 1;
}
int main() {
  n = read(), o = 0;
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) b[i] = read();
  for (int i = 1; i <= n; i++) {
    if (b[i] == 1) o = i;
  }
  if (o) {
    int flag = 0;
    for (int i = o; i <= n; i++) {
      if (b[i] != i - o + 1) flag = 1;
    }
    if (!flag) {
      int ans = b[n] + 1;
      flag = 0;
      memset(vis, 0, sizeof(vis));
      for (int i = 1; i <= n; i++) vis[a[i]] = 1;
      int i = 1;
      while (ans <= n) {
        if (vis[ans] == 0) {
          flag = 1;
          break;
        }
        vis[b[i]] = 1, ans++, i++;
      }
      if (!flag) cout << n - b[n], exit(0);
    }
  }
  int l = 1, r = 1000000, ans = r;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1, ans = min(ans, mid);
    else
      l = mid + 1;
  }
  cout << ans + n - 1;
}
