Harryxun/stf_run huggingface dataset:
def li():
    return [int(x) for x in input().split()]

N, K = li()
P = [0] + li()
C = [0] + li()

def get_cycle(start_i):
    cycle = []
    i = start_i
    while True:
        i = P[i]
        if i == start_i:
            break
        cycle.append(i)
    cycle.append(start_i)
    return cycle

def get_cycle_scores(cycle):
    score = 0
    scores = [score]
    for i in cycle:
        score += C[i]
        scores.append(score)
    return scores

max_scores = []
for start_i in range(1,N+1):
    cycle = get_cycle(start_i)
    scores = get_cycle_scores(cycle)
    # 1cycle以下の場合は全て調べればよし
    if K <= len(cycle):
        max_score = max(scores[move] for move in range(1, K+1))
        max_scores.append(max_score)
        continue
    # 1サイクルの長さ
    cycle_len = len(cycle)
    # 1サイクルごとのスコア
    score_per_cycle = scores[cycle_len]
    # 1サイクルごとのスコアがマイナスならサイクルを回らないのがハイスコア
    if score_per_cycle < 0:
        continue
    # 1サイクルごとのスコアがプラスなら限界までサイクルを回るのがハイスコア
    # ラスト1サイクルのスコア
    scores_last_cycle = []
    # 最後の1cycleの値を調べる
    for move in range(K-cycle_len+1, K+1):
        # 何周したか
        loop_cnt = move // cycle_len
        # あまり
        rest_move = move % cycle_len
        # スコア = 1周ごとのスコア * 何周したか + あまり
        score = loop_cnt * score_per_cycle + scores[rest_move]
        scores_last_cycle.append(score)

    max_scores.append(max(scores_last_cycle))

print(max(max_scores))



### FIXED CODE ###

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

N, K = li()
P = [0] + li()
C = [0] + li()

def get_cycle(start_i):
    cycle = []
    i = start_i
    while True:
        i = P[i]
        if i == start_i:
            break
        cycle.append(i)
    cycle.append(start_i)
    return cycle

def get_cycle_scores(cycle):
    score = 0
    scores = [score]
    for i in cycle:
        score += C[i]
        scores.append(score)
    return scores

max_scores = []
for start_i in range(1,N+1):
    cycle = get_cycle(start_i)
    scores = get_cycle_scores(cycle)
    cycle_len = len(cycle)
    score_per_cycle = scores[cycle_len]
    # 1cycle以下の場合は全て調べればよし
    if K <= len(cycle):
        max_score = max(scores[move] for move in range(1, K+1))
        max_scores.append(max_score)
        continue
    # 1サイクルごとのスコアがマイナスならサイクルを回らないのがハイスコア
    if score_per_cycle <= 0:
        max_score = max(scores[move] for move in range(1, cycle_len+1))
        max_scores.append(max_score)
        continue
    # 1サイクルごとのスコアがプラスなら限界までサイクルを回るのがハイスコア
    # ラスト1cycleのスコアを調べる
    scores_last_cycle = []
    for move in range(K-cycle_len+1, K+1):
        # 何周したか
        loop_cnt = move // cycle_len
        # あまり
        rest_move = move % cycle_len
        # スコア = 1周ごとのスコア * 何周したか + あまり
        score = loop_cnt * score_per_cycle + scores[rest_move]
        scores_last_cycle.append(score)

    max_scores.append(max(scores_last_cycle))

print(max(max_scores))
from math import *
f=lambda:[*map(int,input().split())]
n,k=f()
a=f()
# Binary Search
def ok(x):
    c=0
    for i in a:
      c+=ceil(i/x)
    return c<=n+k
l,r=0,10**9+1
while r-l>10**-2:
  m=(l+r)/2
  if ok(m): r=m
  else: l=m
t=ceil(r)
print(t-ok(t-1))

### FIXED CODE ###

f=lambda:[*map(int,input().split())]
n,k=f()
a=f()
a=[i*100 for i in a]
# Binary Search
def ok(x):
    c=0
    for i in a:
      c+=0--i//x
    return c<=n+k
l,r=0,10**11+1
while r-l>1:
  m=l+r>>1
  if ok(m): r=m
  else: l=m
t=0--r//100
print(t-ok(t-1) if t>1 else t)
n, a, b = map(int, input().split())

s = int(n / (a + b))

if n % (a + b) == 0:
  print(a * s)
