#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 100010;
int d[MAXN];
int nrOK = 0;
deque<int> down, down2;
int N;
bool inHand[MAXN];
int method1() {
  down2 = down;
  for (int i = 1; i <= N; ++i)
    if (d[i] == 0) inHand[i] = true;
  int ans = 0;
  while (1) {
    int need = down2.back() + 1;
    if (need > N) break;
    if (!(inHand[need])) return (INT_MAX);
    ;
    down2.push_back(need);
    inHand[need] = false;
    inHand[down2.front()] = true;
    down2.pop_front();
    ++ans;
  }
  if (!(is_sorted(down2.begin(), down2.end()))) return (INT_MAX);
  ;
  for (const auto &it : down2) {
    if (!(it != 0)) return (INT_MAX);
    ;
  }
  return ans;
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    int x;
    scanf("%d", &x);
    d[x] = 0;
  }
  for (int i = 1; i <= N; ++i) {
    int x;
    scanf("%d", &x);
    d[x] = i;
    down.push_back(x);
  }
  int ans1 = method1();
  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    int tar = i - 1;
    if ((d[i] <= tar)) continue;
    ;
    ans = max(ans, d[i] - tar);
  }
  ans += N;
  ans = min(ans1, ans);
  printf("%d\n", ans);
  return 0;
}
