#include <bits/stdc++.h>
using namespace std;
constexpr int mod = 1000000007;
constexpr int mod1 = 998244353;
vector<int> dx = {0, -1, 0, 1}, dy = {1, 0, -1, 0};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < (n); ++i) cin >> a.at(i);
  for (int i = 0; i < (n); ++i) cin >> b.at(i);
  bool flg = true, flg1 = false;
  for (int i = 0; i < (n); ++i)
    if (b.at(i) != i + 1) flg = false;
  if (flg) {
    cout << 0 << endl;
    return 0;
  }
  int k = 0;
  for (int i = n - 1; i > 0; --i) {
    if (b.at(i) - 1 == b.at(i - 1) && b.at(i - 1) > 0)
      k++;
    else
      break;
  }
  if (b.at(n - 1 - k) == 1) {
    k++;
    flg1 = true;
  }
  vector<int> c(n + 1);
  for (int i = 0; i < (n); ++i) {
    c.at(i + 1) = i - 1;
  }
  for (int i = 0; i < (n); ++i) {
    if (a.at(i)) c.at(a.at(i)) = -1;
  }
  flg = true;
  for (int i = 0; i < (n); ++i) {
    if (b.at(i) > k && c.at(b.at(i)) - k < i) flg = false;
  }
  for (int i = 0; i < (n); ++i) {
    if (b.at(i)) {
      if (flg && b.at(i) <= k)
        c.at(b.at(i)) = 0;
      else if (c.at(b.at(i)) < i)
        c.at(b.at(i)) = i - c.at(b.at(i));
      else
        c.at(b.at(i)) = 0;
    }
  }
  int ans = 0;
  for (int i = 0; i < (n + 1); ++i) ans = max(ans, c.at(i));
  if (flg)
    ans += n - k;
  else
    ans += n;
  cout << ans << endl;
}