else:
  rem = n % (a + b)
  if rem <= a:
    ans = a * s + rem
  else:
    ans = a * s + (rem % a)
  print(ans)


### FIXED CODE ###

n, a, b = map(int, input().split())

s = int(n / (a + b))

if n % (a + b) == 0:
  print(a * s)
else:
  rem = n % (a + b)
  if rem <= a:
    ans = a * s + rem
  else:
    ans = a * s + a
  print(ans)
N=int(input())
import math

hoge=[[108]]

x=[3,5,7,11,13,17,19]
a=[pow(2,3)*i for i in x]

x=[2,5,7]
b=[pow(3,3)*i for i in x]

x=[5,7,11,13,17,19,23,29,31]
c=[2*3*i for i in x]

x=[7,11,13,17,19]
d=[2*5*i for i in x]

x=[11,13]
e=[2*7*i for i in x]

x=[7,11,13]
f=[3*5*i for i in x]

hoge.append(a)
hoge.append(b)
hoge.append(c)
hoge.append(d)
hoge.append(e)
hoge.append(f)


x=[]
for v in hoge:
  for item in v:
    x.append(item)

x.sort()
import numpy as np
X=N-x
XX=np.where(X < 0) 
print(XX[0])

### FIXED CODE ###

import numpy as np
N=int(input())
x=[105, 135, 165, 189, 195]

X=[N-x[i] for i in range(len(x))]
XX=np.where(np.array(X) < 0) 
if N<195:
  print(XX[0][0])
else:
  print(5)
N=int(input())
S=map(int,input().split())
if 'Y' in S :
  print('Four')
else:
  print('Three')


### FIXED CODE ###

N=int(input())
S=input()
if 'Y' in S :
  print('Four')
else:
  print('Three')
N, M = map(int, input().split())
line = []

for i in range(M):
    ab = list(map(int, input().split()))
    line.append(ab)

start = 1
end = N
first = set()
second = set()

for l in line:
    if end in l:
        second.add(l[0])
        second.add(l[1])
    elif start in l:
        first.add(l[1])
        first.add(l[0])
first.remove(start)
second.remove(end)

for f in first:
    if f in second:
        print('POSSIBLE')
        exit()

print('IMPOSSIBLE')

### FIXED CODE ###

N, M = map(int, input().split())
line = []

for i in range(M):
    ab = list(map(int, input().split()))
    line.append(ab)

start = 1
end = N
first = set()
second = set()

for l in line:
    if end in l:
        second.add(l[0])
        second.add(l[1])
    elif start in l:
        first.add(l[1])
        first.add(l[0])


for f in first:
    if f in second:
        print('POSSIBLE')
        exit()

print('IMPOSSIBLE')
import math

def main():
    while 1:
        try:
            n = input().split()
            a = int(n[0])
            b = int(n[1])
            ans = int(math.log10(a+b)+1)
            print(ans)
        except IndexError:
            break

if __name__ == "__main__":
    main()

### FIXED CODE ###

import math

def main():
    data = []
    while 1:
        try:
            n = input().split()
            a = int(n[0])
            b = int(n[1])
            ans = int(math.log10(a+b)+1)
            data.append(ans)
        except EOFError:
            break

    for i in data:
        print(i)

if __name__ == "__main__":
    main()
#######################################
p = 2019

l,r = map(int, input().split())
n = r-l+1

if n<2*p:
  tmps = [(i*j)%p for i in range(l,r) for j in range(i+1,r+1)]
  mini = min(tmps)

else:
  lis = list(range(l,r+1))[:p+1]
  lis = list(set([li%p for li in lis]))
  tmps = [(i*j)%p for i in lis for j in lis]
  mini = min(tmps)
  #mini = 0

print(mini)


### FIXED CODE ###

#######################################
p = 2019

l,r = map(int, input().split())
n = r-l+1

if n<2*p:
  tmps = [(i*j)%p for i in range(l,r) for j in range(i+1,r+1)]
  mini = min(tmps)

else:
  #lis = list(range(l,r+1))[:p+1]
  #lis = list(set([li%p for li in lis]))
  #tmps = [(i*j)%p for i in lis for j in lis]
  #mini = min(tmps)
  mini = 0

print(mini)
from collections import Counter
N = int(input())
A = []
for i in range(N):
  A.append(input())
C= Counter(A)
B = 0
for v in c.values():
  B = max(B,v)
