Nauuo is a girl who loves playing cards.

One day she was playing cards but found that the cards were mixed with some empty ones.

There are $n$ cards numbered from $1$ to $n$, and they were mixed with another $n$ empty cards. She piled up the $2n$ cards and drew $n$ of them. The $n$ cards in Nauuo's hands are given. The remaining $n$ cards in the pile are also given in the order from top to bottom.

In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.

Nauuo wants to make the $n$ numbered cards piled up in increasing order (the $i$-th card in the pile from top to bottom is the card $i$) as quickly as possible. Can you tell her the minimum number of operations?


-----Input-----

The first line contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of numbered cards.

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0\le a_i\le n$) — the initial cards in Nauuo's hands. $0$ represents an empty card.

The third line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($0\le b_i\le n$) — the initial cards in the pile, given in order from top to bottom. $0$ represents an empty card.

It is guaranteed that each number from $1$ to $n$ appears exactly once, either in $a_{1..n}$ or $b_{1..n}$.


-----Output-----

The output contains a single integer — the minimum number of operations to make the $n$ numbered cards piled up in increasing order.


-----Examples-----
Input
3
0 2 0
3 0 1

Output
2
Input
3
0 2 0
1 0 3

Output
4
Input
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10

Output
18


-----Note-----

Example 1

We can play the card $2$ and draw the card $3$ in the first operation. After that, we have $[0,3,0]$ in hands and the cards in the pile are $[0,1,2]$ from top to bottom.

Then, we play the card $3$ in the second operation. The cards in the pile are $[1,2,3]$, in which the cards are piled up in increasing order.

Example 2

Play an empty card and draw the card $1$, then play $1$, $2$, $3$ in order.

Difficulty: MEDIUM_HARD

Solutions:
from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
p = [0 for i in range(n + 1)]
for i in range(n):
	p[b[i]] = i + 1
if p[1]:
	i = 2
	while i <= n and p[i] == p[1] + i - 1:
		i += 1
	if p[i - 1] == n:
		j = i
		while j <= n and p[j] <= j - i:
			j += 1
		if j > n:
			print(n - i + 1)
			exit(0)
ans = 0
for i in range(1, n + 1):
	ans = max(ans, p[i] - i + 1 + n)
print(ans)


n = int(input())
a1 = list(map(int, input().split()))
a = list(map(int, input().split()))
a = [0] + a
d = [0] * (n + 1)
mx = 0
for i in range(1, n + 1):
	d[a[i]] = i
mx = max((d[i] - i + 1 for i in range(1, n + 1)))
br = n - d[1] + 1
b = True
if d[1] == 0:
	b = False
for i in range(1, n + 1):
	if i <= br:
		if d[i] - d[1] == i - 1:
			continue
		else:
			b = False
			break
	elif d[i] < i - br:
		continue
	else:
		b = False
		break
if b:
	print(n - br)
else:
	print(n + mx)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		if b[i] == 1:
			if b[i:] == [x for x in range(1, n - i + 1)]:
				for j in range(i):
					if b[j] == 0:
						continue
					if b[j] - n + i - 1 < j + 1:
						break
				else:
					return i
	if 1 in a:
		for j in range(n):
			if b[j] == 0:
				continue
			if b[j] < j + 2:
				break
		else:
			return n
	max_diff = -1
	j = -1
	k = -1
	for i in range(n):
		if b[i] == 0:
			continue
		if i + 1 - b[i] > max_diff:
			max_diff = i + 1 - b[i]
			j = i + 1
			k = b[i]
	return j + n - k + 1
print(solve())


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	p = [0 for i in range(n + 1)]
	for i in range(n):
		p[b[i]] = i + 1
	if p[1]:
		i = 2
		while i <= n and p[i] == p[1] + i - 1:
			i += 1
		if p[i - 1] == n:
			j = i
			while j <= n and p[j] <= j - i:
				j += 1
			if j > n:
				print(n - i + 1)
				exit()
	ans = 0
	for i in range(1, n + 1):
		ans = max(ans, p[i] - i + 1 + n)
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
hand = []
deck = []
s = [-1] * (n + 1)
for el in map(int, input().split()):
	hand.append(el)
	s[el] = 0
i = 1
for el in map(int, input().split()):
	deck.append(el)
	s[el] = i
	i += 1
if s[1] != 0:
	i = 2
	while i <= n and s[i] == s[1] + i - 1:
		i += 1
	if s[i - 1] == n:
		j = i
		while j <= n and s[j] <= j - i:
			j += 1
		if j > n:
			print(n - i + 1)
			exit()
ans = 0
for i in range(1, n + 1):
	ans = max(ans, s[i] - i + 1 + n)
print(ans)


from sys import stdin, exit
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
p = [0] * (n + 1)
for i in a:
	p[i] = 0
for (i, v) in enumerate(b):
	p[v] = i + 1
ans = 0
if p[1] > 0:
	i = 2
	while i <= n and p[i] == p[i - 1] + 1:
		i += 1
	if p[i - 1] == n:
		j = i
		while j <= n and p[j] <= j - i:
			j += 1
		if j == n + 1:
			print(p[1] - 1)
			exit(0)
ans = max([p[i] - i + 1 + n for i in range(1, n + 1)])
print(ans)


import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

def solve(N, A, B):
	p = [0 for _ in range(N + 1)]
	for (i, v) in enumerate(B):
		p[v] = i + 1
	if p[1]:
		i = 2
		while i <= N and p[i] == p[1] + i - 1:
			i += 1
		if p[i - 1] == N:
			j = i
			while j <= N and p[j] <= j - i:
				j += 1
			if j > N:
				return N - i + 1
	return max([p[i] - i + 1 + N for i in range(1, N + 1)])
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
print(solve(N, A, B))


import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
table = [0] * (N + 1)
for i in range(N):
	if b[i]:
		table[b[i]] = i + 1
last = N - table[1] + 1
islast = 1
if table[1] == 0:
	last = 0
	islast = 0
for i in range(1, last + 1):
	if table[i] != N - last + i:
		islast = 0
		break
if islast:
	for i in range(N - last):
		x = last + i + 1
		if table[x] > i:
			islast = 0
			break
	if islast:
		print(N - last)
		exit(0)
x = 0
for i in range(1, N + 1):
	x = max(table[i], x) + 1
print(x)


