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


import sys


def solv(n):
    r = 0
    for p in primes:
        if n/2 < p:
            break
        q = n - p
        if q in primes:
            r += 1
    return r


def prime_list(n):
    limit = int(n ** 0.5) + 1
    lis = range(1, n + 1, 2)
    lis[0] = 2
    while True:
        if len(lis) == 0:
            break
        p = lis.pop(0)
        yield p
        if p <= limit:
            lis = [x for x in lis if x % p != 0]


primes = []
for p in prime_list(50000):
    primes.append(p)

while True:
    n = int(sys.stdin.readline())
    if n == 0:
        exit()
    print solv(n)

### FIXED CODE ###


import sys
import itertools


def prime_list(n):
    limit = int(n ** 0.5) + 1
    lis = range(1, n + 1, 2)
    lis[0] = 2
    while True:
        if len(lis) == 0:
            break
        p = lis.pop(0)
        yield p
        if p <= limit:
            lis = [x for x in lis if x % p != 0]


primes = []
for p in prime_list(50000):
    primes.append(p)
c = [0] * 50001 * 2
for i, j in itertools.combinations(primes, 2):
    c[i+j] += 1
for i in primes:
    c[i+i] += 1
while True:
    n = int(sys.stdin.readline())
    if n == 0:
        exit()
    print c[n]
### BUGGY CODE ###

N = int(input())
a = list(map(int,input().split()))
sai = max(a)
ans = 0
def comb(b,c):
      d = 1
      kake = b
      while d < c:
           kake = kake * (b-d)
           d += 1
           kake = kake / d
      return kake
for i in range(N):
    ro = comb(sai,a[i])
    if ro > ans:
       ans = ro
       kore = a[i]
print(sai,kore)

### FIXED CODE ###

N = int(input())
a = list(map(int,input().split()))
a = sorted(a)
sai = max(a)
ma = 100000000000000000000000000
naka = sai / 2
for i in range(N):
    sa = abs(naka - a[i])
    if ma > sa:
       ma = sa
       kore = a[i]
print(sai,kore)
### BUGGY CODE ###

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

import heapq
heapq.heapify(A)

for i in range(M):
  b,c=map(int,input().split())
  for j in range(b):
    val=heapq.heappop(A)
    if val<c:
      heapq.heappush(A,c)
    else:
      heapq.heappush(A,val)
      break
print(sum(A))

### FIXED CODE ###

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

Q=[None]*M
for i in range(M):
  b,c=map(int,input().split())
  Q[i]=[b,c]
  
Q=sorted(Q,key=lambda x:x[1])[::-1]

A=sorted(A)
ind=0
for i in range(len(Q)):
  b,c=Q[i]
  for p in range(b):
    if ind+p>=len(A):
      print(sum(A))
      exit(0)
    if A[ind+p]<c:
      A[ind+p]=c
    else:
      break
  ind+=b
else:
  print(sum(A))
### BUGGY CODE ###

import heapq
N, M = map(int,input().split())
A = [0 for i in range(N)]
B = [0 for i in range(N)]
for i in range(N):
    A[i], B[i] = map(int,input().split())
ptj = {}
for i in range(N):
    if A[i] in ptj:
        ptj[A[i]].append(-B[i])
    else:
        ptj[A[i]] = [-B[i]]
count = 0
s = []
for i in range(1,M+1):
    if i in ptj:
        s.extend(ptj[i])
        heapq.heapify(s)
    if s != []:
        count -= heapq.heappop(s)
print(count)

### FIXED CODE ###

import heapq
N, M = map(int,input().split())
A = [0 for i in range(N)]
B = [0 for i in range(N)]
for i in range(N):
    A[i], B[i] = map(int,input().split())
ptj = {}
for i in range(N):
    if A[i] in ptj:
        ptj[A[i]].append(-B[i])
    else:
        ptj[A[i]] = [-B[i]]
count = 0
s = []
for i in range(1,M+1):
    if i in ptj:
        for j in ptj[i]:
            heapq.heappush(s,j)
    if s != []:
        count -= heapq.heappop(s)
print(count)
### BUGGY CODE ###

