#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;
namespace io {
void setIn(const char* in) { freopen(in, "r", stdin); }
void setOut(const char* out) { freopen(out, "w", stdout); }
template <class T1, class T2>
ostream& operator<<(ostream& out, const pair<T1, T2> p) {
  return out << '[' << p.first << ", " << p.second << "]";
}
template <class T1, class T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
  return in >> p.first >> p.second;
}
template <class T>
ostream& operator<<(ostream& out, const vector<T> v) {
  out << "{ ";
  int sz = (int)v.size();
  for (int i = 0; i < sz; i++) {
    out << v[i];
    if (i != sz - 1) out << ", ";
  }
  return out << " }";
}
inline int rdi() {
  int v;
  cin >> v;
  return v;
}
inline long long rdll() {
  long long v;
  cin >> v;
  return v;
}
inline string rds() {
  string s;
  cin >> s;
  return s;
}
inline char rdc() {
  char c;
  cin >> c;
  return c;
}
template <class T>
void rdv(T beg_iter, T end_iter) {
  for (; beg_iter != end_iter; ++beg_iter) cin >> *beg_iter;
}
inline void wri(const int v) { cout << v; }
inline void wrll(const long long& v) { cout << v; }
inline void wrs(const string& s) { cout << s; }
inline void wrc(const char c) { cout << c; }
template <class T>
void wrv(T beg_iter, T end_iter, string separator = " ",
         string end_of_line = "\n") {
  for (; beg_iter != end_iter; ++beg_iter) {
    cout << *beg_iter;
    cout << separator;
  }
  cout << end_of_line;
}
}  // namespace io
using namespace io;
int main() {
  int n = rdi(), temp = 0, ans = 0;
  deque<int> hand(n), deck(n), pos(n + 1);
  for (int i = 0; i < n; ++i) pos[hand[i] = rdi()] = 0;
  for (int i = 0; i < n; ++i) pos[deck[i] = rdi()] = i + 1;
  set<int> h{hand.begin(), hand.end()};
  h.erase(0);
  deque<int> d{deck.begin(), deck.end()};
  for (int i = 0; i < n; i++) {
    if (h.size()) {
      d.push_back(*h.begin());
      h.erase(h.begin());
      if (d.front()) h.insert(d.front());
      d.pop_front();
    } else {
      bool ok = true;
      for (int i = 0; i < n; ++i)
        if (d[i] != i + 1) {
          ok = false;
          break;
        }
      if (ok) {
        printf("%d\n", i);
        return 0;
      } else
        break;
    }
  }
  if (pos[1] != 0) {
    temp = pos[1];
    hand.insert(hand.end(), deck.begin(), deck.begin() + temp);
    deck.erase(deck.begin(), deck.begin() + temp);
    sort((hand).begin(), (hand).end());
    deck.insert(deck.end(), hand.begin(), hand.begin() + temp);
    hand.erase(hand.begin(), hand.begin() + temp);
    for (int i = 0; i < n; ++i) pos[hand[i]] = 0, pos[deck[i]] = i + 1;
  }
  for (int i = 1; i < n + 1; ++i) ans = max(ans, pos[i] + n - i + 1);
  printf("%d\n", ans + temp);
  return 0;
}
