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

import sys
n = int(input())
a = list(map(int,input().split()))
select = [0 for _ in range(n+1)]
mark = [0 for _ in range(n+1)]
c = 0
for i in range(n-1,0,-1):
    if a[i] ==0 and mark[i+1]%2==1:
        c += 1
        select[i + 1] = 1
        l = 1
        r = (i + 1)
        while (l < r):
            if (i + 1) % l == 0:
                mark[l] += 1
                r = (i + 1) // l
                if l != r:
                    mark[r] += 1
            l += 1
    if a[i] and mark[i+1]%2==0:
        c+=1
        select[i + 1] = 1
        l = 1
        r = (i + 1)
        while (l < r):
            if (i + 1) % l == 0:
                mark[l] += 1
                r = (i + 1) // l
                if l!=r:
                    mark[r] += 1
            l += 1
if a[0]==0 and mark[1]%2==1:
    c+=1
    select[1]=1
    mark[1]+=1
elif a[0]==1 and mark[1]%2==0:
    c += 1
    select[1] = 1
    mark[1] += 1
print(c)
for i in range(1,n+1):
    if select[i]:
        sys.stdout.write('{} '.format(i))

### FIXED CODE ###

import sys,math
n = int(input())
a = list(map(int,input().split()))
select = [0 for _ in range(n+1)]
mark = [0 for _ in range(n+1)]
c = 0
for i in range(n-1,0,-1):
    if a[i] ==0 and mark[i+1]%2==1:
        c += 1
        select[i + 1] = 1
        l = 1
        r = (i + 1)
        m = math.sqrt(i+1)
        m = int(m)
        while (l <=m):
            if (i + 1) % l == 0:
                mark[l] += 1
                r = (i + 1) // l
                if l != r:
                    mark[r] += 1
            l += 1
    if a[i] and mark[i+1]%2==0:
        c+=1
        select[i + 1] = 1
        l = 1
        r = (i + 1)
        m = math.sqrt(i + 1)
        m = int(m)
        while (l <= m):
            if (i + 1) % l == 0:
                mark[l] += 1
                r = (i + 1) // l
                if l!=r:
                    mark[r] += 1
            l += 1
if a[0]==0 and mark[1]%2==1:
    c+=1
    select[1]=1
    mark[1]+=1
elif a[0]==1 and mark[1]%2==0:
    c += 1
    select[1] = 1
    mark[1] += 1
print(c)
for i in range(1,n+1):
    if select[i]:
        sys.stdout.write('{} '.format(i))
### BUGGY CODE ###

from collections import defaultdict

n, wmax = map(int, input().split())
dp = [defaultdict(int) for _ in range(n+1)]
dp[0][0] = 0
for i in range(n):
    w, v = map(int, input().split())
    if i == 0:
        w0 = w
    for j in range(n+1):
        for k in range(300):
            weight = j*w0+k
            if weight <= wmax:
                if weight - w >= 0:
                    dp[i+1][weight] = max(dp[i+1][weight], dp[i][weight - w] + v)
                dp[i+1][weight] = max(dp[i+1][weight], dp[i][weight])

print(max(list(dp[-1].values())))


### FIXED CODE ###

from collections import defaultdict

n, wmax = map(int, input().split())
dp = [defaultdict(int) for _ in range(2)]
dp[0][0] = 0
for i in range(n):
    w, v = map(int, input().split())
    if i == 0:
        w0 = w
    dp[(i+1)%2] = defaultdict(int)
    for j in range(n+1):
        for k in range(300):
            weight = j*w0+k
            if weight <= wmax:
                if weight - w >= 0:
                    dp[(i+1)%2][weight] = max(dp[(i+1)%2][weight], dp[i%2][weight - w] + v)
                dp[(i+1)%2][weight] = max(dp[(i+1)%2][weight], dp[i%2][weight])

print(max(list(dp[n%2].values())))
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
for i in range(n):
  print(a.count(i+1)) 

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
x=[0]*n
for i in a:
  x[i-1]+=1
for i in x:
  print(i)
### BUGGY CODE ###

amount = 10 ** 6
x = int(input())
cnt = 0
can_buy = 0

