#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 1e5;
int pila[MAXN + 2];
int cubeta[MAXN + 2];
int obten_sig(int n) {
  if (cubeta[1] != 0) return 1;
  int pos = 1;
  while (pila[pos] != 1) pos++;
  int ini = pos - 1;
  for (int i = 1; i <= n - ini; i++) {
    if (pila[ini + i] != i) return 1;
  }
  return n - ini + 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    cubeta[a] = 1;
  }
  for (int i = 1; i <= n; i++) {
    cin >> pila[i];
  }
  int idx = 1;
  int pasos = 0;
  int sig = obten_sig(n);
  int menor = 1;
  while (menor <= n && cubeta[menor] != 0) menor++;
  while (sig <= n) {
    if (cubeta[sig] != 0) {
      sig++;
    } else {
      sig = menor;
    }
    if (idx <= n) {
      cubeta[pila[idx]] = 1;
      idx++;
    }
    if (menor <= n && cubeta[menor] != 0) menor++;
    pasos++;
  }
  cout << pasos << '\n';
  return 0;
}
