#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long a[maxn], b[maxn];
long long c[maxn];
long long n, f, fl, s, ans;
void rs() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  f = -1;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    c[b[i]] = i;
    if (b[i] == 1) f = i;
  }
}
int main() {
  rs();
  if (f != -1) {
    s = 1;
    for (int i = f + 1; i <= n; i++) {
      if (b[i] - b[i - 1] != 1) {
        s = 0;
        break;
      }
    }
    if (s) {
      fl = 1;
      for (int i = b[n] + 1; i <= n; i++) {
        if (c[i] > i - b[n] - 1) {
          fl = 0;
          break;
        }
      }
      if (fl) {
        ans = n - b[n];
      } else {
        for (int i = 1; i <= n; i++) ans = max(ans, c[i] + n - i + 1);
      }
    } else {
      for (int i = 1; i <= n; i++) ans = max(ans, c[i] + n - i + 1);
    }
  } else {
    for (int i = 1; i <= n; i++) ans = max(ans, c[i] + n - i + 1);
  }
  cout << ans << endl;
  return 0;
}