xx = x % 100
if x >= 100:
    for i in range(xx + 1):
        for j in range(xx//2 + 1):
            for k in range(xx // 3 + 1):
                for l in range(xx // 4 + 1):
                    for m in range(xx // 5 + 1):
                        if i + 2 * j + 3 * k + 4 * l + 5 * m == xx:
                            rest = x - 101 * i - 102 * j - 103 * k - 104 * l - 105 * m
                            cnt = rest // 100
                            if rest >= 0 and cnt <= amount:
                                can_buy = 1
                                break

print(can_buy)

### FIXED CODE ###

x = int(input())
n = x // 100
can_buy = 0
while n > 0:
    if 100 * n <= x <= 105 * n:
        can_buy = 1
        break
    else:
        n -= 1
print(can_buy)
### BUGGY CODE ###

N,Y = input().split()
N = int(N)
Y = int(Y)

for a in range(N+1):
  for b in range(N+1):
    X = 10000*a + 5000*b + 1000*(N-int(a)-int(b))
    #c = N-int(a)-int(b)
    if X==Y and N-int(a)-int(b)>=0:
      c = N-int(a)-int(b)
      #print(X)
      break
  else:
    continue
  break
if a+b>N:
  a=b=c=-1
print(a,b,c)

### FIXED CODE ###

N, Y = map(int, input().split())
for x in range(N+1):
    for y in range(N-x+1):
        z = N-x-y
        if 0 <= z <= 2000 and 10000*x+5000*y+1000*z == Y:
            print(x, y, z)
            exit()
print(-1, -1, -1)
### BUGGY CODE ###

from itertools import product
from random import randint, seed
from time import time
seed(time())

def sol():
    N = int(input())
    XYP = [tuple(map(int, input().split())) for _ in range(N)]
    XYP.sort(key=lambda xyp: xyp[2], reverse=(randint(0, 3) != 0))

    X = [x for x, _, _ in XYP]
    Y = [y for _, y, _ in XYP]
    INF = 10**18

    def calcCost(AP, A):
        ret = [[INF] * N for _ in range(1 << N)]
        for state in range(1 << N):
            useA = [a for i, a in enumerate(A) if (state & (1 << i)) != 0]
            for i, (a, p) in enumerate(AP):
                cost = abs(a)
                for z in useA:
                    cost = min(cost, abs(a - z))
                ret[state][i] = cost * p
        return ret

    costX = calcCost([(x, p) for x, _, p in XYP], X)
    costY = calcCost([(y, p) for _, y, p in XYP], Y)

    ans = [INF] * (N + 1)
    for state in product((0, 1, 2), repeat=N):
        stateX = 0
        stateY = 0
        M = N
        for s in state:
            stateX <<= 1
            stateY <<= 1
            if s == 0: M -= 1
            if s == 1: stateX |= 1
            if s == 2: stateY |= 1

        cx, cy = costX[stateX], costY[stateY]
        cost = 0
        for i in range(N):
            cost += min(cx[i], cy[i])
            if cost >= ans[M]:
                break
        else:
            ans[M] = cost

    print(*ans, sep='\n')

sol()

### FIXED CODE ###

import sys
input = sys.stdin.buffer.readline
from numba import njit
import numpy as np
INF = 10**18

@njit('(i8, i8[:,:])')
def solve(N, XYP):
    X = XYP[:, 0]
    Y = XYP[:, 1]

    def calcCost(sign, A):
        ret = np.ones((1 << N, N), dtype=np.int64) * INF
        for state in range(1 << N):
            useA = [a for i, a in enumerate(A) if (state & (1 << i)) != 0]
            for i in range(N):
                xyp = XYP[i]
                cost = abs(xyp[sign])
                for z in useA:
                    cost = min(cost, abs(xyp[sign] - z))
                ret[state][i] = cost * xyp[2]
        return ret

    costX = calcCost(0, X)
    costY = calcCost(1, Y)
    ans = np.ones(N + 1, dtype=np.int64) * INF

    for state in range(pow(3, N)):
        stateX = 0
        stateY = 0
        M = N
        for _ in range(N):
            state, r = divmod(state, 3)
            stateX <<= 1
            stateY <<= 1
            if r == 0: M -= 1
            if r == 1: stateX |= 1
            if r == 2: stateY |= 1

        cx, cy = costX[stateX], costY[stateY]
        cost = 0
        for i in range(N):
            cost += min(cx[i], cy[i])
        ans[M] = min(ans[M], cost)

    for a in ans:
        print(a)

def sol():
    N = int(input())
    XYP = np.asarray([list(map(int, input().split())) for _ in range(N)])
    solve(N, XYP)

sol()
### BUGGY CODE ###

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

fi_flg = False
res = 0
while fi_flg is False:
  twice_flg = False
  for i in range(N):
    if a[i] % 2 == 0:
      if twice_flg is False:
        a[i] = int(a[i]/2)
        twice_flg = True
 
  if twice_flg is False:
    fi_flg = True
  else:
    res += 1

print(res)

### FIXED CODE ###

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

res = 0
for i in range(N):
  fi_flg = False
  while fi_flg is False:
    if a[i] % 2 == 0:
      a[i] = int(a[i]/2)
      res += 1
    else:
      fi_flg = True
      
print(res)
### BUGGY CODE ###

import sys

sys.setrecursionlimit(10 ** 9)


def dfs(v, p, c):
    cnt[v] += c
    for next_v in g[v]:
        if next_v == p:
            continue
        dfs(next_v, v, cnt[v])


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

g = [[] for _ in range(N)]
cnt = [0 for _ in range(N)]
for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)

for i in range(Q):
    p, x = map(int, input().split())
    p -= 1
    cnt[p] += x

dfs(0, -1, 0)

print(*cnt)

### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)


def dfs(v, p):
    for next_v in g[v]:
        if next_v == p:
            continue
        cnt[next_v] += cnt[v]
        dfs(next_v, v)


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

g = {i:[] for i in range(N+1)}
cnt = {i:0 for i in range(N+1)}
for _ in range(N - 1):
    a, b = map(int, input().split())
    g[a].append(b)
    g[b].append(a)

for i in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x

dfs(1, -1)

print(*list(cnt.values())[1:])
### BUGGY CODE ###

import itertools
n=int(input()) 
 
L=list(map(int, input().split())) 
 
comb = list(itertools.combinations(L, 3))

count = 0

for i in comb:
  if i[0]+i[1]>i[2] and i[1]+i[2]>i[0] and i[2]+i[0]>i[1]:
    count = count+1
print(count)

### FIXED CODE ###

import bisect
n=int(input()) 
L=list(map(int, input().split())) 
L.sort()

count = 0        
for i in range(n-2):
  a = L[i]
  for k in range(i+1,n-1):
    b = L[k]

    count += bisect.bisect_left(L, a + b) - k -1
print(count)
### BUGGY CODE ###

n=int(input())
a_s=list(map(int,input().split()))
while True:
    a_s=set(a_s)
    a_s.discard(0)
    a_s=list(a_s)
    index=a_s.index(min(a_s))
    for i in range(len(a_s)):
        if i!=index and min(a_s)!=0:
            a_s[i]=a_s[i]%min(a_s)
    if len(a_s)==1:
        break
print(sum(a_s))

### FIXED CODE ###

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

def sakujo(a_list):
    for i in range(len(a_list)):
        if a_list[i]!=0:
            end=i
            break
    return a_list[i:]

while True:
    a_s.sort()
    a_s=sakujo(a_s)
    if len(a_s)==1:
        break
    for i in range(1,len(a_s)):
        a_s[i]=a_s[i]%a_s[0]

print(sum(a_s))
### BUGGY CODE ###

def yakusu(n):
    results = []
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            results.append(i)
            results.append(n // i)
    return list(set(results)) + [n]

N = int(input())
count = 0
for i in yakusu(N):
    NN = N
    while NN >= i:
        if NN % i == 0:
            NN //= i
        else:
            NN %= i
    if NN == 1:
        count += 1
for i in yakusu(N-1):
    NN = N
    while NN >= i:
        if NN % i == 0:
            NN //= i
        else:
            NN %= i
    if NN == 1:
        count += 1
print(count)


### FIXED CODE ###

def yakusu(n):
    if n == 1:
        return []
    results = []
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            results.append(i)
            results.append(n // i)
    return list(set(results)) + [n]

N = int(input())
count = 0
for i in yakusu(N):
    NN = N
    while NN >= i:
        if NN % i == 0:
            NN //= i
        else:
            NN %= i
    if NN == 1:
        count += 1
for i in yakusu(N-1):
    NN = N
    while NN >= i:
        if NN % i == 0:
            NN //= i
        else:
            NN %= i
    if NN == 1:
        count += 1
print(count)
### BUGGY CODE ###

def main():
    INF = float('inf')
    N,W = map(int,input().split())
    wv = [list(map(int,input().split())) for _ in range(N)]
    sumv = sum([v for w,v in wv])
    dp = [[INF]*(sumv+1) for _ in range(N+1)]
    dp[0][0] = 0
    for i in range(N):
        for j in range(sumv+1):
            if j-wv[i][1] >= 0:
                dp[i+1][j] = min(dp[i][j],dp[i][j-wv[i][1]]+wv[i][0])
            else:
                dp[i+1][j] = dp[i][j]
    ans = 0
    for i in range(sumv+1):
        if dp[N][i] <= W:
            ans = max(ans,i)
    print(ans)
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    INF = float('inf')
    N,W = map(int,input().split())
    wv = [list(map(int,input().split())) for _ in range(N)]
    sumv = sum([v for w,v in wv])
    dp = [[INF]*(sumv+1) for _ in range(N+1)]
    dp[0][0] = 0
    for i in range(N):
        for j in range(sumv+1):
            if j-wv[i][1] >= 0:
                dp[i+1][j] = min(dp[i][j],dp[i][j-wv[i][1]]+wv[i][0])
            else:
                dp[i+1][j] = dp[i][j]
    ans = 0
    for i in range(sumv+1):
        if dp[N][i] <= W:
            ans = max(ans,i)
    print(ans)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
input = sys.stdin.readline
h,w = map(int,input().split())
l=['#'*(w+2)]+['#'+input().strip()+'#' for i in range(h)]+['#'*(w+2)]
dp = [[[0]*4 for i in range(w+2)] for i in range(h+2)]
ans = 0

for i in range(h+2):
  for j in range(w+2):
     if l[i][j] == '#':
      dp[i][j][0] = 0
     else:
      dp[i][j][0] = dp[i][j-1][0] + 1
for i in range(h+2):
  for j in range(w+1,0,-1):
     if l[i][j] == '#':
      dp[i][j][1] = 0
     else:
      dp[i][j][1] = dp[i][j+1][1] + 1
for i in range(h+2):
  for j in range(w+2):
     if l[i][j] == '#':
      dp[i][j][2] = 0
     else:
      dp[i][j][2] = dp[i-1][j][2] + 1
for i in range(h+1,0,-1):
  for j in range(w+2):
     if l[i][j] == '#':
      dp[i][j][3] = 0
     else:
      dp[i][j][3] = dp[i+1][j][3] + 1
for i in range(h+2):
  for j in range(w+2):
    if l[i][j] == '.':
      ans = max(ans, sum(dp[i][j]) - 3)
print(ans)

### FIXED CODE ###

import numpy as np
h,w = map(int,input().split())
ar = np.array([list(input()) for _ in range(h)]) == '.'
#print(ar)
l = np.zeros((h,w),dtype=int)
r = np.zeros((h,w),dtype=int)
u = np.zeros((h,w),dtype=int)
d = np.zeros((h,w),dtype=int)

for i in range(h):
  u[i] = (u[i-1]+1)*ar[i]
  #print(u)
  d[-i-1] = (d[-i]+1)*ar[-i-1]
  #print(d)
  
for j in range(w):
  l[:,j] = (l[:,j-1]+1)*ar[:,j]
  #print(l)
  r[:,-j-1] = (r[:,-j]+1)*ar[:,-j-1]
  #print(r)
print(np.max(u+d+l+r)-3)
### BUGGY CODE ###

import sys
n,m = map(int, sys.stdin.readline().rstrip("\n").split())
cage = {}
for i in range (2,n+1):
    cage[i] = None
ab = [map(int, line.rstrip("\n").split()) for line in sys.stdin]
a,b = [list(i) for i in zip(*ab)]
room = [1]
nroom = []
while None in cage.values():
    for i in range(len(room)):
        j = 0
        while j < (len(a)):
            if a[j] == room[i]:
                if cage[b[j]] == None:
                    cage[b[j]] = a[j]
                nroom.append(b[j])
                a.pop(j)
                b.pop(j)
            elif b[j] == room[i]:
                if cage[a[j]] == None:
                    cage[a[j]] = b[j]
                nroom.append(a[j])
                a.pop(j)
                b.pop(j)
            else:
                j += 1
    room = nroom[:]
    nroom.clear()
    if len(a) == 0 and None in cage.values():
        print('No')
        break
else:
    print('Yes')
    for i in range (2,n+1):
        print(cage[i])

### FIXED CODE ###

import sys
import queue
n,m = map(int, sys.stdin.readline().rstrip("\n").split())
adj = [[]for i in range(n+1)]
parent = [-1] * (n+1)
for line in sys.stdin:
    a,b = map(int, line.rstrip("\n").split())
    adj[a].append(b)
    adj[b].append(a)
q = queue.Queue()
q.put((1,0))
while(not q.empty()):
    x, p = q.get()
    if parent[x] != -1: # 訪問済み
        continue
    parent[x] = p
    for next in adj[x]:
        if parent[next] == -1: # 未訪問なら
            q.put((next,x))
print ('Yes') # 必ず目標は達成できる
for p in parent[2:]: # 部屋2以降の親（前の部屋）
    print(p)
### BUGGY CODE ###

from itertools import permutations
import sys

N, M, R = map(int, sys.stdin.readline().split())
r = [i - 1 for i in map(int, sys.stdin.readline().split())]
        
d = [[float('inf')] * N for _ in range(N)]
        
for i in range(N):
  d[i][i] = 0
            
for i in range(M):
  a, b, c = map(int,sys.stdin.readline().split())
  d[a-1][b-1] = c
  d[b-1][a-1] = c
            
  for k in range(N):
    for i in range(N):
      for j in range(N):
        if d[i][j] > d[i][k] + d[k][j]:
          d[i][j] = d[i][k] + d[k][j]
        
ans = 10**12
        
for each in permutations(r):
  t_dis = 0
  for i in range(R-1):
    t_dis += d[each[i]][each[i+1]]  
            
  ans = min(ans, t_dis)
        
print(ans)

### FIXED CODE ###

from itertools import permutations
import sys

def main():
    
    N, M, R = map(int, sys.stdin.readline().split())
    r = [i - 1 for i in map(int, sys.stdin.readline().split())]
    
    d = [[float('inf')] * N for _ in range(N)]
    
    for i in range(N):
        d[i][i] = 0
        
    for i in range(M):
        a, b, c = map(int,sys.stdin.readline().split())
        d[a-1][b-1] = c
        d[b-1][a-1] = c
        
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if d[i][j] > d[i][k] + d[k][j]:
                    d[i][j] = d[i][k] + d[k][j]
    
    ans = 10**12
    
    for each in permutations(r):
        t_dis = 0
        for i in range(R-1):
            t_dis += d[each[i]][each[i+1]]  
        
        ans = min(ans, t_dis)
    
    print(ans)

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

N,T = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N)]

dp = [[[-1,-1] for t in range(T)] for i in range(N+1)]
dp[0][0][0] = 0
for i,(a,b) in enumerate(AB):
    for t in range(T-1,-1,-1):
        for k in range(2):
            dpitk = dp[i][t][k]
            if dpitk < 0: continue
            if dp[i+1][t][k] < dpitk: dp[i+1][t][k] = dpitk
            if t+a < T:
                if dp[i+1][t+a][k] < dpitk + b: dp[i+1][t+a][k] = dpitk + b
            if k==0:
                if dp[i+1][t][1] < dpitk + b: dp[i+1][t][1] = dpitk + b
print(max(a[1] for a in dp[-1]))

### FIXED CODE ###

N,T = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N)]

dp0 = {0:0}
dp1 = {0:0}

for i,(a,b) in enumerate(AB):
    _dp0 = {0:0}
    _dp1 = {0:0}
    for k0,v0 in dp0.items():
        if k0 in _dp0:
            _dp0[k0] = max(_dp0[k0], v0)
        else:
            _dp0[k0] = v0
        if k0+a < T:
            if k0+a in _dp0:
                _dp0[k0+a] = max(_dp0[k0+a], v0+b)
            else:
                _dp0[k0+a] = v0+b
        if k0 in _dp1:
            _dp1[k0] = max(_dp1[k0], v0+b)
        else:
            _dp1[k0] = v0+b
    for k1,v1 in dp1.items():
        if k1 in _dp1:
            _dp1[k1] = max(_dp1[k1], v1)
        else:
            _dp1[k1] = v1
        if k1+a < T:
            if k1+a in _dp1:
                _dp1[k1+a] = max(_dp1[k1+a], v1+b)
            else:
                _dp1[k1+a] = v1+b
    dp0 = _dp0
    dp1 = _dp1

print(max(dp1.values()))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n,q=map(int,input().split())
s=input()
lr=[input() for __ in range(q)]

ans=[]

for i in lr:
    l,r=map(int,i.split())
    #print(int((len(s[l-1:r])-len(s[l-1:r].replace('AC',"")))/2))
    ans.append(str(int((len(s[l-1:r])-len(s[l-1:r].replace('AC',"")))/2)))

print('\n'.join(ans))

### FIXED CODE ###

n,q=map(int,input().split())
s=input()
lr=[input() for __ in range(q)]
cs=[0]*(n+1)

#端がACで-1で止まる文字列指定は大丈夫？.
for i in range(n):
    cs[i+1]=cs[i] + (1 if s[i:i+2] == 'AC' else 0)

for num in lr:
    l,r=map(int,num.split())
    print(cs[r-1] - cs[l-1])
### BUGGY CODE ###

#!/usr/bin/env python3


def main() -> None:
    N, K = rmi()
    A = rmi()
    road = []
    current = 1
    # route map make
    for n in range(N):
        if not current in road:
            road.append(current)
            current = A[current - 1]
        else:
            road.append(current)
            break
    # route map check
    dup = [x for x in set(road) if road.count(x) > 1][0]
    if dup == 1:
        loop = road[:-1]
        rem = K % len(loop)
        w(loop[rem])
    else:
        indexes = [i for i, x in enumerate(road) if x == dup]
        loop = road[indexes[0]: indexes[1]]
        if K < indexes[0]:
            w(road[K])
        else:
            K -= indexes[0]
            rem = K % len(loop)
            w(loop[rem])


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10 ** 9)
    main()