ans = []
for k, v in c.items()
  if v == B:
    ans.append(k)
ans.sort()
print(*ans,sep='\n')

### FIXED CODE ###

from collections import Counter
N = int(input())
A = []
for i in range(N):
  A.append(input())
C= Counter(A)
B = 0
for v in C.values():
  B = max(B,v)
ans = []
for k, v in C.items():
  if v == B:
    ans.append(k)
ans.sort()
print(*ans,sep='\n')
ans=10**8
h,w,K=map(int,input().split())
s=[]
import copy
def two(x):
	return "0"*(h-1-len(bin(x)[2:]))+bin(x)[2:]
for i in range(h):
	s.append(input())
for i in range(2**(h-1)):
	r=two(i)
	pre_ans=r.count("1")
	d=[0]*(pre_ans+2)
	cnt=0
	for j in range(w):
		now=0
		for k in range(h):
			if s[k][j]=="1":
				d[now]+=1
			if k!=h-1:
				if r[k]=="1":
					now+=1
		if max(d)>K:
			e=[]
			for x in range(len(dd)):
				e.append(d[x]-dd[x])
			d=copy.copy(e)
			cnt+=1
		if max(d)>K:
			pre_ans=10**7
			break
		dd=copy.copy(d)
	pre_ans+=cnt
	ans=min(ans,pre_ans)
print(ans)

### FIXED CODE ###

ans=10**8
h,w,K=map(int,input().split())
s=[]
import copy
def two(x):
	return "0"*(h-1-len(bin(x)[2:]))+bin(x)[2:]
for i in range(h):
	s.append(input())
for i in range(2**(h-1)):
	r=two(i)
	pre_ans=r.count("1")
	d=[0]*(pre_ans+1)
	dd=copy.copy(d)
	cnt=0
	for j in range(w):
		now=0
		for k in range(h):
			if s[k][j]=="1":
				d[now]+=1
			if k!=h-1:
				if r[k]=="1":
					now+=1
		if max(d)>K:
			e=[]
			for x in range(len(d)):
				e.append(d[x]-dd[x])
			d=copy.copy(e)
			cnt+=1
		if max(d)>K:
			cnt=10**7
		dd=copy.copy(d)
	pre_ans+=cnt
	ans=min(ans,pre_ans)
print(ans)
for _ in range(int(input())):
    ax, ay, bx, by, cx, cy, dx, dy = map(float, input().split())
    print(['NO','YES'][(by-ay)/(bx-ax)==(dy-cy)/(dx-cx)])

### FIXED CODE ###

for _ in range(int(input())):
    ax, ay, bx, by, cx, cy, dx, dy = map(float, input().split())
    print(['NO','YES'][abs((ay-by)*(dx-cx)-(ax-bx)*(dy-cy))<1e-10])
A, B = map(int, input().split())
S = input() 
if S[A] == "-" and (S[0:A]+S[A+1:B]).isdigit():
    print("Yes")
else:
    print("No")

### FIXED CODE ###

M = int(input())
print(48 - M)
import numpy as np
n, m = map(int, input().split())
A = np.array(list((map(int, input().split()))))
for _ in range(m):
    A.sort()
    A[-1] = A[-1] // 2
print(sum(A))

### FIXED CODE ###

import heapq
import math

n, m = map(int, input().split())
A = list(map(lambda x: -x, map(int, input().split())))
heapq.heapify(A)
for _ in range(m):
    tmp = heapq.heappop(A)
    tmp = math.ceil(tmp / 2)
    heapq.heappush(A, tmp)
print(-sum(A))
N,Y = map(int,input().split())
for i in range(N+1):
    if (Y - 1000*N -9000*i) % 4000 == 0:
        b = (Y - 1000*N -9000*i) // 4000 
        c = N - i - b
        if b >= 0 and c >= 0:
            print(a,b,c)
            exit()
print("-1 -1 -1")

### FIXED CODE ###

l = list(map(int,input().split()))
N = l[0]
Y = l[1]

l = list()

for i in range(N+1):
    a = i
    if (Y - 1000*N -9000*a) % 4000 == 0:
        b = (Y - 1000*N -9000*a) // 4000 
        c = N - a - b
        if a >= 0 and b >= 0 and c >= 0:
            l.append([a,b,c])
            break

if len(l) ==0:
    print("-1 -1 -1")
else:
    ans = l[0]
    print(ans[0],ans[1],ans[2])
