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: competition

Starter code:

Solutions:
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())



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
    # print("k =", j)
    while i < N:
        # print("i, B[i], N, k, i-N+k =", i, B[i], N, j, i-j)
        if B[i] != i - j:
            # print("DD")
            return 0
        i += 1
    # print("j =", j)
    for i in range(j+1):
        # print(B[i], N+1-j+i)
        if 0 < B[i] < N+1-j+i:
            # print("EE")
            return 0
    # print(j+1)
    return j + 2

c = chk()
if c > 0:
    print(c - 1)
else:
    # print([max(i-B[i]+2, 0) for i in range(N)])
    print(max([max(i-B[i]+2, 0) if B[i] else 0 for i in range(N)]) + N)


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)

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
    else:
        if(d[i]<i-br):
            continue
        else:
            b=False
            break
if(b):
    print(n-br)
else:
    print(n+mx)


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=(d[1]!=0)
for i in range(1,n+1):
    if(i<=br):
        if(d[i]-d[1]==i-1):
            continue
        else:
            b=False
            break
    else:
        if(d[i]<i-br):
            continue
        else:
            b=False
            break
if(b):
    print(n-br)
else:
    print(n+mx)


def index(l):
    for i in range(n):
        if l[i]>0:
            j=(i+1)-l[i]
            #print(i,l[i])
            l1.append(j)
    #print(l1)
    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)
    #print(index2)
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)]
        #print(li)
        if deck[index2:]==li:
            output=index2
        else:
            output=(max(l2)+n+1)
    else:
        for i in range(index2):
            if deck[i]>0:
                #print(index2-i,(n-deck[i])+2)
                if (index2-i)>=(n-deck[i])+2:
                    #print(index2-i,(n-deck[i])+2)
                    output=(l2[-1])
                else:
                    output=(max(l2)+n+1)
                    break
            #else:
                #output=11
else:
    if max(l2)<=(-1):
        output=(n)
    else:
        output=(max(l2)+n+1)
print(output)
    


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
else:
    if max(l2)<=(-1):
        output=(n)
    else:
        output=(max(l2)+n+1)
print(output)
    


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)
            return

ans = max([p[i] - i + 1 + n for i in range(1, n + 1)])
print(ans)


import io, os
#input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline

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)


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)
            return
ans = 0
for i in range(1, n + 1):
    ans = max(ans, s[i] - i + 1 + n)
print(ans)


n = int(input())
l1=list(map(int,input().split()))
l2= list(map(int,input().split()))
f=0
if 1 in l2:
    c=l2.index(1)
    d=1
    for i in range(c,n):
        if l2[i]!=d:
#            print(l2[i],d,i,c)
            break
        d+=1    
    else:

#        print("ll") 
        for k in range(c):
            if l2[k]!=0 and l2[k]-k<=d:
                break
        else:
            print(c)
#            print(d)
            f=1        
#    n-=d
if f==0:
    s=-2
    for i in range(n):
        if l2[i]!=0:
            s=max(s,i-l2[i])
    print(n+s+2)            

'''input
3
0 2 0
1 0 3
'''
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
		
	# print(mydict)
	zeroes = mydict[0]

	pile = deque(barr[:])


	# inserting the zeroes initially
	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()


		# updating the dictionary
		mydict[element] += 1
	
		# checking if it is zero
		if element == 0:
			zeroes += 1
	# print(mydict)

	# inserting the numbers
	start = find_start(barr)
	# print(start)
	for i in range(start, n + 1):
		# print(pile)
		if mydict[i] > 0:
			op[0] += 1
			pile.append(i)
			mydict[i] -= 1
			element = pile.popleft()
			mydict[element] += 1
		else:
			return False
	return True


# main starts
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)

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)

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(len(b)):
# 	print (b[i],end=" ")
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)	 				


# -*- coding:utf-8 -*-

"""

created by shuangquan.huang at 2/12/20

"""

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)]
    # print(B)
    for i, v in enumerate(B):
        p[v] = i + 1
    # print(p)
    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))


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))