### FIXED CODE ###

#!/usr/bin/env python3


def main() -> None:
    N, K = rmi()
    A = rmi()
    route = []
    visited = [False] * N
    current = 1
    for n in range(N):
        route.append(current)
        if not visited[current - 1]:
            visited[current - 1] = True
            current = A[current - 1]
        else:
            break
    dup = route[-1]
    dup_index = route.index(dup)
    roop_range = len(route) - dup_index - 1
    if K < dup_index:
        w(route[K])
    else:
        w(route[dup_index:][(K - dup_index) % roop_range])


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10 ** 9)
    main()
### BUGGY CODE ###

import sys
from collections import deque
n = int(input())
command = sys.stdin.readlines()
Q = deque()
for i in range(n):
    a,b = command[i].split()
    if a == "insert":
        Q.append(b)
    else:
        if b in Q:
            print("yes")
        else:
            print("no")

### FIXED CODE ###

import sys
n = int(input())
dictionary = {}

k = sys.stdin.readlines()


for j in k:
    i = j.split()
    if i[0] == "insert":
        dictionary[i[1]] = 0
    else:
        if i[1] in dictionary:
            print("yes")
        else:
            print("no")
### BUGGY CODE ###

from collections import deque

import sys
input = sys.stdin.readline


n = int(input())
a_list = {i:None for i in range(n)}
num = 0
day = 0
for i in range(n):
    a_list[i] = deque([int(i) - 1 for i in input().split()])