age = int(input());
if age == 1:
  print(1);
else:
  b, c = map(int, input().split());
  print(b + c);


### FIXED CODE ###

age = int(input());
if age == 1:
  print("Hello World");
else:
  b = int(input());
  c = int(input());
  print(b + c);
import heapq

def heapsort(iterable):
    h = []
    for value in iterable:
        heapq.heappush(h, value)
    return [heapq.heappop(h) for i in range(len(h))]

N, K = map(int, input().split(" "))
H = list(map(int, input().split(" ")))

heapsort(H)
# heapq.heapify(H)
# H.sort(reverse=True)

if K >= N:
    print(0)
else:
    for i in range(K):
        del H[i]
    print(sum(H))


### FIXED CODE ###

N, K = map(int, input().split())
H = list(map(int, input().split()))

H.sort(reverse=True)

if K >= N:
    print(0)
else:
    print(sum(H[K:]))
import math
x1, y1, x2, y2 = map(float, input().split())  
 print(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2))


### FIXED CODE ###

from math import sqrt
x1, y1, x2, y2 = map(float, input().split())  
 
print(sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2))
, m, p = map(int, input().split())
abcs = [list(map(int, input().split())) for _ in range(m)]
distance = [-100000 * n] * (n + 1)

distance[1] = 0
for _ in range(n + 1):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p

ret = distance[n]
for _ in range(n + 1):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p

print(-1 if ret != distance[n] else max(ret, 0))

### FIXED CODE ###

n, m, p = map(int, input().split())
abcs = [list(map(int, input().split())) for _ in range(m)]
distance = [-1000000 * n] * (n + 1)

distance[1] = 0
reachable1 = set([1])
reachableN = set([n])
for _ in range(n + 1):
    for a, b, c in abcs:
        if distance[b] < distance[a] + c - p:
            distance[b] = distance[a] + c - p
        # 枝刈りが必要                                                                                                                                                                                                                
        if a in reachable1:
            reachable1.add(b)
        if b in reachableN:
            reachableN.add(a)

for a, b, c in abcs:
    if (distance[b] < distance[a] + c - p) and (a in reachable1) and (b in reachableN):
        print(-1)
        exit(0)

print(max(distance[n], 0))
def main():
  inf = float("INF")
  a,b,q = map(int, input().split())
  s = [None] * (a + 3)
  s[0:2] = [-inf,-inf]
  s[-1] = inf
  t = [None] * (b + 3)
  t[0:2] = [-inf,-inf]
  t[-1] = inf
  for i in range(a):
      s[i+2] = int(input())
  for i in range(b):
      t[i+2] = int(input())

  from bisect import bisect_left

  dp0 = [None] * (a+3)
  dp1 = [None] * (b+3)

  for _ in range(q):
    ans = inf
    x = int(input())
    sr = bisect_right(s, x)
    tr = bisect_right(t, x)    
    for si in (sr, sr-1):
      for ti in (tr, tr-1):
        ans = min(ans, abs(x-s[si]) + abs(s[si]-t[ti]), abs(x-t[ti]) + abs(s[si]-t[ti]))
    print(ans)
  
if __name__ == "__main__":
  main()


### FIXED CODE ###

#A

"""
s = input().split("/")
if int(s[1]) <= 4:
    print("Heisei")
else:
    print("TBD")
"""
#B
"""
n = int(input())
ans = 0
for i in range(n):
    a = input().split()
    if a[1] == "JPY":
        ans += int(a[0])
    else:
        ans +=  float(a[0]) * 380000
print(ans)
"""

#C
"""
n, a, b, c = map(int, input().split())
abc = [a,b,c]
x = [int(input()) for i in range(n)]
x.sort()
ans = []
flg = 0
for i in x:
    if i in abc:
        ans.append(i)
        flg = 1
if flg:
    for i in ans:
        abc.remove(i)
        x.remove(i)

"""
#D

a, b, q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
s.sort()
t.sort()

import bisect


