#include <bits/stdc++.h>
using std::max;
using std::min;
using std::sort;
using std::swap;
template <typename T>
void read(T &x) {
  int flag = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = -flag;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
const int _ = 2e5 + 10, Inf = 1e9 + 7;
int n, a[_], b[_], p[_];
int F() {
  if (!p[1]) return Inf;
  int cur = 2;
  for (; p[cur] == p[1] + cur - 1; ++cur)
    ;
  if (p[cur - 1] == n) {
    int k = cur;
    for (; k <= n && p[k] <= k - cur; ++k)
      ;
    if (k > n) return n - cur + 1;
  }
  return Inf;
}
int G() {
  int ret = 0;
  for (int i = 1; i <= n; ++i) ret = max(ret, p[i] + 1 + n - i);
  return ret;
}
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) read(a[i]), p[a[i]] = 0;
  for (int i = 1; i <= n; ++i) read(b[i]), p[b[i]] = i;
  printf("%d\n", min(F(), G()));
  return 0;
}
