#include <bits/stdc++.h>
using namespace std;
int a[200010], b[200010];
int n, ans, s;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  bool flag = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (b[i] == 1) s = i, ans += i;
    if (s != 0 && s != i && b[i] != b[i - 1] + 1) flag = 1;
  }
  if (flag == 0 && s != 0) {
    int j = n - s + 1;
    for (int i = 1; i < s; i++) {
      j++;
      if (b[i] <= j && b[i] != 0) flag = 1;
    }
    if (!flag) {
      printf("%d", s - 1);
      return 0;
    }
  }
  int j = 1, res = 0;
  for (int i = s + 1; i <= n; i++) {
    if (b[i] != 0) res = max(res, j - b[i] + 1);
    j++;
  }
  cout << ans + res + n;
}