n = int(input())
(*hand,) = map(int, input().split())
(*pile,) = map(int, input().split())
dist = [0] * (n + 1)
for (index, card) in enumerate(pile):
	if card != 0:
		dist[card] = index + 1

def is_valid():
	if dist[1] == 0:
		numbers_placed = 0
	else:
		base = dist[1]
		for i in range(2, n - dist[1] + 2):
			if dist[i] != dist[1] + i - 1:
				numbers_placed = 0
				break
		else:
			numbers_placed = n - dist[1] + 1
	if numbers_placed != 0:
		for i in range(numbers_placed + 1, n + 1):
			if dist[i] >= i - numbers_placed:
				ans = dist[1] + n
				break
		else:
			ans = n - numbers_placed
		return ans
	else:
		delay = 0
		for i in range(1, n + 1):
			delay += max(dist[i] - (i - 1 + delay), 0)
		return delay + n
print(is_valid())


N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
getting = [0] * (N + 1)
flg = False
for i in range(N):
	b = b_list[i]
	getting[b] = i + 1
	if flg and b_list[i - 1] != b - 1:
		flg = False
	if b == 1:
		flg = True
if flg:
	start = b_list[-1] + 1
	step = 0
	for i in range(start, N + 1):
		if getting[i] > step:
			flg = False
			break
		step += 1
	if flg:
		print(N - start + 1)
		exit()
ans = 0
for i in range(1, N + 1):
	if getting[i] > i - 1 and ans < getting[i] - (i - 1):
		ans = getting[i] - (i - 1)
print(N + ans)


from copy import deepcopy
import heapq

def main():
	buf = input()
	n = int(buf)
	buf = input()
	buflist = buf.split()
	a = set(map(int, buflist))
	buf = input()
	buflist = buf.split()
	b = list(map(int, buflist))
	if 0 in a:
		a.remove(0)
	a = list(a)
	heapq.heapify(a)
	next_card = b[-1]
	if next_card != 0:
		expected_card = next_card - 1
		next_card += 1
		pointer = -2
		while expected_card > 0:
			if b[pointer] != expected_card:
				break
			pointer -= 1
			expected_card -= 1
		if expected_card == 0:
			a_sp = deepcopy(a)
			pointer = 0
			while next_card <= n:
				if not a_sp:
					break
				if a_sp[0] != next_card:
					break
				heapq.heappop(a_sp)
				if b[pointer] > 0:
					heapq.heappush(a_sp, b[pointer])
				pointer += 1
				next_card += 1
			if next_card > n:
				print(n - b[-1])
				return
	b_pointer = 0
	drawn = 0
	next_card = 1
	while next_card <= n:
		if a:
			if a[0] == next_card:
				heapq.heappop(a)
				next_card += 1
		if b_pointer < n:
			if b[b_pointer] > 0:
				heapq.heappush(a, b[b_pointer])
		b_pointer += 1
		drawn += 1
	print(drawn)
main()


if True:
	N = int(input())
	A = [int(a) for a in input().split()]
	B = [int(a) for a in input().split()]
else:
	A = [0, 0, 0, 5, 0, 0, 0, 4, 0, 0, 11]
	B = [9, 2, 6, 0, 8, 1, 7, 0, 3, 0, 10]
	A = [0, 2, 0]
	B = [3, 0, 1]
	N = len(A)

def chk():
	i = 0
	while B[i] != 1:
		i += 1
		if i == N:
			return 0
	j = i - 1
	while i < N:
		if B[i] != i - j:
			return 0
		i += 1
	for i in range(j + 1):
		if 0 < B[i] < N + 1 - j + i:
			return 0
	return j + 2
c = chk()
if c > 0:
	print(c - 1)
else:
	print(max([max(i - B[i] + 2, 0) if B[i] else 0 for i in range(N)]) + N)


n = int(input())
a = [None] + list(map(int, input().split()))
b = [None] + list(map(int, input().split()))
p = [None] * (n + 1)
for i in range(1, n + 1):
	p[a[i]] = 0
	p[b[i]] = i
if p[1]:
	i = 2
	while i <= n and p[i] == p[1] + i - 1:
		i += 1
	if p[i - 1] == n:
		j = i
		while j <= n and p[j] <= j - i:
			j += 1
		if j > n:
			print(n - i + 1)
			exit()
ans = 0
for i in range(1, n + 1):
	ans = max(ans, p[i] - i + 1 + n)
print(ans)


n = int(input(''))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] * n
for i in range(n):
	if a[i] == 0:
		a[i] = n * 20
	else:
		c[a[i] - 1] = -1
	if b[i] == 0:
		b[i] = n * 20
	else:
		c[b[i] - 1] = i
d = c.copy()
l = b[-1]
ll = l
f = 0
for i in range(1, l + 1):
	if l == 20 * n:
		f = 1
		break
	j = i * -1
	if b[j] != ll:
		f = 1
		break
	ll -= 1
if f == 0:
	j = 0
	for i in range(l, n):
		if d[i] == -1:
			if b[j] <= n:
				d[b[j] - 1] = -1
			j += 1
		else:
			f = 1
			break
if f == 0:
	print(n - l)
	exit()
m = min(b)
ind = b.index(m)
if m == 1:
	for i in range(ind + 1):
		if b[i] != 20 * n:
			c[b[i] - 1] = -1
	j = 2
	fe = -1
	fi = -1
	ans = -1
	for i in range(ind + 1, n):
		if b[i] < j:
			fe = b[i]
			fi = j
			ans = max(fi - fe, ans)
		j += 1
	if fe == -1:
		print(ind + n + 1)
	else:
		print(ind + 1 + n + ans)
	exit()
else:
	j = 2
	fe = -1
	fi = -1
	ans = -1
	for i in range(n):
		if b[i] < j:
			fe = b[i]
			fi = j
			ans = max(fi - fe, ans)
		j += 1
	if fe == -1:
		print(n)
	else:
		print(ans + n)


for _ in range(1):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	k = b[n - 1]
	for i in range(k):
		if b[n - i - 1] != k - i:
			k = 0
	ma = 0
	for i in range(n - k):
		if b[i] == 0:
			continue
		if b[i] - k - 1 <= i:
			ma = max(ma, i - b[i] + k + 2)
	if ma == 0:
		print(n - k)
	elif k == 0:
		print(n + ma)
	else:
		k = 0
		ma = 0
		for i in range(n - k):
			if b[i] == 0:
				continue
			if b[i] - k - 1 <= i:
				ma = max(ma, i - b[i] + k + 2)
		print(n + ma)


