#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unrolled-loops")
int n, b[200010], c[200010], dp, pv = LONG_MAX, cnt, tmp, ans = 0, f = 1;
multiset<int> a;
deque<int> dq;
void tr() {
  int flag = 0;
  for (int i = 0; i < n; i++) {
    if (c[i] == f) {
      i++;
      flag = 1;
      for (; i < n; i++) {
        f++;
        if (c[i] != f) return;
      }
    }
  }
  if (!flag) return;
  for (int i = 0; i < n; i++) a.insert(b[i]);
  for (int i = 0; i < n; i++) dq.push_back(c[i]);
  int cur = f + 1;
  while (1) {
    ans++;
    if (cur == n + 1) {
      cout << ans - 1;
      exit(0);
    }
    if (a.count(cur))
      cur++;
    else
      return;
    a.insert(dq.front());
    dq.pop_front();
    dq.push_back(0);
  }
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  if (n == 1 && c[0] == 1) {
    cout << 0;
    return;
  } else if (n == 1) {
    cout << 1;
    return;
  }
  tr();
  dq.clear();
  a.clear();
  for (int i = 0; i < n; i++) a.insert(b[i]);
  for (int i = 0; i < n; i++) dq.push_back(c[i]);
  ans = 0;
  while (!a.count(1)) {
    a.insert(dq.front());
    dq.pop_front();
    a.erase(a.find(0));
    dq.push_back(0);
    ans++;
  }
  int cur = 1;
  while (1) {
    ans++;
    if (a.count(cur)) cur++;
    if (cur == n + 1) {
      cout << ans;
      return;
    }
    a.insert(dq.front());
    dq.pop_front();
    dq.push_back(0);
  }
}
int main() { solve(); }
