#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], b[maxn];
int num[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    num[b[i]] = i;
  }
  bool flag = false;
  int tot = b[n];
  while (tot != 1 && num[tot] == num[tot - 1] + 1) tot--;
  if (tot == 1) {
    for (int i = b[n] + 1; i <= n; i++) {
      if (num[i] > i - b[n] - 1) {
        flag = true;
        break;
      }
    }
    if (!flag) {
      printf("%d\n", n - b[n]);
      return 0;
    }
  }
  int ans = n, mark = 0;
  int cnt = num[1];
  for (int i = 1; i <= n; i++) {
    if (num[i] < cnt)
      cnt++;
    else
      while (num[i] >= cnt) cnt++;
    if (cnt >= n) {
      mark = i;
      break;
    }
  }
  ans += (n - mark) + (cnt - n);
  printf("%d\n", ans);
  return 0;
}