N = 200005
n = int(input())
k = list(map(int, input().split()))
b = list(map(int, input().split()))
p = [0 for i in range(N)]
done = 0
b.insert(0, 0)
for i in range(1, n + 1):
	p[b[i]] = i
if p[1] != 0:
	i = 2
	while p[i] == p[1] + i - 1:
		i += 1
	if p[i - 1] == n:
		j = i
		while p[j] <= j - i and j <= n:
			j += 1
		if j > n:
			print(n - i + 1)
			done = 1
mx = 0
if done == 0:
	for i in range(1, n + 1):
		mx = max(mx, p[i] - i + 1 + n)
	print(mx)


import sys, heapq
from collections import deque, defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl = lambda : list(map(int, input().split()))
inm = lambda : map(int, input().split())
DBG = True and False

def ddprint(x):
	if DBG:
		print(x)
n = inn()
a = inl()
b = inl()
sorted = b[n - 1]
if sorted > 0:
	ok = True
	for i in range(sorted):
		if b[n - 1 - i] != sorted - i:
			ok = False
			break
	if not ok:
		sorted = 0
ddprint('sorted {}'.format(sorted))
ok = True
for i in range(n - sorted):
	if b[i] > 0 and b[i] - sorted - 1 <= i:
		ok = False
		break
if ok:
	print(n - sorted)
	exit()
mx = 0
for i in range(n):
	if b[i] == 1:
		ddprint('i {} bi 1! mx {} up {}'.format(i, mx, mx < i + 1))
		mx = max(mx, i + 1)
	elif b[i] > 0:
		ddprint('i {} bi {} mx {} up {}'.format(i, b[i], mx, mx < i - b[i] + 1))
		mx = max(mx, i - b[i] + 2)
print(mx + n)


def get_int_list():
	return list(map(int, input().split()))

def get_max_repeats(l):
	if len(l) == 0:
		return 0
	repeats = 1
	max_repeats = 1
	last_element = l[0]
	for i in range(1, len(l)):
		if l[i] == last_element:
			repeats += 1
		else:
			if repeats > max_repeats:
				max_repeats = repeats
			repeats = 1
		last_element = l[i]
	if repeats > max_repeats:
		max_repeats = repeats
	return max_repeats

def order(pile):
	if pile[-1] == 1:
		return 1
	if len(pile) == 1:
		return 0
	for i in range(len(pile) - 2, -1, -1):
		if pile[i] == 1 and pile[i + 1] == 2:
			return len(pile) - i
		elif pile[i] == pile[i + 1] - 1:
			continue
		else:
			return 0
n = int(input())
hand = get_int_list()
pile = get_int_list()
places = {}
for i in range(n):
	if hand[i] > 0:
		places[hand[i]] = 0
	if pile[i] > 0:
		places[pile[i]] = i + 1
offset = order(pile)
if offset > 0:
	in_order = True
	for i in range(offset + 1, n + 1):
		if places[i] == 0:
			continue
		distance = places[i] - (i - 1) + offset
		if distance > 0:
			in_order = False
			break
	if in_order:
		print(str(n - offset))
	else:
		one_to_hand = places[1]
		max_distance = 0
		for i in range(2, n + 1):
			if places[i] == 0:
				continue
			distance = places[i] - (i - 1) - one_to_hand
			if distance > max_distance:
				max_distance = distance
		moves = one_to_hand + max_distance + n
		print(str(moves))
else:
	one_to_hand = places[1]
	max_distance = 0
	for i in range(2, n + 1):
		if places[i] == 0:
			continue
		distance = places[i] - (i - 1) - one_to_hand
		if distance > max_distance:
			max_distance = distance
	moves = one_to_hand + max_distance + n
	print(str(moves))


from sys import stdin
from collections import deque
n = int(stdin.readline())
hand = [int(x) for x in stdin.readline().split()]
deck = [int(x) for x in stdin.readline().split()]
ind = {}
for (i, x) in enumerate(deck):
	if x != 0:
		ind[x] = i
offset = sorted([(ind[i] - i + 2, i) for i in ind])
if not offset:
	print(n)
elif not 1 in deck:
	print(max(offset[-1][0] + n, n))
else:
	i1 = deck.index(1)
	newD = deck[i1:]
	valid = True
	for x in range(len(newD) - 1):
		if newD[x] > newD[x + 1]:
			valid = False
			break
	if valid:
		v2 = True
		newOff = i1 - n
		for (o, x) in offset:
			if x > -newOff and o > newOff:
				v2 = False
				break
		if v2:
			print(newOff + n)
		else:
			print(max(offset[-1][0] + n, n))
	else:
		print(max(offset[-1][0] + n, n))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = -1
r = n
moment = [0] * (n + 1)
max_post = 1
while b[(-max_post - 1) % n] != 0 and b[(-max_post - 1) % n] + 1 == b[-max_post]:
	max_post += 1
if b[-max_post] != 1:
	max_post = 0
for i in range(n):
	moment[b[i]] = i + 1
m = -1
for i in range(max_post + 1, n + 1):
	m = max(m, moment[i] - i + max_post)
m += 1
if m > 0:
	m = -2
	for i in range(1, n + 1):
		m = max(m, moment[i] - i)
	m += 1
	print(m + n)
else:
	print(max(0, m + n - max_post))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
f = True
if 1 in b:
	j = b.index(1)
	for i in range(n - j):
		d = i + 1 - b[j + i]
		if d != 0:
			break
	else:
		s = -2
		for k in range(j):
			if b[k] != 0 and b[k] - k <= n - (j - 1):
				break
		else:
			print(j)
			f = False
if f:
	s = -2
	for k in range(n):
		if b[k] != 0:
			s = max(s, k - b[k])
	print(s + n + 2)


from sys import stdin
from collections import deque
n = int(stdin.readline().strip())
s = set(map(int, stdin.readline().strip().split()))
s1 = deque(map(int, stdin.readline().strip().split()))
ans = 0
st = s.copy()
s2 = s1.copy()
ans1 = 0
while s2[-1] + 1 in st:
	st.add(s2[0])
	st.remove(s2[-1] + 1)
	s2.append(s2[-1] + 1)
	s2.popleft()
	ans1 += 1
f = True
for i in range(n):
	if s2[i] != i + 1:
		f = False
		break
