#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
int n;
int arr[maxn], brr[maxn], p[maxn];
int vis[maxn];
int que[maxn << 1];
int solve1() {
  memset(vis, 0, sizeof vis);
  int head = 1, tail = 0;
  for (int i = 1; i <= n; ++i) {
    vis[arr[i]]++;
    que[++tail] = brr[i];
  }
  int Max = 0;
  for (int i = 1; i <= n; ++i) {
    if (que[i] == 1) {
      Max = 1;
      for (int j = i + 1; j <= n; ++j) {
        if (que[j] - que[j - 1] != 1) {
          return INF;
        }
        Max = max(Max, que[j]);
      }
    }
  }
  int res = 0;
  for (int i = Max + 1; i <= n; ++i) {
    if (!vis[i]) {
      return INF;
    }
    que[++tail] = i;
    ++vis[que[head]];
    ++head;
    ++res;
  }
  return res;
}
bool check(int x) {
  int tmp = x;
  int head = 1, tail = 0;
  priority_queue<int> Q1;
  priority_queue<int, vector<int>, greater<int> > Q2;
  int tot0 = 0;
  for (int i = 1; i <= n; ++i) {
    if (arr[i] == 0) {
      ++tot0;
    } else {
      Q1.push(arr[i]);
    }
    que[++tail] = brr[i];
  }
  while (x--) {
    if (tot0) {
      tot0--;
      que[++tail] = 0;
    } else {
      break;
    }
    if (que[head] == 0) {
      ++tot0;
      ++head;
    } else {
      Q1.push(que[head]);
      ++head;
    }
  }
  while (!Q1.empty()) {
    Q2.push(Q1.top());
    Q1.pop();
  }
  int now = 0;
  for (int i = 1; i <= n; ++i) {
    if (Q2.empty()) {
      return false;
    }
    if (Q2.top() != now + 1) {
      return false;
    }
    now = Q2.top();
    Q2.pop();
    que[++tail] = now;
    if (que[head] != 0) {
      Q2.push(que[head]);
    }
    ++head;
  }
  return true;
}
int solve2() {
  int l = 0, r = n, res = INF;
  while (r - l >= 0) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid - 1;
      res = mid;
    } else {
      l = mid + 1;
    }
  }
  return res + n;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", arr + i);
    p[arr[i]] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", brr + i);
    p[brr[i]] = i;
  }
  int ans = 2e9;
  ans = min(ans, solve1());
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    res = max(res, p[i] - i + 1 + n);
  }
  printf("%d\n", min(res, ans));
  return 0;
}
