Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from collections import defaultdict
N = int(input())
arr = list(map(int, input().split()))
puts = []
dd = defaultdict(int)
 
def make_divisors(n):
    divisors = []
    # Nの最大の約数は√N
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    # divisors.sort()
    return divisors
 
for i in range(1,N+1)[::-1]:
    if N//2 < i and arr[i-1] == 1:
        puts.append(i)
        ds = make_divisors(i)
        for j in ds:
            dd[j] += 1
    else:
        # c = len([p for p in puts if p % i == 0])
        c = dd[i]
        if (c % 2 == 0 and arr[i-1] == 1) or (c % 2 == 1 and arr[i-1] == 0):
            puts.append(i)
            ds = make_divisors(i)
            for j in ds:
                dd[j] += 1
 
print(len(puts))
if len(puts):
    print(" ".join(map(str, sorted(puts, reverse=True))))

### FIXED CODE ###

from collections import defaultdict
N = int(input())
arr = list(map(int, input().split()))
puts = []
dd = defaultdict(int)

def make_divisors(n):
    divisors = []
    # Nの最大の約数は√N
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    # divisors.sort()
    return divisors

for i in range(1,N+1)[::-1]:
    if N//2 < i and arr[i-1] == 1:
        puts.append(i)
        ds = make_divisors(i)
        for j in ds:
            dd[j] += 1
    else:
        # c = len([p for p in puts if p % i == 0])
        c = dd[i]
        if (c % 2 == 0 and arr[i-1] == 1) or (c % 2 == 1 and arr[i-1] == 0):
            puts.append(i)
            ds = make_divisors(i)
            for j in ds:
                dd[j] += 1

print(len(puts))
if len(puts):
    print(" ".join(map(str, sorted(puts, reverse=True))))
### BUGGY CODE ###

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


if __name__ == "__main__":
    n = int(input())
    a_list = [int(input()) for _ in range(n)]

    color_list = [-1] * n
    color_num = 0

    for a in a_list:
        # 増分が一番小さいものを選ぶ
        for index, color in enumerate(color_list[:color_num + 1]):
            if color < a:
                color_list[index] = a
                if index == color_num:
                    color_num = color_num + 1
                break
        color_list[:color_num + 1] = sorted(color_list[:color_num + 1], reverse=True)
        # print(color_list)
    # print()
    # print(color_list)
    print(color_num)

# a1から順番に塗っていく
# 色ごとに、今まで塗った最大値を記録する
# 既存の色でぬれる場合は、なるべく最大値の増分が少なくなるように色を選ぶ
# 既存の色でぬれない場合は新しい色を作って最大値を記録する


### FIXED CODE ###

from bisect import bisect_left


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


if __name__ == "__main__":
    n = int(input())
    a_list = [int(input()) for _ in range(n)]

    color_list = [-1] * n
    color_num = 0

    for a in a_list:
        # ２分探索
        index = bisect_left(color_list, a)
        # print(index)
        color_list[index - 1] = a
        if n - index == color_num:
            color_num = color_num + 1
        # print(color_list)
    # print()
    # print(color_list)
    print(color_num)

# a1から順番に塗っていく
# 色ごとに、今まで塗った最大値を記録する
# 既存の色でぬれる場合は、なるべく最大値の増分が少なくなるように色を選ぶ
# 既存の色でぬれない場合は新しい色を作って最大値を記録する
### BUGGY CODE ###

n,q = [int(x) for x in input().split()]
s = input()
for _ in range(q):
    l,r = [int(x) for x in input().split()]
    print(s[l-1:r].count("AC"))

### FIXED CODE ###

n,q = [int(x) for x in input().split()]
s = input()

t = [0]*(n+1)
for i in range(n):
    t[i+1] = t[i] + (1 if s[i:i+2] == "AC" else 0)

for _ in range(q):
    l,r = [int(x) for x in input().split()]
    print(t[r-1]-t[l-1])
### BUGGY CODE ###

n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    print(s[l-1:r].count('AC'))


### FIXED CODE ###