if f:
	print(ans1)
	exit(0)
if 1 not in s:
	while s1[0] != 1:
		s.add(s1[0])
		s1.popleft()
		s1.append(0)
		ans += 1
	ans += 1
	s.add(s1[0])
	s1.popleft()
	s1.append(0)
dist = [0 for i in range(n + 2)]
mx = 0
for i in range(n):
	if s1[i] != 0:
		dist[s1[i]] = max(0, i - (s1[i] - 2))
	mx = max(mx, dist[s1[i]])
print(mx + ans + n)


RI = lambda : [int(x) for x in input().strip().split()]
rw = lambda : input().strip().split()
from collections import defaultdict as df
n = int(input())
m = RI()
l = RI()
index = df(int)
for i in range(n):
	if l[i] != 0:
		index[l[i]] = i + 1
extra = [0] * (n + 1)
for i in range(n):
	if index[i + 1] == 0:
		extra[i + 1] = 1
	else:
		extra[i + 1] = index[i + 1] + 1
e = n
s = n - 1
while l[s] != 1:
	if l[s - 1] == l[s] - 1:
		s -= 1
		continue
	else:
		break
if l[s] == 1:
	for i in range(n - s):
		extra[i + 1] = -1
sumo = 0
flag = 0
for i in range(n):
	if extra[i + 1] == -1:
		continue
	if extra[i + 1] <= sumo + 1:
		sumo += 1
	else:
		flag = 1
		break
if flag == 0:
	print(sumo)
else:
	for i in range(n):
		if index[i + 1] == 0:
			extra[i + 1] = 1
		else:
			extra[i + 1] = index[i + 1] + 1
	sumo = 0
	for i in range(n):
		if extra[i + 1] <= sumo + 1:
			sumo += 1
		else:
			sumo = extra[i + 1]
	print(sumo)


n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
flag = 0
if 1 in l2:
	c = l2.index(1)
	for i in range(n - c):
		d = i + 1 - l2[c + i]
		if d != 0:
			break
	else:
		for k in range(c):
			if l2[k] != 0 and l2[k] - k <= n - (c - 1):
				break
		else:
			print(c)
			flag = 1
if flag == 0:
	s = -2
	for i in range(n):
		if l2[i] != 0:
			s = max(s, i - l2[i])
	print(s + n + 2)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
inhand = [0 for i in range(2 * n + 2)]
if 1 in b:
	loc1 = b.index(1)
	asc = 1
	for i in range(loc1, n):
		if b[i] > asc:
			asc = 1
			break
		if b[i] == asc:
			asc += 1
	moves = 0
	if asc > 1:
		for i in range(n):
			inhand[a[i]] = 1
		for i in range(2 * n):
			if asc > n:
				break
			if inhand[asc]:
				asc += 1
			inhand[b[moves % n]] = 1
			moves += 1
		if moves <= loc1:
			print(moves)
			exit()
asc = 1
inhand = [0 for i in range(n + 1)]
for i in range(n):
	inhand[a[i]] = 1
moves = 0
for i in range(2 * n + 2):
	if asc > n:
		break
	if inhand[asc]:
		asc += 1
	inhand[b[moves % n]] = 1
	moves += 1
print(moves)


import sys
import math
import array
import bisect
import collections
from collections import Counter, defaultdict
import fractions
import heapq
import re
sys.setrecursionlimit(1000000)

def array2d(dim1, dim2, init=None):
	return [[init for _ in range(dim2)] for _ in range(dim1)]

def argsort(l, reverse=False):
	return sorted(range(len(l)), key=lambda i: l[i], reverse=reverse)

def argmin(l):
	return l.index(min(l))

def YESNO(ans, yes='YES', no='NO'):
	print([no, yes][ans])
II = lambda : int(input())
MI = lambda : map(int, input().split())
MIL = lambda : list(MI())
MIS = lambda : input().split()

def find_t(piles):
	try:
		ind = piles.index(1)
		for i in range(ind, len(piles) - 1):
			if piles[i + 1] - piles[i] == 1:
				continue
			else:
				return 0
		else:
			return len(piles) - ind
	except:
		return 0

def main():
	N = II()
	hands = MIL()
	piles = MIL()
	if 0 not in hands:
		return N
	t = find_t(piles)
	if t != 0:
		if not any((c != 0 and i + 2 > c - t for (i, c) in enumerate(piles[:-t]))):
			return N - t
	return max(max((i - c for (i, c) in enumerate(piles) if c != 0)) + 2 + N, N)
print(main())


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)
n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

def get_min(start_idx, l):
	min_value = -1
	min_index = start_idx
	for i in range(start_idx, n):
		v = l[i]
		if v != 0 and (min_value > v or min_value == -1):
			min_index = i
			min_value = v
	if min_value == -1:
		return (n, 0)
	return (min_index, min_value)
(min_index, min_value) = get_min(0, b_list)
if min_value == 1:
	flag = False
	for i in range(min_index + 1, n):
		v = b_list[i]
		if v - 1 != i - min_index:
			flag = True
			break
	ls = n - min_index
	fl = [0 for _ in range(n + 1)]
	for (i, v) in enumerate(a_list):
		fl[v] = 1
	for i in range(min_index):
		if fl[ls + i + 1] != 1:
			flag = True
		fl[b_list[i]] = 1
	if not flag:
		print(min_index)
		sys.exit()
pos = [0 for i in range(n + 1)]
for (i, v) in enumerate(b_list):
	pos[v] = i + 1
answer = -1
for i in range(1, n + 1):
	answer = max(answer, n + 1 - i + pos[i])
print(answer)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
r = 0
for ai in a:
	if ai != 0:
		r = max(r, n - ai + 1)
b1 = [0] * (n + 1)
b2 = [0] * (n + 1)
bset = set()
for i in range(n):
	if b[i] != 0:
		b2[b[i]] = i + 1 + n - b[i] + 1
		b1[b[i]] = i + 1 - b[i]
		if b1[b[i]] < 0 or b1[b[i]] < r:
			b1[b[i]] = b2[b[i]]
		bset.add(b1[b[i]])
if len(bset) == 0:
	r = n
else:
	mxbset = max(bset)
	bset2 = set()
	for i in range(1, n + 1):
		if b2[i] != 0:
			if b2[i] <= mxbset:
				bset2.add(mxbset)
			else:
				bset2.add(b1[i])
	if len(bset2) == 1 and max(bset2) >= r:
		r = max(bset2)
	else:
		r = max(r, max(b2))
