#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
int pos[N], a[N], b[N];
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i], pos[b[i]] = i;
  int p = 1;
  for (int i = 1; i <= n; i++) p = max(p, pos[i] - i + 2);
  int ans = p - 1 + n;
  int p1 = 0;
  for (int i = 1; i <= n; i++)
    if (b[i] == 1) p1 = i;
  if (p1) {
    int ok = 1;
    for (int i = p1 + 1; i <= n; i++) ok &= (b[i] == b[i - 1] + 1);
    for (int i = 0; n - p1 + 2 + i <= n; i++) {
      ok &= (pos[n - p1 + 2 + i] <= i);
    }
    if (ok) ans = p1 - 1;
  }
  cout << ans << endl;
}