# coding: utf-8

S = input()
Q = int(input())
# print("S:{}".format(S))
# print("Q:{}".format(Q))
query = []

for i in range(Q):
    query.append(input().split())
# for i in query:
#     print(i)

for i in query:
    if len(i) == 1:
        S = S[::-1]
    else:
        if i[1]=="1":
            S = i[2]+S
        else:
            S += i[2]
print(S)


### FIXED CODE ###

# coding: utf-8

S = input()
Q = int(input())

query = []

for i in range(Q):
    query.append(input().split())


bool = True
pre =''
suf =''

for i in query:
    if len(i) == 1:
        bool = not(bool)

    else:
        if i[1]=="1":
            if bool:
                pre = i[2]+pre
            else:
                suf += i[2]
        else:
            if bool:
                suf += i[2]
            else:
                pre = i[2]+pre

S = pre+ S +suf
print(S) if bool else print(S[::-1])
### BUGGY CODE ###

n,W=map(int, input().split())
w=[0]*n
v=[0]*n
for i in range(n):
    x,y = map(int, input().split())
    w[i] = x
    v[i] = y
dp = [[0 for i in range(W+1)] for j in range(n+1)]
for i in range(n):
    for j in range(W+1):
        if j < w[i]:
            dp[i + 1][j] = dp[i][j]
        else:
            dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i])
#print(dp)
print(dp[n][W])

### FIXED CODE ###

n,W=map(int, input().split())
w=[0]*n
v=[0]*n
for i in range(n):
    x,y=map(int, input().split())
    w[i]=x
    v[i]=y
dp=[[0 for i in range(W+1)] for j in range(n+1)]
for i in range(n):
    for j in range(W+1):
        if j<w[i]:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i])
print(dp[n][W])
#print(dp)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N = int(input())
A = [None] + [list(map(int, input().split()))[::-1] for _ in range(N)]
ans = 0
while any(A):
    used = set()
    for i in range(1, N+1):
        if not A[i] or i in used:
            continue
        j = A[i][-1]
        if j in used:
            continue
        if A[j][-1] == i:
            used.add(i)
            used.add(j)
            A[i].pop()
            A[j].pop()
    if not used:
        ans = -1
        break
    ans += 1
print(ans)


### FIXED CODE ###

from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = [deque(map(lambda x: int(x)-1, input().split())) for _ in range(N)]
    ans = 0
    used_prev = set(range(N))
    while any(A):
        used = set()
        for i in used_prev:
            a = A[i]
            if not a or i in used:
                continue
            j = a[0]
            b = A[j]
            if not b or j in used:
                continue
            if i == b[0]:
                A[i].popleft()
                A[j].popleft()
                used.add(i)
                used.add(j)
        if not used:
            ans = -1
            break
        used_prev = used
        ans += 1
    print(ans)


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

from math import gcd

K=int(input())
sum=0

for i in range(K):
  for j in range(K):
    for k in range(K):
       sum+=gcd(gcd(i+1,j+1),k+1)    
print(sum)

### FIXED CODE ###

from math import gcd

K=int(input())+1
sum=0

for i in range(1,K):
  for j in range(1,K):
    for k in range(1,K):
       sum+=gcd(gcd(i,j),k)    
print(sum)
### BUGGY CODE ###

from functools import lru_cache, reduce
from operator import xor


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


# 右の箱から順に詰める
@lru_cache(maxsize=None)
def f(i):
    return reduce(
        xor,
        (f(j) for j in range(2 * i, N + 1, i)),
        a[i - 1]
    )


b = [i for i in range(1, N + 1) if f(i)]
ans = (
    '0' if len(b) == 0 else
    '{}\n{}'.format(
        len(b),
        ' '.join(map(str, b))
    )
)

print(ans)


### FIXED CODE ###

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

b = [0 for _ in range(N + 1)]
M = 0
for i in range(N, 0, -1):
    x = a[i - 1]
    for j in range(i, N + 1, i):
        x += b[j]
    x = x % 2
    b[i] = x
    M += x
print(M)
for i in range(1, N + 1):
    if b[i] == 1:
        print(i)