for i in range(q):
    x = int(input())
    s_l = bisect.bisect_left(s, x) - 1
    t_l = bisect.bisect_left(t, x) - 1
    s_r = s_l + 1
    t_r = t_l + 1
    if s_l == -1:
        s_l = 0
    if t_l == -1:
        t_l = 0
    if s_l == a :
        s_l = a - 1
        s_r = a - 1
    if s_r == a:
        s_r = a - 1
    if t_l == b:
        t_l = b - 1
        t_r = b - 1
    if t_r == b:
        t_r = b - 1
    ans = min(abs(t[t_l] - s[s_r]) + min(abs(t[t_l] - x), abs(s[s_r] - x)),
    abs(s[s_l] - t[t_r]) + min(abs(s[s_l] - x), abs(t[t_r] - x)),
    abs(s[s_l] - t[t_l]) + min(abs(s[s_l] - x), abs(t[t_l] - x)),
    abs(s[s_r] - t[t_r]) + min(abs(s[s_r] - x), abs(t[t_r] - x)),
    )
    
    print(ans)
N = int(input()
A = map(int,input().split())

flg = True
cnt = 0

while flg = True:
    for i in range(N):
        if A[i]%2 = 1 then:
            flg = False
            break
    cnt += 1
    A = [a/2 for a in A]

print(cnt)

### FIXED CODE ###

N = int(input())
A = [int(i) for i in input().split()]

cnt = 0
flg = True

while flg == True:
    for i in range(N):
        if A[i]%2 == 1:
            flg = False
            break
    if flg == True:
        cnt += 1
        A = [a/2 for a in A]

print(cnt)
import sys
readline = sys.stdin.readline
write = sys.stdout.write

def gindex(l, r):
    L = l + N0; R = r + N0
    lm = (L // (L & -L)) >> 1
    rm = (R // (R & -R)) >> 1
    while L < R:
        if R <= rm:
            yield R
        if L <= lm:
            yield L
        L >>= 1; R >>= 1
    while L:
        yield L
        L >>= 1
 
def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if v is None:
            continue
        lazy[2*i-1] = data[2*i-1] = lazy[2*i] = data[2*i] = v
        lazy[i-1] = None
 
def update(l, r, x):
    *ids, = gindex(l, r)
    propagates(*ids)
 
    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            lazy[R-1] = data[R-1] = x
        if L & 1:
            lazy[L-1] = data[L-1] = x
            L += 1
        L >>= 1; R >>= 1
    for i in ids:
        data[i-1] = min(data[2*i-1], data[2*i])
 
def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r
 
    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = min(s, data[R-1])
        if L & 1:
            s = min(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s


n,Q=map(int,input().split())

N=10000000
INF = 2**31-1
 
LV = (N-1).bit_length()
N0 = 2**LV
data = [INF]*(2*N0)
lazy = [None]*(2*N0)
 

a=[list(map(int,input().split())) for i in range(n)]
a.sort(key=lambda x:x[2],reverse=True)

a=[ [ max(0,a[i][0]-a[i][2]) , max(0,a[i][1]-a[i][2]) , a[i][2] ] for i in range(n)]

for i in range(n):
    l,r,x=a[i][0],a[i][1],a[i][2]
    update(l,r,x)

for i in range(Q):
    dp=int(input())
    ans=query(dp,dp+1)
    print(-1 if ans==INF else ans)

### FIXED CODE ###

def main():
    from heapq import heappush, heappop
    import sys
    sys.setrecursionlimit(10 ** 7)

    n,q=map(int,input().split())

    event=[]
    for _ in range(n):
        s,t,x=map(int,input().split())
        event.append((s-x,1,x))
        event.append((t-x,-1,x))

    for i in range(q):
        event.append((int(input()),2,i))
    event.sort()

    ans=[0]*q

    stop=set()
    hq=[]
    for t,op,x in event:
        if op==1:
            heappush(hq,x)
            stop.add(x)
        elif op==-1:
            stop.remove(x)

        else:
            while hq and hq[0] not in stop:
                heappop(hq) 
            ans[x]=hq[0] if hq else -1
        
    for x in ans:
        print(x)

main()
import re
if len(re.finall('[A-Z]', input()) == 0:
       print('a')
else:
       print('A')

### FIXED CODE ###

n = input()
if n.isupper() == True:
  print('A')
else:
  print('a')
import collections

n = int(input())
v = list(map(int, input().split()))

even_num=[v[i] for i in range(0,n,2)]
odd_num=[v[i] for i in range(1,n,2)]

count_even=collections.Counter(even_num).most_common()
count_odd=collections.Counter(odd_num).most_common()

count_even.append((0,0))
count_odd.append((0,0))

if count_even[0][0]!=count_odd[0][0]:
    print(n-count_even[0][1]-count_odd[0][1])
else:
    odd_even=count_odd[0][1]+count_even[1][1]
    even_odd=count_even[0][1]+count_odd[1][1]
    if odd_even>even_odd:
        print(n-odd_num)
    else:
        print(n-even_odd)




### FIXED CODE ###

import collections

n = int(input())
v = list(map(int, input().split()))

even_num=[v[i] for i in range(0,n,2)]
odd_num=[v[i] for i in range(1,n,2)]

count_even=collections.Counter(even_num).most_common()
count_odd=collections.Counter(odd_num).most_common()

#count_even.append((0,0))
#count_odd.append((0,0))

if count_even[0][0] != count_odd[0][0]:
    print(n-count_even[0][1]-count_odd[0][1])
elif len(count_even)==1 and len(count_odd)==1:
    print(n//2)
elif len(count_even)==1:
    print(n-count_odd[1][1])
elif len(count_odd)==1:
    print(n-count_even[1][1])
else:
    odd_even=count_odd[0][1]+count_even[1][1]
    even_odd=count_even[0][1]+count_odd[1][1]
    if odd_even>even_odd:
        print(n-odd_even)
    else:
        print(n-even_odd)
a, b, c = map(int, input().split())
array = []
for i in range(c):
	array.append(input())
 
point = [b] * a
 
for i in range(c):
	for j in range(a):
		if j+1 != int(array[c]):
			point[j] = point[j] -1
            
for i in range(a):
	if point[i] > 0:
		print('Yes')
	else:
		print('No')

### FIXED CODE ###

a, b, c = map(int, input().split())
array = []
for i in range(c):
	array.append(int(input()))

point = [b] * a
 
for i in range(c):
	point[array[i]-1] = point[array[i]-1] + 1
            
for i in range(len(point)):
	if (point[i]-c) > 0 :
		print('Yes')
	else:
		print('No')
a,b,k=map(int, input().split())
if b-a+1>2k:
  for i in range(a,a+k):
    print(i)
  for i in range(b-k+1,b+1):
    print(i)
else:
  for i in range(a,b+1):
    print(i)

### FIXED CODE ###

a,b,k=map(int, input().split())
if b-a+1>2*k:
  for i in range(a,a+k):
    print(i)
  for i in range(b-k+1,b+1):
    print(i)
else:
  for i in range(a,b+1):
    print(i)
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var):
    print(*var, file=sys.stderr)

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
# from collections import defaultdict as dd, Counter
# from bisect import bisect_left as bl, bisect_right as br

n, = I()
a = I()
pos = [0]*n
for i in range(n):
    pos[a[i]-1] = i
ans = 1
for i in range(1, n):
    if pos[i] > pos[i-1]:
        ans += 1
    else:
        break
if 1 in a:
    print(ans)
else:
    print(-1)

### FIXED CODE ###

import sys
def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(var):
	print(var, file=sys.stderr)

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
# from collections import defaultdict as dd, Counter
# from bisect import bisect_left as bl, bisect_right as br

a = []
a.append(I()[0])
a.append(I()[0])
for i in range(1, 4):
	if i not in a:
		print(i)
		break
N = int(input())
li = list(map(int, input().rstrip().split()))

i = 0
counter = 0

while li[i] % 2 == 0:
  counter += 1
  i += 1
else:
  print(counter / N)

### FIXED CODE ###

N = int(input())
li = list(map(int, input().rstrip().split()))

counter = 0
i = 0

while li[i] % 2 == 0:
    counter += 1
    li[i] = li[i] / 2
    if i == N-1:
      i = 0
    else:
      i += 1
print(int(counter/N))
import math

N = int(input())

print(2*N//math.gcd(2, N))


### FIXED CODE ###



def solve():
    N = int(input())

    if N % 2 == 0:
        print(N)
    else:
        print(N*2)


# Solve
if __name__ == "__main__":
    solve()
n,k=map(int,input().split())
k-=1
x=list(map(int,input().split()))
print(min(min(abs(x[i],x[i+k]))+abs(x[i]-x[i+k])for i in range(n-k)))

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=abs(a[0])*2+abs(a[-1])*2#とりあえず最大
for i in range(n-k+1):
  x,y=a[i],a[i+k-1]
  if x*y<0:m=min(abs(x),y)*2+max(abs(x),y)
  else:m=max(abs(x),abs(y))
  ans=min(ans,m)
print(ans)