print(r)


from sys import stdin
from copy import deepcopy
from collections import deque

def find_start(pile):
	start = 1
	index = -1
	for i in range(len(pile)):
		if pile[i] == start:
			index = i
			break
	latest = start
	if index != -1:
		flag = True
		for i in range(index, len(pile)):
			if pile[i] == latest:
				latest += 1
			else:
				flag = False
				break
		if flag:
			start = latest
	return start

def check_start(num, op):
	op[0] = 0
	nonlocal arr
	nonlocal barr
	mydict = [0] * (n + 1)
	for i in range(len(arr)):
		mydict[arr[i]] += 1
	zeroes = mydict[0]
	pile = deque(barr[:])
	for i in range(num):
		if zeroes > 0:
			op[0] += 1
			pile.append(0)
			zeroes -= 1
			mydict[0] -= 1
		else:
			return False
		element = pile.popleft()
		mydict[element] += 1
		if element == 0:
			zeroes += 1
	start = find_start(barr)
	for i in range(start, n + 1):
		if mydict[i] > 0:
			op[0] += 1
			pile.append(i)
			mydict[i] -= 1
			element = pile.popleft()
			mydict[element] += 1
		else:
			return False
	return True
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
barr = list(map(int, stdin.readline().split()))
start = 0
end = 2 * n
op = [0]
if check_start(0, op):
	print(op[0])
	return
pos = [0] * (n + 1)
for i in range(n):
	pos[barr[i]] = i + 1
ans = -float('inf')
for i in range(1, n + 1):
	ans = max(pos[i] - i + 1 + n, ans)
print(ans)


def index(l):
	for i in range(n):
		if l[i] > 0:
			j = i + 1 - l[i]
			l1.append(j)
	return l1
n = int(input())
hand = [int(g) for g in input().split()]
deck = [int(g) for g in input().split()]
if 1 in deck:
	index2 = deck.index(1)
l1 = []
buffer = deck[:]
buffer.sort()
l2 = index(deck)
if len(set(deck)) == 1 and set(deck) == {0}:
	output = n
elif 0 not in deck and deck == buffer:
	output = 0
elif 1 in deck:
	if index2 == 0:
		output = max(l2) + n + 1
	if set(deck[:index2]) == {0}:
		li = [int(g) for g in range(1, n - index2 + 1)]
		if deck[index2:] == li:
			output = index2
		else:
			output = max(l2) + n + 1
	else:
		for i in range(index2):
			if deck[i] > 0:
				if index2 - i >= n - deck[i] + 2:
					output = l2[-1]
				else:
					output = max(l2) + n + 1
					break
elif max(l2) <= -1:
	output = n
else:
	output = max(l2) + n + 1
print(output)


import sys
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if max(B) == 0:
	print(n)
	sys.exit()
if 1 in B:
	x = B.index(1)
	y = n - x + 1
	for i in range(x, n):
		if B[i] != i - x + 1:
			break
	else:
		for i in range(x):
			if B[i] > 0 and B[i] - y - i <= 0:
				break
		else:
			print(x)
			sys.exit()
ANS = -2
for i in range(n):
	if B[i] != 0:
		ANS = max(ANS, i - B[i])
print(ANS + n + 2)


import sys
n = int(input())
a = [0]
a.extend(list(map(int, input().split())))
b = [0]
b.extend(list(map(int, input().split())))
p = [0 for _ in range(n + 10)]
for i in range(1, n + 1):
	p[a[i]] = 0
for i in range(1, n + 1):
	p[b[i]] = i
if p[1]:
	i = 2
	while p[i] == p[1] + i - 1:
		i += 1
	if p[i - 1] == n:
		j = i
		while j <= n and p[j] <= j - i:
			j += 1
		if j > n:
			print(n - i + 1)
			sys.exit(0)
ans = 0
for i in range(1, n + 1):
	ans = max(ans, p[i] - i + 1 + n)
print(ans)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, OrderedDict, deque
import math
import heapq

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
n = intIn()
a = set(listIn())
pile = deque(listIn())
op = 0
a1 = a.copy()
p = pile.copy()
x = pile[-1]
start = x
while x + 1 in a1:
	op += 1
	a1.add(p[0])
	a1.remove(x + 1)
	p.append(x + 1)
	p.popleft()
	x += 1
f = 1
for v in range(n):
	if p[v] != v + 1:
		f = 0
		break
if f:
	print(op)
	exit(0)
op = 0
if 1 not in a:
	while pile[0] != 1:
		op += 1
		a.add(pile[0])
		pile.popleft()
		pile.append(0)
	op += 1
	a.add(pile[0])
	pile.popleft()
	pile.append(0)
maxx = 0
for i in range(n):
	m = 0
	if pile[i] != 0:
		idx = i
		num = pile[i] - 1
		m = max(0, idx - (num - 1))
	maxx = max(m, maxx)
print(maxx + op + n)


import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
tmp = B[-1]
ok = True
if tmp != 0:
	for i in reversed(range(N)):
		b = B[i]
		if tmp - (N - 1 - i) > 0:
			if b != tmp - (N - 1 - i):
				ok = False
	for i in range(N - tmp):
		b = B[i]
		if b > 0 and b <= tmp + i + 1:
			ok = False
else:
	ok = False
if ok:
	ans = N - tmp
else:
	ans = N
	for (i, b) in enumerate(B):
		if b != 0:
			ans = max(N - b + 2 + i, ans)
print(ans)


n = int(input())
_ = input()
ar = list(map(int, input().split()))
idx = 0
idx2 = -2
flag = False
for (i, e) in enumerate(ar):
	idx = max(idx, 0 if e == 0 else i - e + 2)
	if e == 1 and idx2 < i - 1:
		flag = True
		i1 = i
		s = 0
	idx2 = max(idx2, -2 if e == 0 else i + n - e)
	if flag:
		if s + 1 == e:
			s += 1
		else:
			flag = False
if flag:
	print(i1)
else:
	print(n + idx)


from math import ceil