n, q = map(int, input().split())
s = input()
ans = [0 for _ in range(n)]
count = 0
for i in range(1, n):
    a = s[i-1]
    b = s[i]
    if a == 'A' and b == 'C':
        count += 1
    ans[i] = count

for i in range(q):
    l, r = map(int, input().split())
    print(ans[r-1]-ans[l-1])
### BUGGY CODE ###

import copy
X=int(input())
ava={0}
ava2={0}

for i in range(1,10**9):
  ava=copy.deepcopy(ava2)
  #print(i,ava,ava2)
  if X not in ava:
    for item in ava: 
      ava2.add(item+i)
      ava2.add(item-i)
  else:
    print(i-1)
    break

### FIXED CODE ###

X=int(input())

for i in range(10**9):
  if (i*(i+1))//2>=X:
    print(i)
    break
### BUGGY CODE ###

import heapq


def main():
    n, m = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n)]

    AB.sort(key=lambda x: x[0])
    H = []
    ans = 0
    mi = 1
    for a, b in AB:
        while a > mi:
            if len(H) > 0:
                heapq.heapify(H)
                ans += -heapq.heappop(H)

            mi += 1
            if mi > m:
                break

        if a > m:
            break

        H.append(-b)

    while mi <= m and len(H) > 0:
        heapq.heapify(H)
        ans += -heapq.heappop(H)
        mi += 1

    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import heapq


def main():
    n, m = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n)]

    AB.sort(key=lambda x: x[0])
    H = []
    ans = 0
    mi = 1
    for a, b in AB:
        while a > mi:
            if len(H) > 0:
                ans += -heapq.heappop(H)

            mi += 1
            if mi > m:
                break

        if a > m:
            break

        heapq.heappush(H, -b)

    while mi <= m and len(H) > 0:
        ans += -heapq.heappop(H)
        mi += 1

    print(ans)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0

    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True

        for j in range(n):
            d[j] = min(d[j],d[v]+cost[v][j])
    return d

n,x,y = map(int,input().split())

#cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
cost = [[float("inf") for i in range(n)] for i in range(n)]

for i in range(n-1):
    cost[i][i+1] = 1
    cost[i+1][i] = 1
cost[x-1][y-1] = 1
cost[y-1][x-1] = 1

ans = [0 for _ in range(n)]
for i in range(n):
    #nは頂点数、wは辺の数
    res = dijkstra(i,n,n,cost)
    for j in range(i+1,n):
        ans[res[j]] += 1

for i in range(1,n):
    print(ans[i])


### FIXED CODE ###

from scipy.sparse.csgraph import dijkstra
import sys
def main():
	input = sys.stdin.readline

	n,x,y = map(int,input().split())

	route = [[0 for _ in range(n)]for _ in range(n)]
	for i in range(n-1):
		route[i][i+1] = 1

	route[x-1][y-1] = 1
	route[y-1][x-1] = 1
	route = tuple(route)
	#res = shortest_path(route, unweighted=False,directed=False)
	res = tuple(dijkstra(route, directed=False))

	ans = [0 for _ in range(n)]
	for i in range(n):
		for j in range(i,n):
			ans[int(res[i][j])] += 1

	for i in range(1,n):
		print(ans[i])
if __name__ == '__main__':
    main()
### BUGGY CODE ###

N, M = map(int, input().split())
lst = [tuple(map(int, input().split())) for _ in range(M)]
lst.sort(key=lambda x: x[1])
bridge = [False for _ in range(N - 1)]
for war in lst:
    if True not in bridge[war[0] - 1:war[1] - 1]:
        bridge[war[1] - 2] = True
print(bridge.count(True))

### FIXED CODE ###

N, M = map(int, input().split())
lst = [tuple(map(int, input().split())) for _ in range(M)]
lst.sort(key=lambda x: x[1])
bridge = []
for war in lst:
    if bridge and war[0] <= bridge[-1]:
        continue
    bridge.append(war[1] - 1)
print(len(bridge))
### BUGGY CODE ###