### BUGGY CODE ###

R,G,B,N = map(int,input().split())
cnt = 0
for r in range(N//R+1):
	for g in range((N-r*R)//G+1):
		if (N-R*r-G*g)%B==0 and (N-R*r-G*g)//B>=0:
			cnt += 1

print(cnt)

### FIXED CODE ###

R,G,B,N = map(int,input().split())
cnt = 0
for r in range(N//R+1):
	n=N-r*R
	for g in range(n//G+1):
		n1=n-G*g
		if n1%B==0 and n1//B>=0:
			cnt += 1

print(cnt)
### BUGGY CODE ###

N, Ma, Mb = map(int, input().split())

abc = [list(map(int, input().split())) for i in range(N)]
cost = 4001


for i in range(1, 2**N):
  a, b = 0, 0
  tmp = 0
#  flag = 0
  for j in range(N):
    if (i>>j) & 1:
#      flag = 1
      tmp += abc[j][2]
      a += abc[j][0]
      b += abc[j][1]
  if a/Ma == b/Mb:
    cost = min(tmp, cost)
if cost <= 4000:
  print(cost)
else:
  print(-1)

  

### FIXED CODE ###

N, Ma, Mb = map(int, input().split())
table = []
for i in range(N):
  a,b,c = map(int, input().split())
  table.append([a,b,c])
inf = 10**9
dp = [[[inf]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(1,N+1):
  a,b,c = table[i-1]
  for j in range(401):
    for k in range(401):
      dp[i][j][k] = dp[i-1][j][k]
      if 400>=j-a>=0 and 400>=k-b>=0:
        dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = 10**9
i = 1
while max(Ma,Mb)*i <= 400:
  ans = min(ans,dp[N][Ma*i][Mb*i])
  i += 1
if ans == 10**9:
  ans = -1
print(ans)
### BUGGY CODE ###

r,g,b,n = map(int,input().split())
 
ans = 0
for i in range(n//r+1):
    for j in range((n-r*i)//g+1):
        if (n-r*i-g*j) < 0:
            break
        if (n-r*i-g*j)%b == 0:
            ans += 1
print(ans)

### FIXED CODE ###

r,g,b,n = map(int,input().split())
 
ans = 0
for i in range(n//r+1):
    for j in range((n-r*i)//g+1):
        if (n-r*i-g*j)%b == 0:
            ans += 1
print(ans)
### BUGGY CODE ###

N = int(input())
S = []
for x in range(N):
    S.append(input())
S_label_n = []
S_label = list(set(S))
for x in S_label:
    S_label_n.append(S.count(x))
maxIndex = [i for i, x in enumerate(S_label_n) if x == max(S_label_n)]
ans_list = []
for x in maxIndex:
    ans_list.append(S_label[x])
ans_list.sort()
for x in ans_list:
    print(x)

### FIXED CODE ###

import sys
input = sys.stdin.readline
N = int(input())
dictS = {}
for x in range(N):
    tmp = input().rstrip()
    if tmp in dictS:
        dictS[tmp] = dictS[tmp]+1
    else:
        dictS[tmp]=1
        
max_n =max(dictS.values())
maxIndex = [x for x in dictS.items() if x[1] ==max_n ]
ans_list = []
for x in maxIndex:
    ans_list.append(x[0])
ans_list.sort()
for x in ans_list:
    print(x)
### BUGGY CODE ###

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

num = max(l)+1
rcd = [0 for _ in range(num)]

for i in range(1, num):
    for j in l:
        if i % j == 0:
            rcd[i] += 1
            if rcd[i] >= 2:
                break
        else:
            continue

ans = 0
for k in l:
    if rcd[k] == 1:
        ans += 1

print(ans)

### FIXED CODE ###

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

num = max(l)+1
rcd = [0 for _ in range(num)]

for v in l:
    rcd[v] += 1

unq = []
for w in range(num):
    if rcd[w] == 1:
        unq.append(w)
#print(unq)

rcd = [0 for _ in range(num)]
l = set(l)
for x in l:
    for y in range(2*x, num, x):
        rcd[y] = 1
#print(rcd)

ans = 0
for z in unq:
    if rcd[z] == 0:
        ans += 1
print(ans)
### BUGGY CODE ###

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

dp = [float("inf")] * (N+K)
dp[0] = 0
for i in range(N):
    for j in range(K):
        if i+j+1 > N-1:
            break
        else:
            dp[i+j+1] = min(dp[i+j+1], dp[i]+abs(h[i] - h[i+j+1]))


print(dp[N-1])

### FIXED CODE ###

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

h += [float("inf")] * (K)
dp = [float("inf")] * (N+K)
dp[0] = 0
for i in range(N):
    for j in range(K):
        dp[i+j+1] = min(dp[i+j+1], dp[i]+abs(h[i] - h[i+j+1]))


print(dp[N-1])
### BUGGY CODE ###

from fractions import gcd
k = int(input())
sum = 0
for a in range(1,k+1):
  for b in range(1,k+1):
    for c in range(1,k+1):
      sum += gcd(gcd(a,b),c)
print(sum)          
      

### FIXED CODE ###

from math import gcd
k = int(input())
sum = 0
lst = []
for a in range(1,k+1):
  for b in range(1,k+1):
      g = gcd(a,b)
      lst.append(g)
for c in range(1,k+1):
    for i in range(len(lst)):
        sum += gcd(lst[i],c)     
print(sum)
### BUGGY CODE ###

import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
from math import cos,sin,tan,sqrt
import cmath as cma
import copy as cp
import sys
import re
sys.setrecursionlimit(10**7)
EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf
MOD = 10**9 + 7

def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def adj(n): return [[] for _ in range(n)]

n = iinput()
a = iarr()
amax = 10**6

dp = [1 for i in range(amax+1)]
for num in a:
    for i in range(2, amax//num + 1): dp[num * i] = 0

cnt = col.Counter(a)

ans = 0
for num in a:
    if dp[num] and cnt[num]==1: ans += 1

print(ans)


### FIXED CODE ###

import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
from math import cos,sin,tan,sqrt
import cmath as cma
import copy as cp
import sys
import re
sys.setrecursionlimit(10**7)
EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf
MOD = 10**9 + 7

def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def adj(n): return [[] for _ in range(n)]

n = iinput()
a = iarr()
amax = max(a)
#amax = 10**6

cnt = [0 for i in range(amax+1)]
for num in a:
    for i in range(1,amax//num + 1):
        tmp = num * i
        cnt[tmp] += 1

ans = 0
for num in a:
    if cnt[num]==1: ans += 1
print(ans)
### BUGGY CODE ###

N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[[]]*N
dp=[0]*N
for i in range(1,N):
    tmp=[]
    for j in range(min(i,K)):
        tmp.append(dp[i-j-1]+abs(H[i]-H[i-j-1]))
    else:
        dp[i]=min(tmp)
print(dp[N-1])

### FIXED CODE ###

N,K=map(int,input().split())
H=list(map(int,input().split()))
dp=[[]]*N
dp=[0]*N
for i in range(1,N):
    tmp=[]
    for j in range(min(i,K)):
        tmp.append(dp[i-j-1]+abs(H[i]-H[i-j-1]))
    else:
        dp[i]=min(tmp)
print(dp[N-1])
### BUGGY CODE ###

n,q=map(int,input().split())
s=input()
for _ 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()
num=[0]
be=""

for i in range(n):
  if be=="A" and s[i]=="C":
    num.append(num[-1]+1)
    
  else:
    num.append(num[-1])
    
  be=s[i]
    
for i in range(q):
  l,r=map(int,input().split())
  
  print(abs(num[l]-num[r]))
### BUGGY CODE ###

n,m=map(int,input().split())
a=[int(input()) for i in range(m)]
dp=[0]*(n+1)
dp[0]=1
if 1 in a:
  dp[1]=0
else:
  dp[1]=1
for i in range(2,n+1):
  if i in a:
    dp[i]=0
  else:
    dp[i]=(dp[i-1]+dp[i-2])%1000000007
print(dp[n]%1000000007)

### FIXED CODE ###

n,m=map(int,input().split())
a=set([int(input()) for i in range(m)])
mod=1000000007
dp=[1,1]
if 1 in a:
  dp[1]=0
for i in range(2,n+1):
  if i in a:
    dp.append(0)
  else:
    dp.append((dp[-1]+dp[-2])%mod)
print(dp[-1]%mod)
### BUGGY CODE ###

N, K = [int(i) for i in input().split(' ')]

prev = N

if N == 0:
  print( N)
else:
  while True:
    if prev == 0:
      print(prev)
      break
    elif abs(N - K) >= prev:
      print(prev)
      break
    else:
      N = abs(N - K)
      prev = N




### FIXED CODE ###

N, K = [int(i) for i in input().split(' ')]

def res():
  if N == K:
    return 0
  elif N > K:
    return N % K if K - (N % K) > (N % K) else K - (N % K)
  else :
    return abs(N - K) if abs(N - K) < abs(abs(N-K) - K) else abs(abs(N-K) - K)

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

class Solver():
    def __init__(self, A: [int]):
        self.A = A
        self.max_depth = len(A)

    def solve(self, tgt):
        self.tgt = tgt
        if self._rec_solve(0, 0):
            return 'yes'
        else:
            return 'no'

    def _rec_solve(self, _sum, index):
        if _sum == self.tgt:
            return True
        elif index >= self.max_depth or _sum > self.tgt:
            return False
        else:
            return self._rec_solve(_sum + self.A[index], index + 1) or self._rec_solve(_sum, index + 1)
            


if __name__ == '__main__':
    n = int(input())
    A = [int(i) for i in input().rstrip().split()]
    q = int(input())
    m_ary = [int(i) for i in input().rstrip().split()]
    s = Solver(A)
    for i in m_ary[:]:
        print(s.solve(i))

### FIXED CODE ###

class Solver():
    def __init__(self, A: [int]):
        self.A = A
        self.max_depth = len(A)

    def solve(self, tgt):
        self.tgt = tgt
        self.history = (self.max_depth+1)*[4000 * [None]]
        self.history = [[None for i in range(4000)] for x in range(self.max_depth + 1)]
        if self._rec_solve(0, 0):
            return 'yes'
        else:
            return 'no'

    def _rec_solve(self, _sum, index):
        if self.history[index][_sum] is not None:
            return self.history[index][_sum]
        if _sum == self.tgt:
            self.history[index][_sum] = True
            return True
        if index >= self.max_depth or _sum > self.tgt:
            self.history[index][_sum] = False
            return False
        self.history[index][_sum] = self._rec_solve(_sum + self.A[index], index + 1) or self._rec_solve(_sum, index + 1)
        return self.history[index][_sum]
            


if __name__ == '__main__':
    n = int(input())
    A = [int(i) for i in input().rstrip().split()]
    q = int(input())
    m_ary = [int(i) for i in input().rstrip().split()]
    s = Solver(A)
    for val in m_ary:
        print(s.solve(val))
### BUGGY CODE ###

from collections import deque
n = int(input())
q = deque([(0, 0, 0)])
while q:
    now, cnt, stat = q.popleft()
    if now>n:
        continue
    if now==n:
        print(cnt)
        break
    for i in range(1, 8):
        if stat<=i:
            q.append((now+6**i, cnt+1, i))
    for i in range(1, 7):
        if stat<=i+7:
            q.append((now+9**i, cnt+1, i+7))
    q.append((now+1, cnt+1, 14))

### FIXED CODE ###

import sys
n = int(input())
INF = 10*n
dp = [INF]*(n+1)
dp[0] = 0
for i in range(1, n+1):
    tmp = dp[i-1] + 1
    for j in range(10):
        if i-6**j<0:
            break
        tmp = min(tmp, dp[i-6**j] + 1)
    for j in range(10):
        if i-9**j<0:
            break
        tmp = min(tmp, dp[i-9**j] + 1)
    dp[i] = tmp
print(dp[n])
### BUGGY CODE ###

H, W, D = map(int,input().split())
dist = [[float("inf") for i in range(H*W+1)] for j in range(H*W+1)]
for i in range(H*W+1):
  dist[i][i] = 0
#dist[i][j]は隣接行列
pos = [[0,0] for i in range(H*W+1)]
for i in range(H):
  A = list(map(int,input().split()))
  for j in range(W):
    pos[A[j]] = [i+1, j+1]
for i in range(H*W+1-D):
  dist[i][i+D] = abs(pos[i][0]-pos[i+D][0])+abs(pos[i][1]-pos[i+D][1])
distances = [[] for i in range(D)]#あまりがiの距離
for i in range(1,H*W+1-D):
  d = dist[i][i+D]
  distances[i%D]+=[d]
distances2 = [[0] for i in range(D)]#累積和に直す
for i in range(D):
  K = len(distances[i])
  for j in range(K):
    distances2[i]+=[distances2[i][-1]+distances[i][j]]
Q = int(input())
for i in range(Q):
  L, R = map(int, input().split())
  q = L%D
  l, r = (L-1)//D, (R-1)//D
  print(distances2[q][r]-distances2[q][l])

### FIXED CODE ###

H, W, D = map(int,input().split())
dist = [0]*(H*W+1)#dist[i]はiからi+Dまでの距離
pos = [[0,0] for i in range(H*W+1)]
for i in range(H):
  A = list(map(int,input().split()))
  for j in range(W):
    pos[A[j]] = [i+1, j+1]
for i in range(H*W+1-D):
  dist[i] = abs(pos[i][0]-pos[i+D][0])+abs(pos[i][1]-pos[i+D][1])
distances = [[] for i in range(D)]#あまりがiの距離
for i in range(1,H*W+1-D):
  d = dist[i]
  distances[i%D]+=[d]
distances2 = [[0] for i in range(D)]#累積和に直す
for i in range(D):
  for j in range(len(distances[i])):
    distances2[i]+=[distances2[i][-1]+distances[i][j]]
Q = int(input())
for i in range(Q):
  L, R = map(int, input().split())
  q = L%D
  l, r = (L-1)//D, (R-1)//D
  print(distances2[q][r]-distances2[q][l])
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
Q=int(input())
for i in range(Q):
  b,c=map(int, input().split())
  A= [c if x==b else x for x in A]
  print(sum(A))

### FIXED CODE ###

N=int(input())
A=list(map(int, input().split()))
L=[0]*(100001)
#何回その数字が出てきたかのリスト
ans=0
for a in A:
  L[a]+=1
  #aが出てきた数（2回出てきたら2になる）
  ans+=a
Q=int(input())
for i in range(Q):
  B,C=map(int, input().split())
  key=L[B]
  L[C]+=key
  ans+=key*(C-B)
  L[B]=0
  print(ans)
### BUGGY CODE ###

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

sP = {}
WA =0
AC =0
number_list =[]
for _ in range(M):
    data = input()
    number, result = data.split()
    if number not in number_list:
        number_list.append(number)

    if data not in sP:
        if number + " AC" not in sP:
            if result == "WA":
                sP[data] = 1
                WA += 1

            elif result == "AC":
                sP[data] = 1
                AC += 1
        else:
            pass
    else:
        sP[data] += 1
        if number + " AC" not in sP:
            if result == "WA":
                WA += 1

            elif result == "AC":
                AC += 1
        else:
            pass

for num in number_list:
    num_res = str(num) + " AC"
    if num_res not in sP:
        WA -= sP[str(num) + " WA"]


print(AC, WA)

### FIXED CODE ###

n, m = map(int, input().split())
ACc = [0 for i in range(n)]
WAc = [0 for i in range(n)]

for i in range(m):
    p, s = input().split()
    p = int(p)
    if s == "AC":
        if ACc[p - 1] == 0:
            ACc[p - 1] += 1
    else:
        if ACc[p - 1] != 1:
            WAc[p - 1] += 1
for j in range(n):
    if ACc[j] == 0:
        WAc[j] = 0

print(sum(ACc), sum(WAc))
### BUGGY CODE ###

from collections import deque

import sys
input = sys.stdin.readline


n = int(input())
a_list = [None for _ 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 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)