class cinn:

	def __init__(self):
		self.x = []

	def cin(self, t=int):
		if len(self.x) == 0:
			a = input()
			self.x = a.split()
			self.x.reverse()
		return self.get(t)

	def get(self, t):
		return t(self.x.pop())

	def clist(self, n, t=int):
		l = [0] * n
		for i in range(n):
			l[i] = self.cin(t)
		return l

	def clist2(self, n, t1=int, t2=int, t3=int, tn=2):
		l = [0] * n
		for i in range(n):
			if tn == 2:
				a1 = self.cin(t1)
				a2 = self.cin(t2)
				l[i] = (a1, a2)
			elif tn == 3:
				a1 = self.cin(t1)
				a2 = self.cin(t2)
				a3 = self.cin(t3)
				l[i] = (a1, a2, a3)
		return l

	def clist3(self, n, t1=int, t2=int, t3=int):
		return self.clist2(self, n, t1, t2, t3, 3)

	def cout(self, i, ans=''):
		if ans == '':
			print('Case #' + str(i + 1) + ':', end=' ')
		else:
			print('Case #' + str(i + 1) + ':', ans)

	def printf(self, thing):
		print(thing, end='')

	def countlist(self, l, s=0, e=None):
		if e == None:
			e = len(l)
		dic = {}
		for el in range(s, e):
			if l[el] not in dic:
				dic[l[el]] = 1
			else:
				dic[l[el]] += 1
		return dic

	def talk(self, x):
		print(x, flush=True)

	def dp1(self, k):
		L = [-1] * k
		return L

	def dp2(self, k, kk):
		L = [-1] * k
		for i in range(k):
			L[i] = [-1] * kk
		return L
c = cinn()
n = c.cin()
hand = c.clist(n)
stack = c.clist(n)
lpos = [0] * (n + 1)
for i in range(n):
	el = hand[i]
	el2 = stack[i]
	lpos[el] = 0
	lpos[el2] = i + 1
ideal = n
for j in range(n):
	if j + lpos[1] - 1 == n:
		ideal = n - lpos[1] + 1
		break
	if not stack[j + lpos[1] - 1] == j + 1:
		ideal = 0
		break
if ideal != 0:
	good = True
	for i in range(ideal + 1, n + 1):
		if lpos[i] > i - ideal - 1:
			good = False
			break
	if good == True:
		print(lpos[1] - 1)
	else:
		print(lpos[1] + n)
else:
	maxdist = 0
	for i in range(1, n + 1):
		if lpos[i] > i - 1:
			if maxdist < lpos[i] - i + 1:
				maxdist = lpos[i] - i + 1
	print(maxdist + n)


def ain():
	return map(int, input().split())

def lin():
	return list(ain())

def plist(l):
	for x in l:
		print(x, end=' ')
	print()

def indexof(l, v, l3):
	return l3[v]
n = int(input())
l1 = lin()
l2 = lin()
l3 = [-1] * (n + 1)
l4 = [0] * (n + 1)
for i in range(n):
	x = l2[i]
	if x != 0:
		l3[x] = i
ui = 0
if n == 1:
	if l2[0] == 1:
		print(0)
	else:
		print(1)
	ui = 1
if indexof(l2, 1, l3) >= 0 and indexof(l2, 1, l3) != n - 1 and (ui == 0):
	i = indexof(l2, 1, l3)
	j = 1
	fk = 0
	while i < n:
		if l2[i] != j:
			fk = 1
		i += 1
		j += 1
	if fk == 0:
		x = l2[n - 1]
		fm = 0
		for i in range(1, n - l2[n - 1] + 1):
			if indexof(l2, x + i, l3) >= i - 1:
				fm = 1
				break
		if fm == 0:
			print(indexof(l2, 1, l3))
			ui = 1
if l2[n - 1] != 1 and ui == 0:
	if indexof(l2, 1, l3) == -1:
		w = 0
		for j in range(n):
			if l2[j] != 0:
				w = max(w, j - l2[j] + 2)
	else:
		i1 = indexof(l2, 1, l3)
		w = i1 + 1
		for j in range(i1 + 1, n):
			if l2[j] != 0:
				w = max(w, j - l2[j] + 2)
	print(w + n)
elif ui == 0:
	fl = 0
	for i in range(n - 1):
		if l2[i] - i < 3 and l2[i] != 0:
			fl = 1
			break
	if 2 not in l1:
		fl = 1
	if fl == 0:
		print(n - 1)
	else:
		print(2 * n)


n = int(input())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
arr3 = [0]
mini = n + 1
for i in range(0, len(arr2)):
	if arr2[i] < mini and arr2[i] != 0:
		mini = arr2[i]
if mini == n + 1:
	print(len(arr1))
	exit()
q = n + 1
a = 0
b = 0
m = 0
for i in range(0, len(arr2)):
	if mini == arr2[i]:
		q = int(i)
	if arr2[i] != 0:
		m = max(i + 1 - arr2[i] + 1, m)
c = 1
count = 0
if arr2[q] == 1:
	for i in range(q + 1, len(arr2)):
		c += 1
		if arr2[i] == c:
			count += 1
		else:
			break
	a1 = 0
	b1 = 0
	if count == len(arr2) - q - 1:
		for j in range(0, q):
			if arr2[j] != 0:
				if arr2[j] <= arr2[count + q] + 1 + j:
					print(m + n)
					exit()
		print(q)
		exit()
	print(m + n)
	exit()
print(m + n)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
avail = [0 for i in range(n + 1)]
cont = False
for i in range(n):
	avail[b[i]] = i + 1
	if cont and b[i - 1] != b[i] - 1:
		cont = False
	if b[i] == 1:
		cont = True
if cont:
	start = b[-1] + 1
	step = 0
	for i in range(start, n + 1):
		if avail[i] > step:
			cont = False
			break
		step += 1
	if cont:
		print(n - start + 1)
		exit()
max_avail = 0
for i in range(1, n + 1):
	if avail[i] > i - 1 and max_avail < avail[i] - (i - 1):
		max_avail = avail[i] - (i - 1)
print(max_avail + n)


from sys import *
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
if not max(b):
	print(n)
	exit()
if 1 in b:
	x = b.index(1)
	for i in range(x + 1, n):
		if b[i] != i - x + 1:
			break
	else:
		for i in range(x):
			if b[i] and b[i] - (n - x + 2 + i) < 0:
				break
		else:
			print(x)
			exit()
ex = 0
for i in range(n):
	if b[i]:
		ex = max(ex, i - (b[i] - 2))
print(n + ex)


import sys
import math
from itertools import permutations
input = sys.stdin.readline
n = int(input())
alist = list(map(int, input().split()))
blist = list(map(int, input().split()))
arr = [0] * n
ans = 0
for i in range(n):
	if alist[i] > 0:
		arr[alist[i] - 1] = 1