for kkk in range(n * (n - 1) // 2):
    visit = set([])
    day += 1
    for j in range(n):
        if j not in visit:
            if len(a_list[j]) > 0:
                k = a_list[j][0]
                if k > j:
                    if k not in visit:
                        if a_list[k][0] == j:
                            num += 1
                            visit.add(a_list[j].popleft())
                            visit.add(a_list[k].popleft())
                        if num >= n * (n - 1) // 2:
                            print(day)
                            exit()
    if (len(visit) == 0):
        print(-1)
        exit()
    if num >= n * (n - 1) // 2:
        print(day)
        exit()

### FIXED CODE ###

from collections import deque
n = int(input())
a_list = []
for i in range(n):
    a_list.append(deque([int(aa) - 1 for aa in input().split()]))
day = 0
battle = 0
for _ in range(n * (n - 1) // 2):
    if battle == n * (n - 1) // 2:
        break
    day += 1
    battle_today = 0
    tomorrow = set([])
    if (day == 1):
        player = set([i for i in range(n)])
    for i in player:
        if len(a_list[i]) == 0:
            continue
        j = a_list[i][0]
        if (i == a_list[j][0]) & (j not in tomorrow) & (len(a_list[j]) > 0) & (i not in tomorrow):
            tomorrow.add(i)
            tomorrow.add(a_list[i][0])
            battle += 1
            battle_today += 1
            a_list[i].popleft()
            a_list[j].popleft()
    player = tomorrow
    if battle_today == 0:
        day = -1
        break
print(day)