#include <bits/stdc++.h>
using namespace std;
const int MAXN = ((int)2e5) + 1;
int n, a[MAXN], b[MAXN], mv[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mv[a[i]] = 1;
  }
  int k = -1;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    mv[b[i]] = i + 2;
    if (b[i] == 1) k = i;
  }
  int ans = 0;
  bool fail = (k == -1);
  if (!fail) {
    for (int i = k + 1; i < n; i++) {
      if (b[i] != i - k + 1) {
        fail = 1;
      }
    }
    for (int x = n - k + 1; x <= n; x++) {
      if (mv[x] > x - (n - k)) {
        fail = 1;
      }
    }
  }
  if (!fail) {
    cout << k << '\n';
    return 0;
  }
  for (int x = 1; x <= n; x++) {
    ans = max(ans, mv[x] + n - x);
  }
  cout << ans << '\n';
  return 0;
}