now = -1
for i in range(n):
	if arr[i] == 0:
		now = i
		break
if now == -1:
	print(n)
else:
	flag = True
	if arr[0] == 0:
		ind1 = blist.index(1)
		for i in range(ind1 + 1, n):
			if blist[i] != i - ind1 + 1:
				flag = False
				break
	if arr[0] == 0 and flag:
		cur = n - ind1 - 1
		for i in range(n - ind1, n):
			if arr[i] == 0:
				cur = i
				break
		flag1 = True
		for i in range(ind1):
			if blist[i] > 0:
				if blist[i] <= n + i - ind1 + 1:
					flag1 = False
					break
		if flag1:
			print(ind1)
		else:
			maxx = -1000000
			for i in range(n):
				if blist[i] > 0:
					maxx = max(maxx, i - blist[i] + 2)
			print(n + maxx)
	else:
		maxx = 0
		for i in range(n):
			if blist[i] > 0:
				maxx = max(maxx, i - blist[i] + 2)
		print(n + maxx)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ainds = [None] * n
binds = [None] * n
for i in range(n):
	if a[i] > 0:
		ainds[a[i] - 1] = i
	if b[i] > 0:
		binds[b[i] - 1] = i
if binds[0] is not None:
	if b[binds[0]:] == [i + 1 for i in range(n - binds[0])]:
		good = True
		for i in range(n - binds[0], n):
			if binds[i] is not None:
				if binds[i] > i - n + binds[0] - 1:
					good = False
		if good:
			print(binds[0])
		else:
			print(binds[0] + n + 1)
	else:
		extra = 0
		for i in range(n):
			if binds[i] is not None:
				extra = max(extra, binds[i] - binds[0] - i)
		print(binds[0] + 1 + n + extra)
else:
	extra = 0
	for i in range(n):
		if binds[i] is not None:
			extra = max(extra, binds[i] - i + 1)
	print(n + extra)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def bs(f, mi, ma):
	mm = -1
	while ma > mi:
		mm = (ma + mi) // 2
		if f(mm):
			mi = mm + 1
		else:
			ma = mm
	if f(mm):
		return mm + 1
	return mm

def main():
	n = I()
	a = LI()
	b = LI()
	if b == list(range(1, n + 1)):
		return 0
	ss = set(a)
	if 1 not in ss:
		i = b.index(1)
		bf = 1
		for j in range(i, n):
			if b[j] != j - i + 1:
				bf = 0
				break
		if bf:
			t = b[-1]
			s = ss | set()
			for j in range(n - t):
				if t + j + 1 not in s:
					bf = 0
					break
				s.add(b[j])
			if bf:
				return n - t

	def f(i):
		s = ss | set(b[:i])
		for j in range(1, n + 1):
			if j not in s:
				return True
			if i + j <= n:
				s.add(b[i + j - 1])
		return False
	r = bs(f, 0, n)
	return r + n
print(main())


import sys
n = int(input())
b = [0] + list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
maks = dmn = 0
satu = False
for i in range(1, n + 1):
	if b[i] == 1:
		satu = True
if satu:
	for i in range(1, n + 1):
		if a[i] != 0 and a[i] - 1 < i:
			maks = max(maks, i - a[i] + 1)
	print(n + maks)
	sys.exit()
dua = True
boo = False
for i in range(1, n + 1):
	if boo and a[i] != a[i - 1] + 1:
		dua = False
	if a[i] == 1:
		boo = True
if dua:
	ans = True
	for i in range(1, n + 1):
		if a[i] == 1:
			dmn = i
	for i in range(1, n + 1):
		if a[i] == 1:
			break
		if a[i] == 0:
			continue
		if a[i] - a[n] <= i:
			ans = False
	if ans:
		print(dmn - 1)
	else:
		print(dmn + n)
	sys.exit()
for i in range(1, n + 1):
	if a[i] == 1:
		dmn = i
boo = False
for i in range(1, n + 1):
	if boo and a[i] != 0:
		maks = max(maks, i - dmn - a[i] + 1)
	if a[i] == 1:
		boo = True
print(n + maks + dmn)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if n == 1:
	if a[0] == 0:
		print(0)
		exit()
	else:
		print(1)
		exit()
yes = True
pos = -1
for i in range(n - 2, -1, -1):
	if b[i] == 1:
		pos = i
		break
	elif b[i] + 1 == b[i + 1]:
		continue
	else:
		yes = False
if b[-1] == 1:
	yes = True
	pos = n - 1
if yes:
	min_len = -10000000000000
	for i in range(pos):
		if b[i] == 0:
			continue
		min_len = max(i - b[i] + 2, min_len)
	if min_len + (n - pos) <= 0:
		print(pos)
		exit()
min_len = 0
for i in range(n):
	if b[i] == 0:
		continue
	min_len = max(i - b[i] + 2, min_len)
res = n + min_len
print(res)


import sys
input = sys.stdin.readline
from collections import *
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
idx = defaultdict(int)
s = set(b)
for i in range(n):
	idx[b[i]] = i
la = b[-1]
if b[n - la:] == [i for i in range(1, la + 1)]:
	flag = True
	for i in range(la + 1, n + 1):
		if i in s:
			if idx[i] > i - 2 - la:
				flag = False
	if flag:
		print(n - la)
		exit()
ans = 0
for i in range(1, n + 1):
	if i in s:
		x = idx[i] - i + 2
		ans = max(ans, x)
ans += n
print(ans)


n = int(input())
a = [0 if i == 0 else n - i + 1 for i in map(int, input().split())]
b = list(map(int, input().split()))
t = True
s = 0
if 1 in b:
	i = 0
	while b[i] != 1:
		b[i] = 0 if b[i] == 0 else i + n - b[i] + 2
		i += 1
	s = i
	while i < n - 1:
		if b[i + 1] == b[i] + 1:
			i += 1
		else:
			break
	if i == n - 1:
		if s == 0:
			print(0)
		elif max(max(a), max(b[:s])) > s:
			t = False
		else:
			print(s)
	else:
		t = False
	if not t:
		for i in range(s, n):
			b[i] = 0 if b[i] == 0 else i + n - b[i] + 2
		print(max(max(a), max(b)))