n = int(input())
A = list(map(int, input().split()))
B = list()
ans = 0
for i in range(n - 1):
    for j in range(n - i - 1):
        b = A[i] ^ A[j + i + 1]
        if ans + b < 1000000007:
            ans += b
        else:
            ans = (ans + b) % 1000000007
print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
p = [pow(2, i, mod) for i in range(60)]
ans = 0
for i in range(60):
    c = 0
    for j in a:
        if (j >> i) & 1:
            c += 1
    ans += (c * (n - c) * p[i]) % mod
    ans %= mod
print(ans)
### BUGGY CODE ###

import sys

a = []
b = []
for line in sys.stdin:
    a.append(int(line))
    b.append(int(line))

del a[0]
del a[len(a)-1]
del b[0]
del b[0]

maxlist = []
for i in range(len(a)):
    list = []
    for j in range(len(b)):
        if j >= i:
            list.append(b[j]-a[i])
    maxlist.append(max(list))
print(max(maxlist))




### FIXED CODE ###

import sys

a = []
b = []
for line in sys.stdin:
    a.append(int(line))
    b.append(int(line))

del a[0]
del a[len(a)-1]
del b[0]
del b[0]

maxv = -1000000000
minv = a[0]
for i in range(len(a)):
    maxv = max(maxv,b[i]-minv)
    minv = min(minv,b[i])
print(maxv)
### BUGGY CODE ###

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

total = 1
for a in li:
  total *= a
  
if 10 ** 18 < total:
  total = -1
  
print(total)

### FIXED CODE ###

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

total = 1
li.sort()
for a in li:
    if a == 0:
        total = 0
        break
    total *= a
    if 10 ** 18 < total:
        total = -1
        break
print(total)
### BUGGY CODE ###

from operator import mul
from functools import reduce
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
MOD = 10 ** 9 + 7

ans = 0

def com(n, r):
  r = min(r, n - r)
  numer = reduce(mul, range(n, n - r, -1), 1)
  denom = reduce(mul, range(1, r + 1), 1)
  return numer // denom

start = com(N - 1, K - 1)
x = N - 1

for i in range(N):
  if N - 1 - i >= K - 1:
    if ((i > 0) and (N - 1 - i > 0)):
      #print(start, x - i -K)
      start = start * (x - i - K + 2) // (x - i + 1)
      #print(start)
    ans += (A[- i - 1] - A[i]) * start
    ans = ans % MOD
    #print(ans, start,x - i)
    
print(ans % MOD)

### FIXED CODE ###

N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
MOD = 10 ** 9 + 7

ans = 0

def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

N2 = 10 ** 6  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
for i in range(2, N2 + 1):
    fact.append((fact[-1] * i) % MOD)
    inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
    factinv.append((factinv[-1] * inv[-1]) % MOD)

#print(cmb(n, r, p))
for i in range(len(A)):
  if N - 1 - i >= K - 1:
    ans += (A[- i - 1] - A[i]) * cmb(N - 1 - i, K - 1, MOD)
    ans = ans % MOD
    
    
print(ans % MOD)
### BUGGY CODE ###

n, a, b = map(int, input().split())
l = list('')
while len(l) <= n:
    for i in range(a):
        l.append('blue')
    for i in range(b):
        l.append('red')
count = 0
for i in range(n):
    if l[i] == 'blue':
        count += 1
print(count)

### FIXED CODE ###

n, a, b = map(int, input().split())
mod = int(n % (a + b))
cycle = n // (a + b)
if mod == 0:
    print(a * cycle)
elif mod > a:
    print(a * cycle + a)
else: 
    print(a * cycle + mod)
### BUGGY CODE ###

def prime_factors(n):       # 戻り値はiterable type
    i = 2
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            yield i             # whileループを進めるごとにイテレータに値を貯め続ける
    if n > 1:
        yield n

def min2(x,y):
    return x if x < y else y

from itertools import combinations

N = int(input())
P = list(prime_factors(N))
L = len(P) + 1
res =float("inf")

