#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > path[200005];
string s;
int a[200005], b[200005], f[200005], ff[200005], n, ans;
int r1() {
  if (!b[n]) return -1;
  if (b[n] != 1)
    for (int i = n - 1;; i--) {
      if (b[i] != b[i + 1] - 1) return -1;
      if (b[i] == 1) break;
    }
  for (int i = (1); i <= (n); i++) ff[i] = f[i];
  for (int i = b[n] + 1, j = 1; i <= n; i++, j++) {
    if (!ff[i]) return -1;
    ff[b[j]] = 1;
  }
  return n - b[n];
}
int r0(int x) {
  for (int i = (1); i <= (n); i++) ff[i] = f[i];
  for (int i = (1); i <= (x - n); i++) ff[b[i]] = 1;
  for (int i = 1, j = x - n + 1; i <= n; i++, j++) {
    if (!ff[i]) return 0;
    if (j <= n) ff[b[j]] = 1;
  }
  return 1;
}
int main() {
  cin >> n;
  for (int i = (1); i <= (n); i++) scanf("%d", &a[i]), f[a[i]] = 1;
  for (int i = (1); i <= (n); i++) scanf("%d", &b[i]);
  int ans = r1();
  if (ans != -1) {
    cout << ans << endl;
    return 0;
  }
  int l = n, r = n * 2;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (r0(mid)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
