#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 10;
long long a[maxn];
long long b[maxn];
long long make[maxn];
int32_t main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= n; i++) cin >> b[i];
  long long temp = 0;
  for (long long i = 1; i <= n; i++) {
    if (b[i] == 1) temp = i;
  }
  long long k = 1;
  if (temp) {
    for (long long i = temp + 1; i <= n; i++)
      if (b[i] != b[i - 1] + 1) k = 0;
  }
  if (k == 1 && temp) {
    for (long long i = 1; i <= n; i++) {
      make[a[i]] = 1;
    }
    long long ans = 0;
    long long flag = 0;
    for (long long t = b[n] + 1; t <= n; t++) {
      if (make[t] == 1) {
        ans++;
        make[b[ans]] = 1;
      } else {
        flag = 1;
        break;
      }
    }
    if (flag == 0) {
      cout << ans << endl;
      return 0;
    } else
      memset(make, 0, sizeof(make));
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) make[a[i]] = 1;
  for (long long i = 1; i <= temp; i++) make[b[i]] = 1, ans++;
  long long d = 0;
  for (long long i = temp + 1; i <= n; i++) {
    if (b[i] == 0) continue;
    if (b[i] <= i - temp) {
      d = max(d, i - temp - (b[i] - 1));
    }
  }
  cout << ans + d + n << endl;
}