for k in range(1, L//2+1):
    for a in combinations(prime_factors(N), k):
        q = 1
        for p in a:
            q *= p
        res = min2((q - 1) + (N // q - 1), res)

print(res)

### FIXED CODE ###

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def min2(x,y):
    return x if x < y else y

N = int(input())
P = make_divisors(N)
L = len(P) + 1
res =float("inf")

for p in P:
    res = min2((p - 1) + (N // p - 1), res)

print(res)
### BUGGY CODE ###

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

array = []
for i in range(m):
    tmp = sorted([int(x) for x in input().split()])
    array.append(tmp)

determination = []
for j in range(len(array)):
    if array[j][0] == 1:
        determination.append(array[j][1])

res = False
for k in determination:
    for l in array:
        if k in l and n in l:
            res = True
            break
    if res:
        break
print("POSSIBLE" if res else "IMPOSSIBLE")

### FIXED CODE ###

n, m = map(int, input().split())
ships = [[int(x) for x in input().split()] for x in range(m)]

judge = False
relay = []
goal = []

for ship in ships:
    if n in ship:
        goal.append(ship[0])
    elif 1 in ship:
        relay.append(ship[1])

res = False
if len(goal) == 0 or len(relay) == 0:
    print("IMPOSSIBLE")

else:
    print("IMPOSSIBLE" if not set(relay) & set(goal) else "POSSIBLE")
### BUGGY CODE ###

Q = int(input())
Y = []
D = []
S = [0] * Q
s = 0
for i in range(Q):
    a = list(map(int, input().split()))
    Y.append(a)
    if a[0] == 1:
        D.append(a[1])
        s += a[2]
    S[i] = s

D = sorted(list(set(D)))
INV = {}
for i in range(len(D)):
    INV[D[i]] = i

N = 17
X = [0] * (2**(N+1)-1)
C = [0] * (2**(N+1)-1)

def add(j, x):
    i = 2**N + j - 1
    while i >= 0:
        X[i] += x
        C[i] += 1
        i = (i-1) // 2

def rangeof(i):
    s = (len(bin(i+1))-3)
    l = ((i+1) - (1<<s)) * (1<<N-s)
    r = l + (1<<N-s)
    return (l, r)

def rangesum(a, b, i=0):
    l, r = rangeof(i)
    if l >= b or r <= a:
        return 0
    if a <= l and r <= b:
        return X[i]
    return rangesum(a, b, 2*i+1) + rangesum(a, b, 2*i+2)
def rangecnt(a, b, i=0):
    l, r = rangeof(i)
    if l >= b or r <= a:
        return 0
    if a <= l and r <= b:
        return C[i]
    return rangecnt(a, b, 2*i+1) + rangecnt(a, b, 2*i+2)

c = 0
su = 0
CC = [0] * (2**N)
for i in range(Q):
    y = Y[i]
    if y[0] == 1:
        add(INV[y[1]], y[1])
        CC[INV[y[1]]] += 1
        c += 1
        su += y[1]
    else:
        l, r = 0, 2**N
        while True:
            m = (l+r)//2
            rc = rangecnt(0, m)
            if rc >= (c+1)//2:
                r = m
            elif rc + CC[m] <= (c-1)//2:
                l = m
            else:
                break
        rs = rangesum(0, m)
        print(D[m], D[m]*rc-rs+(su-rs)-(c-rc)*D[m]+S[i])


### FIXED CODE ###

import sys
input = sys.stdin.readline
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        ans += a[2]
        if len(L) == 0 or a[1] <= -L[0]:
            heappush(L, -a[1])
            ans += -L[0] - a[1]
        else:
            heappush(R, a[1])
            ans += a[1] + L[0]
        if len(R) > len(L):
            ans -= R[0] + L[0]
            heappush(L, -heappop(R))
        elif len(L) - len(R) >= 2:
            heappush(R, -heappop(L))
    else:
        print(-L[0], ans)
### BUGGY CODE ###

def check(s_i, n_i, count, i_set):
    for i in range(9):
        i_set_tmp = set(i_set)
        if i not in i_set_tmp:
            continue
        s_tmp = s_i
        n_tmp = n_i
        if s_tmp == 0 and n_tmp == 0:
            return count + 1
        elif s_tmp < 0 or n_tmp == 0:
            break
        else:
            s_tmp -= i*n_tmp
            n_tmp -= 1
            i_set_tmp.remove(i)
            count = check(s_tmp, n_tmp, count, i_set_tmp)
    return count

while True:
    try:
        n, s = map(int, input().split())
        print(check(s, n, 0, set([0,1,2,3,4,5,6,7,8,9])))
    except:
        break



### FIXED CODE ###

s_max = [0]
for i in range(1,11):
    s = 0
    for j in range(i):
        s += (10 - j - 1)*(i - j)
    s_max.append(s)

def check(u, r, s, m):
    if r == 0:
        return s == 0
    n = 0
    for i in range(10):
        n *= 2
        if u[i]:
            n += 1
    if (n, r, s) in m:
        return m[(n, r, s)]
    if s < 0 or s > s_max[r]:
        return 0

    ans = 0
    for i in range(10):
        if not u[i]:
            u[i] = True
            ans += check(u, r - 1, s - i*r, m)
            u[i] = False
    m[(n, r, s)] = ans
    return m[(n, r, s)]

memo = {}
while True:
    try:
        n, s = map(int, input().split())
        used = [False for i in range(10)]
        ans = check(used, n, s, memo)
        print(ans)
    except:
        break
### BUGGY CODE ###

N, Q = [int(nq) for nq in input().split(" ")]
S = input()
s = S.replace("AC", "NM")
for i in range(Q):
    li, ri = [int(lr) for lr in input().split(" ")]
    print(s[li-1:ri].count("NM"))


### FIXED CODE ###

N, Q = [int(nq) for nq in input().split(" ")]
S = input()
t = [0] * (N)
for i in range(1, N):
  t[i] = t[i-1] + (1 if (S[i-1] == "A" and S[i] == "C") else 0)
for _ in " " * Q:
  l, r = [int(lr) for lr in input().split(" ")]
  print(t[r-1]-t[l-1])
### BUGGY CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]


N, M = map(int, input().split())

cards = [0] * (N + 1)

for i in range(M):
    L, R = map(int, input().split())

    for j in range(L, R + 1):
        cards[j] += 1

print(cards.count(M))


### FIXED CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]


N, M = map(int, input().split())

l_max = 1
r_min = N

for i in range(M):
    L, R = map(int, input().split())

    l_max = max(l_max, L)
    r_min = min(r_min, R)

print(max(r_min - l_max + 1, 0))
### BUGGY CODE ###

MOD=10**9+7
from fractions import gcd
def lcm(a,b):
    return(a//gcd(a,b)*b)
N=int(input())
A=list(map(int,input().split()))
g=1
for i in range(N):
    g=lcm(g,A[i])
res=0
for i in range(N):
    res=(res+g//A[i])%MOD
print(res)

### FIXED CODE ###

MOD=10**9+7
def factorization(n):
    arr=[]
    tmp=n
    for i in range(2,int(n**0.5)+1):
        if(tmp%i==0):
            cnt=0
            while tmp%i==0:
                cnt+=1
                tmp//=i
            arr.append([i,cnt])
    if(not arr):
        arr.append([1,1])
    if(tmp!=1):
        arr.append([tmp,1])
    return arr
def moddiv(a,m):
    return pow(a,m-2,m)
N=int(input())
A=list(map(int,input().split()))
cnt={}
for i in range(N):
    arr=factorization(A[i])
    for a,b in arr:
        cnt[a]=max(cnt.get(a,0),b)
g=1;res=0
for k,v in cnt.items():
    g=g*(pow(k,v,MOD))%MOD
for i in range(N):
    res=(res+(g*moddiv(A[i],MOD))%MOD)%MOD
print(res)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
adder = 0
if (list(range(1, N + 1)) == A):
    print(0)
else:
    for i in range(1, N + 1):
        aa = len(A)
        for j in range(len(A)):
            if A[j] == i:
                A = A[(j + 1):]
                break
        if (len(A) == 0):
            break
        if j == (aa - 1):
            adder = 1
            break
    if i != 1:
        print(N - i + adder)
    else:
        print(-1)


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))

j = 1
for i in range(N):
    if A[i] == j:
        j = j + 1
if j == 1:
    print(-1)
else:
    print(N - j + 1)
### BUGGY CODE ###

from functools import lru_cache


n, tracks = list(map(int, input().split()))
loads = []
for _ in range(n):
    loads.append(int(input()))

# print(loads)
slices = []
min_load = [99999999999999]

@lru_cache()
def slicing(rest, s=0):
    if not rest:
        # slices.append(s+[(len(s) and s[-1][1], len(loads))])
        return sum(loads[s:])

    result = 99999999999999
    for s1 in range(s+1, len(loads)-rest+1):
        result = min(result, max(sum(loads[s: s1]), slicing(rest-1, s1)))
    return result


rs = slicing(tracks-1)
print(rs)
# print(slices)


# def load_count_max(load):
#     print(load, max(sum(loads[l[0]: l[1]]) for l in load))
    # return max(sum(loads[l[0]: l[1]]) for l in load)


# result = min(map(load_count_max, slices))
# print(result)



### FIXED CODE ###

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


def check(p):
    i = 0
    for j in range(k):
        s = 0
        while s + T[i] <= p:
            s += T[i]
            i += 1
            if i == n:
                return n
    return i


def solve():
    left = 0
    right = 100000 * 10000
    while right - left > 1:
        mid = (left + right) // 2
        v = check(mid)
        if v >= n:
            right = mid
        else:
            left = mid
    return right


T = []
for _ in range(n):
    T.append(int(input()))
ans = solve()
print(ans)
### BUGGY CODE ###

from collections import deque
import sys
import copy

def main():
  input = sys.stdin.readline
  n = int(input())
  a = [None]*n
  for i in range(n):
    a[i] = [int(x)-1 for x in input().split()]
  
  count = 0
  ans = 0
  bef_checked = [int(x) for x in range(n)]
  index = [0]*n
  while True:
    ans += 1
    check = copy.deepcopy(bef_checked)
    bef_checked = []
    already = set()
    flag = True
    for v in check:
      if v in already:
        continue
      flag = False
      v2 = a[v][index[v]]
      if v2 in already:
        continue
      if v != a[v2][index[v2]]:
        continue
      already = already|{v, v2}
      index[v] += 1
      index[v2] += 1
      if index[v] == n-1:
        count += 1
      else:
        bef_checked.append(v)
      if index[v2] == n-1:
        count += 1
      else:
        bef_checked.append(v2)
        
    if flag:
      print(-1)
      sys.exit()
    if count == n:
      break
  print(ans)

  
if __name__ == "__main__":
  main()

### FIXED CODE ###

import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()
### BUGGY CODE ###

n,k = map(int,input().split())
l = list(map(int,input().split()))

cheap = []

for m in range(k):
    for i in range(len(l)):
        flag = False
        for j in range(len(l)):
            if l[i] > l[j]:
                flag = True
                break
        if flag == False:
            cheap.append(l[i])
            l.remove(l[i])
            break

print(sum(cheap))

### FIXED CODE ###

n,k = map(int,input().split())
l = list(map(int,input().split()))

l.sort()

cheap = l[:k]

print(sum(cheap))
### BUGGY CODE ###

from sys import stdin
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
b = []
for i in range(n):
    for j in range(n):
        if i+1 == a[j]:
            b.append(j + 1)
b = [str(i) for i in b]
b = ' '.join(b)
print(b)


### FIXED CODE ###

from sys import stdin
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
b = [0] * n
for i in range(n):
    b[a[i] - 1] = i + 1
b = [str(x) for x in b]
b = ' '.join(b)
print(b)