else:
	for i in range(n):
		b[i] = 0 if b[i] == 0 else i + n - b[i] + 2
	print(max(max(a), max(b)))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
f = False
if list(range(1, b[-1] + 1)) == b[-b[-1]:]:
	f = True
	s = set(a)
	for i in range(n - b[-1]):
		if b[-1] + i + 1 in s:
			s.add(b[i])
		else:
			f = False
			break
	if f:
		print(n - b[-1])
if not f:
	print(max([-2] + [i - b[i] for i in range(n) if b[i]]) + 2 + n)


from sys import exit
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

def checkran(X):
	m = X[-1]
	if m == 0:
		return 0
	t = m
	for x in X[::-1]:
		if x != t:
			return 0
		t -= 1
		if t == 0:
			return m

def check(X, k):
	for (i, x) in enumerate(X[:N - k]):
		if x == 0:
			continue
		if k + 1 + i >= x:
			return False
	return True
k = checkran(B)
if k:
	if check(B, k):
		print(N - k)
		exit()
m = 0
for (i, b) in enumerate(B, 1):
	if b == 1:
		m = i
		break
C = B[m:]
C = [i - c for (i, c) in enumerate(C, 2) if c != 0] + [0]
print(m + N + max(C))


num = int(input())
junk = input()
data = [int(i) for i in input().split()]
last = data[-1]
good = True
for i in range(num - 2, num - 1 - last, -1):
	if data[i] != data[i + 1] - 1:
		good = False
		break
start = float('-inf')
end = num
if good:
	start = 1 - last
	end = num - last
for i in range(end):
	d = data[i]
	if d != 0:
		start = max(start, i - d + 3)
if good:
	if start != 1 - last:
		for i in range(end, num):
			d = data[i]
			if d != 0:
				start = max(start, i - d + 3)
print(start + num - 1)


n = int(input())
a = list(map(int, input().split()))
c = list(map(int, input().split()))
b = dict(zip(c, range(n)))
pos_one = b[1] if 1 in b else -1
if 1 in b:
	is_sorted = True
	for i in range(pos_one + 1, n):
		if c[i] != c[i - 1] + 1:
			is_sorted = False
			break
	if is_sorted:
		num_next = c[-1] + 1
		flag = True
		for i in range(n):
			if c[i] == 1:
				break
			if num_next in b and b[num_next] >= num_next - (c[-1] + 1):
				flag = False
				break
			num_next += 1
		if flag:
			print(pos_one)
			exit(0)
max_pos = pos_one + 1 if 1 in b else 0
for i in range(2, n + 1):
	if i in b and b[i] >= pos_one + i:
		max_pos = max(max_pos, b[i] - i + 2)
print(max_pos + n)


n = int(input())
a = [int(v) for v in input().split()]
b = [int(v) for v in input().split()]
c = 0
d = {}
f = 0
for j in range(n):
	d[a[j]] = 1
j = n - 1
while j > 0:
	if b[j] == b[j - 1] + 1 and b[j] != 1:
		j = j - 1
	else:
		break
if b[j] == 1 and j > 0:
	p = b[-1] + 1
	f = 1
elif b[j] == 1 and j == 0:
	f = 2
else:
	p = 1
if f == 0:
	w = 0
	for j in range(n):
		if p not in d:
			w = w + 1
		else:
			p = p + 1
		d[b[j]] = 1
	ans = w + n
	print(ans)
elif f == 2:
	print(0)
else:
	w = 0
	m = j - 1
	j = 0
	while j <= m:
		if p in d and f == 1:
			w = w + 1
			p = p + 1
		elif f == 0 and p not in d:
			w = w + 1
		elif f == 0 and p in d:
			p = p + 1
		else:
			f = 0
			w = w + 1
			p = 1
			m = n - 1
		d[b[j]] = 1
		j = j + 1
	if f == 1:
		print(w)
	else:
		print(w + n)


from collections import defaultdict as dd
import math
import sys
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))
n = nn()
hand = lm()
data = lm()
onespot = -1
for i in range(len(data)):
	if data[i] == 1:
		onespot = i
	if data[i] == 0:
		data[i] = float('+Inf')
if onespot == -1:
	print(n + max([0] + [i + 2 - data[i] for i in range(len(data))]))
elif all([data[i] == i + 1 - onespot for i in range(onespot, len(data))]) and all([data[i] >= data[-1] + i + 2 for i in range(onespot)]):
	print(onespot)
else:
	print(onespot + 1 + n + max([0] + [i + 1 - onespot - data[i] for i in range(onespot, len(data))]))


import sys
from collections import deque
from copy import *
from bisect import *
from math import gcd, ceil, sqrt
from itertools import permutations as prm, product
from random import *

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
sys.setrecursionlimit(10 ** 5)
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def string(s):
	return ''.join(s)

def fori(n):
	return [fi() for i in range(n)]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def gtc(tc, ans):
	print('Case #' + str(tc) + ':', ans)

def cil(n, m):
	return n // m + int(n % m > 0)

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j, n, m):
	return 0 <= i < n and 0 <= j < m

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = 1
while t > 0:
	t -= 1
	n = fi()
	a = li()
	b = li()
	if b == sorted(b) and max(a) == 0:
		print(0)
		continue
	p = {}
	if n == 1:
		print(1)
		continue
	for i in a:
		if i:
			p[i] = 1
	c = deque(b)
	ans = 0
	while True:
		if c[-1] == n or c[-1] + 1 not in p:
			break
		p[c[0]] = 1
		ans += 1
		c.popleft()
		c.append(c[-1] + 1)
	c = list(c)
	if c == sorted(c) and c.count(0) == 0:
		print(ans)
		continue
	d = {}
	for i in range(n):
		d[b[i]] = i
	maxi = 0
	r = [0] * (n + 1)
	for i in range(1, n + 1):
		if i not in d:
			r[i] = r[i - 1]
		else:
			r[i] = max(r[i - 1], d[i] + 2)
			maxi = max(maxi, r[i] + n - i)
	print(maxi)


n = int(input())
_ = input()
d = list(map(int, input().split()))
m = -n
z = -1
for (i, v) in enumerate(d):
	if not v:
		z = -1
		continue
	if z >= 0 and v != i - z + 1:
		z = -1
	elif v == 1:
		z = i
		y = m
	m = max(m, i - v + 2)
if z == 0:
	print(0)
elif z > 0 and y <= -n + z:
	print(z)
else:
	print(n + max(m, 0))