#include <bits/stdc++.h>
#pragma GCC optimize("O3")
const long long mod1 = 998244353;
const long long mod2 = 1000000007;
long long pow(long long a, long long b) {
  if (b == 0 || a == 1) return 1;
  if (b % 2 == 0) {
    long long k = pow(a, b / 2);
    return (k * k);
  } else {
    long long k = pow(a, b / 2);
    return k * k * a;
  }
}
long long powmod(long long a, long long b, long long mod) {
  if (b == 0 || a == 1) {
    if (mod == 1)
      return 0;
    else
      return 1;
  }
  if (b % 2 == 0) {
    long long k = powmod(a, b / 2, mod);
    return (k * k) % mod;
  } else {
    long long k = powmod(a, b / 2, mod);
    return ((k * k) % mod * a) % mod;
  }
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a > b)
    return gcd(a % b, b);
  else
    return gcd(b % a, a);
}
long long prime(long long p) {
  for (long long i = 2; i * i <= p; i++) {
    if (p % i == 0 && i < p) return 0;
  }
  return 1;
}
long long sqr(long long i) { return i * i; }
using namespace std;
signed main() {
  long long a;
  cin >> a;
  vector<long long> m1(a);
  vector<long long> m2(a);
  for (long long i = 0; i < a; i++) cin >> m1[i];
  for (long long i = 0; i < a; i++) cin >> m2[i];
  long long qq = 0;
  for (long long i = 0; i < a - 1; i++)
    if (m2[i + 1] > m2[i]) qq++;
  if (m2[0] == 1 && qq == a - 1) {
    cout << 0;
    return 0;
  }
  long long cur = 0;
  while (m2[a - 1 - cur] - m2[a - 2 - cur] == 1 && m2[a - 2 - cur] != 0) cur++;
  long long pos[a + 1];
  for (long long i = 1; i <= a; i++) pos[i] = 0;
  for (long long i = 0; i < a; i++) pos[m2[i]] = i + 1;
  long long check = 1;
  for (long long i = a - cur - 1; i < a; i++)
    if (m2[i] != i - a + cur + 2) check = 0;
  for (long long i = cur + 2; i <= a; i++)
    if (pos[i] > i - cur - 2) check = 0;
  if (check == 1) {
    cout << a - cur - 1;
    return 0;
  }
  long long max1 = 0;
  for (long long i = 1; i <= a; i++) max1 = max(max1, pos[i] - i + 1);
  long long ans = a + max1;
  cout << a + max1;
  return 0;
}
