#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> mp;
bool check(int mid, int n, int a[], int b[]) {
  mp.clear();
  int i;
  for (i = 1; i <= n; i++) {
    mp[a[i]] = 1;
  }
  for (i = 1; i < mid; i++) {
    mp[b[i]] = 1;
  }
  int f = 1;
  for (i = mid; i <= n; i++) {
    if (mp.count(f) == 0) return 0;
    mp[b[i]] = 1;
    f++;
  }
  return 1;
}
int main() {
  int n;
  cin >> n;
  int a[n], b[n];
  int i;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int be;
  for (i = 1; i <= n; i++) {
    cin >> b[i];
    if (b[i] == 1) be = i;
  }
  int begin, flag;
  begin = 1;
  flag = 1;
  for (i = be; i <= n; i++) {
    if (b[i] != begin) {
      flag = 0;
      break;
    }
    begin++;
  }
  int l = 1;
  int r = n;
  int ans = 2 * n + 1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid, n, a, b)) {
      r = mid - 1;
      ans = min(ans, mid + n);
    } else
      l = mid + 1;
  }
  int ans1 = 0x3f3f3f3f;
  if (flag) {
    mp.clear();
    int ok = 1;
    for (int i = 1; i <= n; ++i) mp[a[i]] = 1;
    int f = begin;
    for (i = 1; i < be; i++) {
      if (mp.count(f) == 0) ok = 0;
      mp[b[i]] = 1;
      f++;
    }
    if (ok) ans1 = be - 1;
  }
  int ans2 = min(ans1, ans - 1);
  cout << ans2 << endl;
  return 0;
}
