#include <bits/stdc++.h>
using namespace std;
int n;
int maxIHave;
int pile[200005], hand[200005];
int reached[200005];
int orig[200005];
bool canS = 1;
int startN = 1;
void copyOrig() {
  for (int i = 1; i <= n; i++) reached[i] = orig[i];
}
bool canContinue() {
  copyOrig();
  int indx = 1;
  for (int i = startN; i <= n; i++) {
    if (!reached[i]) return 0;
    reached[pile[indx]]++;
    indx++;
  }
  return 1;
}
bool canReach(int x) {
  for (int i = 1; i <= x; i++) {
    reached[pile[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!reached[i]) return 0;
    if (x + i <= n) reached[pile[i + x]]++;
  }
  return 1;
}
int main() {
  while (cin >> n) {
    for (int i = 1; i <= n; i++) cin >> hand[i];
    int ind = -1;
    for (int i = 1; i <= n; i++) {
      cin >> pile[i];
      if (pile[i] == 1) ind = i;
    }
    for (int i = ind + 1; canS && i <= n; i++) {
      if (pile[i] != pile[i - 1] + 1) canS = 0;
    }
    if (canS) startN = pile[n] + 1;
    memset(orig, 0, sizeof(orig));
    int b = 0, e = n, mid;
    int res;
    int res2;
    for (int i = 1; i <= n; i++) orig[hand[i]]++;
    while (b <= e) {
      mid = b + (e - b) / 2;
      copyOrig();
      if (canReach(mid)) {
        res = mid;
        e = mid - 1;
      } else {
        b = mid + 1;
      }
    }
    if (canS && canContinue())
      cout << min(n - pile[n], res + n) << endl;
    else
      cout << res + n << endl;
  }
  return 0;
}
