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

import sys
input =sys.stdin.readline

N=int(input().rstrip('\n'))
s = []
for _ in range(N):
  A,B = (int(x) for x in input().rstrip('\n').split())
  s.append([A,B])
used = 0
now = 0
check = 0
for i in range(N):
  x = min([i[1] for i in s])
  y = min([i[0] for i in s if i[1]==x])
  used += y
  d = s.index([a for a in s if a==[y,x]][0])
  s.pop(d)
  if used > x:
    check += 1
    print('No')
    break
if check == 0:
  print('Yes')

### FIXED CODE ###

import sys
input =sys.stdin.readline

N=int(input().rstrip('\n'))
s = {}
for _ in range(N):
  A,B = (int(x) for x in input().rstrip('\n').split())
  if B in s:
    s[B] += A
  else:
    s[B] = A
m = sorted(list(set(s)))
check = 0
used = 0
for i in m:
  used += s[i]
  if used > i:
    check += 1
    print('No')
    break
if check == 0:
  print('Yes')
### BUGGY CODE ###

from collections import deque

s = deque(input(""))
Q = int(input())
c = 0

for i in range(Q):
    t = input("")
    if len(t) == 1:
        c += 1
    elif t[2] == "1":
        if c % 2 == 0:
            s.appendleft(t[4])
        else:
            s.append((t[4]))
    else:
        if c % 2 == 0:
            s.append(t[4])
        else:
            s.appendleft(t[4])

if c % 2 == 0:
    for i in s:
        print(i,end="")
else:
    for i in range(len(s)-1,-1,-1):
        print(s[i],end="")



### FIXED CODE ###

s = input("")
Q = int(input())
c = 0
before = []
after = []
for i in range(Q):
    t = input()
    if len(t) == 1:
        c += 1
    elif t[2] == "1":
        if c%2 == 0:
            before.append(t[4])
        else:
            after.append(t[4])
    else:
        if c%2 == 0:
            after.append(t[4])
        else:
            before.append(t[4])

if c%2 == 0:
    print("".join(before[::-1]),end="")
    print(s,end="")
    print("".join(after),end="")
else:
    print("".join(after[::-1]),end="")
    print(s[::-1],end="")
    print("".join(before),end="")
### BUGGY CODE ###

L,R=map(int,input().split())
M=[]
for i in range(L,R+1):
    for j in range(L,R+1):
        if j<=i:
            continue
        else:
            m = (i*j)%2019
            M.append(m)
print(min(M))

### FIXED CODE ###

L,R=map(int,input().split())
M=[]
for i in range(L,R+1):
    for j in range(L,R+1):
        if j<=i:
            continue
        else:
            m = (i*j)%2019
            if m==0:
                print(0)
                exit()
            M.append(m)
print(min(M))
### BUGGY CODE ###

import sys
import collections
sys.setrecursionlimit(10**6)


def main(input, print):
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()
    a = list(collections.Counter(a).items())
    ans = 0
    for i in range(len(a)):
        if a[i][1] > 1:
            continue
        isDiv = False
        for j in range(i):
            if a[i][0] % a[j][0] == 0:
                isDiv = True
                break
        if not isDiv:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)


### FIXED CODE ###

import sys
import collections
sys.setrecursionlimit(10**6)

M = 10**6+5


def main(input, print):
    n = int(input())
    a = list(map(int, input().split()))
    cnt = [0] * M
    for i in range(n):
        if cnt[a[i]] != 0:
            cnt[a[i]] += 1
            continue

        for j in range(a[i], M, a[i]):
            cnt[j] += 1
    ans = 0
    for i in range(n):
        if cnt[a[i]] == 1:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)
### BUGGY CODE ###

import copy

N = int(input())
A = []
for n in range(N):
    A.append(int(input()))
for i in range(N):
    Ai = copy.copy(A)
    Ai.remove(Ai[i])
    L = copy.copy(Ai)
    L.sort(reverse=True)
    print(L[0])

### FIXED CODE ###

import copy

N = int(input())
A = []
for n in range(N):
    A.append(int(input()))

V = copy.copy(A)
V.sort(reverse=True)
for i in range(N):
    if V[0] == A[i]:
        print(V[1])
    else:
        print(V[0])
### BUGGY CODE ###

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

cumA, cumB = [0], [0]
sa, sb = 0, 0

for a in A:
    sa += a
    cumA.append(sa)
for b in B:
    sb += b
    cumB.append(sb)

ans = 0

for i in range(N+1):
    a = cumA[i]
    if a > K:
        break
    for j in range(M, -1, -1):
        if a + cumB[j] <= K:
            break
    
    ans = max(i + j, ans)    

print(ans)


### FIXED CODE ###

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

cumA, cumB = [0], [0]
sa, sb = 0, 0

for a in A:
    sa += a
    cumA.append(sa)
for b in B:
    sb += b
    cumB.append(sb)

ans = 0

for i in range(N, -1, -1):
    a = cumA[i]
    if a > K:
        continue

    b = K - a

    low = 0
    high = M

    while low <= high:
        mid = (low + high) //2
        guess = cumB[mid]
        if guess <= b:
            if mid == M:
                break
            elif cumB[mid+1] > b:
                break

        if guess > b:
            high = mid -1
        else:
            low = mid + 1

    """
    for j in range(M, -1, -1):
        if i + j == ans:
            break
        if a + cumB[j] <= K:
            break
    """
    ans = max(i + mid, ans)   

print(ans)
### BUGGY CODE ###

import math

n=int(input())+1
ans=0
for i in range(1,n):
  for j in range(1,n):
    for k in range(1,n):
      g1=math.gcd(i,j)
      g=math.gcd(k,g1)
      ans+=g
print(ans)

### FIXED CODE ###

import math
 
n=int(input())+1
ans=0
for i in range(1,n):
  for j in range(i,n):
    for k in range(j,n):
      if i==j and i==k:
        buff=1
      elif i==j:
        buff=3
      elif j==k:
        buff=3
      else:
        buff=6
      g1=math.gcd(i,j)
      g=math.gcd(k,g1)
      ans+=g*buff
print(ans)
### BUGGY CODE ###

from itertools import product
N,M=map(int,input().split())
A=list(map(int,input().split()))

S=sorted([sum(t) for t in product(A,A)],reverse=True)
print(sum(S[:M]))


### FIXED CODE ###

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

#和がx以上になる組み合わせの総数
def pattern_num_ge_x(x):
    p=0
    for a in A:
        ng=-1
        ok=N
        while (ok-ng)>1:
            m=(ok+ng)//2
            if a+A[m]>=x:
                ok=m
            else:
                ng=m
        p+=N-ok
    return(p)

#pattern_num_ge_x(x)がM未満になる最小のxを探す(=r)
#lはM以上になる最大のx
l=-1
r=10**10+1
while r-l>1:
    m=(r+l)//2
    if pattern_num_ge_x(m)<M:
        r=m
    else:
        l=m
#print(l,r)
#for i in range(300):
#    print(i,pattern_num_ge_x(i),sep=',')

#和がr以上になる組み合わせの総和を求める(累積和を使用)
S=list(reversed(A))
for i in range(1,N):
    S[i]+=S[i-1]
#print(S)
ans=0
for a in A:
    ng=-1
    ok=N
    while (ok-ng)>1:
        m=(ok+ng)//2
        if a+A[m]>=r:
            ok=m
        else:
            ng=m
    ans+=a*(N-ok) + (S[N-ok-1] if N-ok-1>=0 else 0)
    M-=(N-ok)
    #print(a,ok,a*(N-ok),ans)

#回数のあまり分lを足す
ans+=l*M

print(ans)
### BUGGY CODE ###

n, p = map(int, input().split())
s = input()

ans = 0
mod = [0 for i in range(p)]
mod[0] = 1

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    s = int(s)
    for i in range(1, n+1):
        m = s % (10**i) % p
        ans += mod[m]
        mod[m] += 1

print(ans)


### FIXED CODE ###

n, p = map(int, input().split())
s = input()

ans = 0
mod = [-1 for i in range(p)]
mod[0] = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    mp = 0
    m10 = 1
    for i in range(n-1, -1, -1):
        m10 = (10 * m10) % p
        mp = (mp + int(s[i]) * m10) % p
        mod[mp] += 1

    for m in mod:
        ans += m*(m+1)//2

print(ans)
### BUGGY CODE ###

import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())

n = ni()

a = []
for i in range(n):
    tmp = ni()
    if tmp in a:
        a.pop(a.index(tmp))
    else:
        a.append(tmp)
print(len(a))


### FIXED CODE ###

import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())

n = ni()

a = [ni() for _ in range(n)]
c = collections.Counter(a).values()
ans = 0
for t in c:
    if t % 2 != 0:
        ans += 1
print(ans)
### BUGGY CODE ###

S = input()

dp = [[0 for j in range(13)] for i in range(len(S)+1)]
dp[0][0] = 1

for i in range(len(S)):
    if S[-i-1] == "?":
        for k in range(10):
            r = (k*10**i)%13
            for j in range(13):
                dp[i+1][(r+j)%13] += dp[i][j]
    else:
        r = (int(S[-i-1])*10**i)%13
        for j in range(13):
            dp[i+1][(r+j)%13] += dp[i][j]

ans = dp[-1][5] % (10**9+7)
print(ans)

### FIXED CODE ###

s=input()
l=len(s)
mod=10**9+7
dp=[[0 for j in range(13)] for i in range(l+1)]
dp[0][0]=1
for i in range(1,l+1):
  if s[i-1]=='?':
    for j in range(10):
      for k in range(13):
        dp[i][(k*10+j)%13]+=dp[i-1][k]%mod
  else:
    for k in range(13):
      dp[i][(k*10+int(s[i-1]))%13]+=dp[i-1][k]%mod
print(dp[-1][5]%mod)
### BUGGY CODE ###

# coding: utf-8
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
P = lr()
dp = [[min(i, N-1-i)] * N for i in range(N)]
people = [[True] * N for i in range(N)]
for j in range(N):
    x = min(j, N-1-j)
    for i in range(N):
        dp[i][j] = min(dp[i][j], x)

answer = 0
for p in P:
    p -= 1
    i = p // N
    j = p % N
    answer += dp[i][j]
    dp[i][j] -= 1
    people[i][j] = False
    def bfs(sy, sx):
        que = deque([(sy, sx)])
        popleft = que.popleft; append = que.append
        while que:
            y, x = popleft()
            cur = dp[y][x]
            for dy, dx in zip([0, 1, 0, -1], [1, 0, -1, 0]):
                ny = y + dy
                nx = x + dx
                if 0 <= ny < N and 0 <= nx < N:
                    if dp[ny][nx] > cur + 1:
                        dp[ny][nx] = cur + 1
                        if not people[ny][nx]:
                            dp[ny][nx] = cur
                        append((ny, nx))
                    elif dp[ny][nx] > cur and (not people[ny][nx]):
                        # 人がいない時
                        dp[ny][nx] = cur
                        append((ny, nx))
    bfs(i, j)

print(answer)


### FIXED CODE ###

# coding: utf-8
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def main():
    N = ir()
    P = lr()
    P = [((p-1)//N, (p-1)%N) for p in P]
    dp = [[min(i, N-1-i, j, N-1-j) for j in range(N)] for i in range(N)]
    people = [[True] * N for i in range(N)]

    answer = 0
    for i, j in P:
        answer += dp[i][j]
        people[i][j] = False
        que = deque([(i, j, dp[i][j])])
        popleft = que.popleft; append = que.append
        while que:
            y, x, cur = popleft()
            cur += people[y][x]
            if x > 0:
                if dp[y][x-1] > cur:
                    dp[y][x-1] = cur
                    append((y, x-1, cur))
            if x < N-1:
                if dp[y][x+1] > cur:
                    dp[y][x+1] = cur
                    append((y, x+1, cur))
            if y > 0:
                if dp[y-1][x] > cur:
                    dp[y-1][x] = cur
                    append((y-1, x, cur))
            if y < N-1:
                if dp[y+1][x] > cur:
                    dp[y+1][x] = cur
                    append((y+1, x, cur))

    print(answer)
    
if __name__ == "__main__":
    main()
### BUGGY CODE ###

from math import floor
A,B,N=map(int,input().split())

x=N
result=[]
maxr=0
for x in reversed(range(1,N+1)):
    ans=floor(A*x/B)-A*floor(x/B)
    result.append(ans)
    if len(result) == 1:
        maxr = ans 
    if len(result) > 1 and max(result) == maxr:
        pass

print(max(result))


### FIXED CODE ###

from math import floor
A,B,N=map(int,input().split())

x=min(B-1,N)

print(floor(A*x/B)-A*floor(x/B))
### BUGGY CODE ###

A,B,K = map(int, input().split())
if A - B > 2 * K:
  [print(i) for i in range(A,A+K)]
  [print(i) for i in range(B+1-K,B+1)]
else:
  #print('---')
  [print(i) for i in range(A,B+1) if i - K < A or i + K > B]

### FIXED CODE ###

A,B,K = map(int, input().split())
if B-A > 2 * K:
  [print(i) for i in range(A,A+K)]
  [print(i) for i in range(B+1-K,B+1)]
else:
  [print(i) for i in range(A,B+1) if i - K < A or i + K > B]
### BUGGY CODE ###

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

def main():
     n = int(input())
     s = input()

     r_list = []
     g_list = []
     b_list = []

     ans = 0

     for i in range(n):
          if s[i] == 'R':
               r_list.append(i)
          elif s[i] == 'G':
               g_list.append(i)
          else:
               b_list.append(i)
     
     for r in r_list:
          for g in g_list:
               for b in b_list:
                    target = sorted([r, g, b])
                    if target[1] - target[0] != target[2] - target[1]:
                         ans += 1

     print(ans)

if __name__ == "__main__":
    main()


### FIXED CODE ###

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

import queue

def main():
     n = int(input())
     s = input()

     r_list = []
     g_list = []
     b_list = []

     ans = 0

     for i in range(n):
          if s[i] == 'R':
               r_list.append(i)
          elif s[i] == 'G':
               g_list.append(i)
          else:
               b_list.append(i)

     for i in range(n):
          if s[i] == 'R':
               ans += check(i, g_list, b_list)

          if s[i] == 'G':
               ans += check(i, r_list, b_list)

          if s[i] == 'B':
               ans += check(i, r_list, g_list)

     print(ans)

def check(spot, first, second):
     ret = 0
     small_first = []
     big_first = []
     small_second = []
     big_second = []

     for f in first:
          if f < spot: small_first.append(spot - f)
          else: big_first.append(f - spot)

     for s in second:
          if s < spot: small_second.append(spot - s)
          else: big_second.append(s - spot)

     return len(small_first) * len(big_second) + len(small_second) * len(big_first) - len(set(small_first) & set(big_second)) - len(set(small_second) & set(big_first))
     
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


h,w = map(int, input().split())
ch,cw = map(int, input().split())
dh,dw = map(int, input().split())
ch -= 1
cw -= 1
dh -= 1
dw -= 1
rows,cols = h,w

OK = "."
NG = "#"

from collections import defaultdict

ns = defaultdict(set)
ss = [None] * rows
for i in range(rows):
    s = input()
    ss[i] = s
n = rows * cols
start = ch*w+cw
goal = dh*w+dw
seen = [None]*n
seen[start] = 0
from heapq import heappop as hpp, heappush as hp
q = [(0, start)] # ワープ回数, 現在位置, 最後の道の位置
# dx = [0,1,0,-1]
# dy = [1,0,-1,0]
dx = []
dy = []
vvs = []
for i in range(-2,3):
    for j in range(-2,3):
        if not (i==j==0):
            dx.append(i)
            dy.append(j)
            if abs(i)+abs(j)>1:
                vvs.append(1)
            else:
                vvs.append(0)
while q:
    pnum,pu = hpp(q)
    ux,uy = divmod(pu,w)
    # print(pnum,divmod(pu,w))
    if pu==goal:
        break
    for xx,yy,vv in zip(dx,dy,vvs):
#         x = ux+xx
#         y = uy+yy
        x,y = ux+xx, uy+yy
        u = x*w + y
        num = pnum+vv
        if x<0 or y<0 or x>=h or y>=w or ss[x][y]=="#":
            continue
#         print(x,y)
        if seen[u] is None or seen[u]>num:
            seen[u] = num
            hp(q, (num, u))
val = seen[goal]
if val is None:
    print(-1)
else:
    print(val)

### FIXED CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


h,w = map(int, input().split())
ch,cw = map(int, input().split())
dh,dw = map(int, input().split())
ch -= 1
cw -= 1
dh -= 1
dw -= 1
rows,cols = h,w

OK = "."
NG = "#"

from collections import defaultdict

ns = defaultdict(set)
ss = [None] * rows
for i in range(rows):
    s = input()
    ss[i] = s
n = rows * cols
start = ch*w+cw
goal = dh*w+dw
seen = [None]*n
seen[start] = 0
from heapq import heappop as hpp, heappush as hp
q = [(0, start)] # ワープ回数, 現在位置, 最後の道の位置
# dx = [0,1,0,-1]
# dy = [1,0,-1,0]
dx = []
dy = []
vvs = []
for i in range(-2,3):
    for j in range(-2,3):
        if not (i==j==0):
            dx.append(i)
            dy.append(j)
            if abs(i)+abs(j)>1:
                vvs.append(1)
            else:
                vvs.append(0)
while q:
    pnum,pu = hpp(q)
    ux,uy = divmod(pu,w)
    # print(pnum,divmod(pu,w))
    if pu==goal:
        break
    for xx,yy,vv in zip(dx,dy,vvs):
#         x = ux+xx
#         y = uy+yy
        x,y = ux+xx, uy+yy
        u = x*w + y
        num = pnum+vv
        if x<0 or y<0 or x>=h or y>=w or ss[x][y]=="#":
            continue
#         print(x,y)
        if seen[u] is None or seen[u]>num:
            seen[u] = num
            hp(q, (num, u))
val = seen[goal]
if val is None:
    print(-1)
else:
    print(val)
### BUGGY CODE ###

from itertools import product

R, G, B, N = map(int, input().split())
colors = sorted([(N // R, R), (N // G, G), (N // B, B)])
ans = 0
for i, j in product(range(colors[1][0]+1), range(colors[2][0]+1)):
    I = colors[1][1]; J = colors[2][1]; 
    if N >= i*I + j*J and (N - (i*I + j*J)) % colors[0][1] == 0: ans += 1
print(ans)

### FIXED CODE ###

from itertools import product

R, G, B, N = map(int, input().split())
R_max = N // R
G_max = N // G
ans = 0
for r, g in product(range(R_max+1), range(G_max+1)):
    if N >= r*R + g*G and (N - (r*R + g*G)) % B == 0: ans += 1
print(ans)
### BUGGY CODE ###

a=int(input())
s=input()
count=0
x=[]
y=[]
z=[]
for i in range(a):
    if s[i]=="R":
        x+=[i]
    elif s[i]=="B":
        y+=[i]
    else:
        z+=[i]
for i in x:
    for j in y:
        p=2*i-j
        q=2*j-i
        r=(i+j)/2
        t=0
        if p in z:
            t+=1
        if q in z:
            t+=1
        if r in z:
            t+=1
        count+=len(z)-t
print(count)

### FIXED CODE ###

a=int(input())
s=input()
ans=s.count("R")*s.count("G")*s.count("B")
for i in range(a):
  for j in range(i+1,(a+i+1)//2):
    if s[i]!=s[j] and s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
      ans-=1
print(ans)
### BUGGY CODE ###

N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(M)]
B = {x for x in range(A[0][0], A[0][1]+1)}
for i in range(1,M):
  C = {x for x in range(A[i][0],A[i][1]+1)}
  B = B & C
ans = len(B)
print(ans)
  
  


### FIXED CODE ###

N, M = map(int, input().split())
A = [0]*M
B = [0]*M
for i in range(M):
  A[i], B[i] = map(int, input().split())
a = max(A)
b = min(B)
if a <= b:
  print(b+1-a)
else:
  print(0)
### BUGGY CODE ###

n,m=map(int,raw_input().split())
d={}
for i in xrange(m):
	x,y=map(int,raw_input().split())
	if x in d:
		d[x].append(y)
	else:
		d[x]=[y]
	if y in d:
		d[y].append(x)
	else:
		d[y]=[x]

parent=[0]*(n+1)
bfs=[1]
visited=set([1])
while len(bfs)!=0:
	x=bfs.pop(0)
	visited.add(x)
	for i in d[x]:
		if i not in visited:
			bfs.append(i)
			parent[i]=x
			visited.add(i)


if len(visited)==n:
	print('Yes')
	for i in xrange(2,n+1):
		print(parent[i])
else:
	print('No')




### FIXED CODE ###

n,m=map(int,input().split())
d={}
for i in range(m):
	x,y=map(int,input().split())
	if x in d:
		d[x].append(y)
	else:
		d[x]=[y]
	if y in d:
		d[y].append(x)
	else:
		d[y]=[x]

parent=[0]*(n+1)
bfs=[1]
visited=set([1])
while len(bfs)!=0:
	x=bfs.pop(0)
	visited.add(x)
	for i in d[x]:
		if i not in visited:
			bfs.append(i)
			parent[i]=x
			visited.add(i)


if len(visited)==n:
	print('Yes')
	for i in range(2,n+1):
		print(parent[i])
else:
	print('No')
### BUGGY CODE ###

n = int(input())
s = 0
lst = []
for _ in range(n):
  tmp = int(input())
  if tmp in lst:
    s -= 1
    lst.remove(tmp)
  else:
    s += 1
    lst.append(tmp)
print(s)

### FIXED CODE ###

n = int(input())
dic = {}
for _ in range(n):
  tmp = int(input())
  if tmp in dic and dic[tmp] == 1:
    dic[tmp] = 0
  else:
    dic[tmp] = 1
print(sum(dic.values()))
### BUGGY CODE ###

import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = [list(map(int, readline().split())) for _ in range(n)]
    for i in range(n):
        for j in range(n - 1):
            a[i][j] -= 1
    ls = [0] * n
    ql = collections.deque()
    for i in range(n):
        ql.append([0, i])

    for i in range(10 ** 10):
        fq = [True] * n
        while True:
            if len(ql) != 0:
                d, ps = ql.popleft()
                if d == i:
                    if fq[ps]:
                        nw = a[ps][ls[ps]]
                        if fq[nw]:
                            rw = a[nw][ls[nw]]
                            if ps == rw:
                                ls[ps] += 1
                                ls[nw] += 1
                                if ls[ps] < n - 1:
                                    ql.append([d+1, ps])
                                if ls[nw] < n - 1:
                                    ql.append([d+1, nw])
                                fq[nw] = False
                                fq[ps] = False
                else:
                    ql.appendleft([d, ps])
                    break
            else:
                if sum(ls) == n * (n - 1):
                    print(i + 1)
                else:
                    print(-1)
                exit()


if __name__ == '__main__':
    solve()


### FIXED CODE ###

def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    ad = [0] * n
    dq = collections.deque([[0, i + 1] for i in range(n)])
    for i in range(10 ** 15):
        fd = {}
        while True:
            if len(dq) != 0:
                d, q = dq.popleft()
                q -= 1
                if d <= i:
                    if ad[q] < n - 1:
                        qq = a[q][ad[q]] - 1
                        tq = a[qq][ad[qq]] - 1
                        if q == tq and qq not in fd and tq not in fd:
                            ad[qq] += 1
                            ad[tq] += 1
                            if ad[qq] < n - 1:
                                dq.append([d + 1, qq + 1])
                            if ad[tq] < n - 1:
                                dq.append([d + 1, tq + 1])
                            fd[qq] = 0
                            fd[tq] = 0
                else:
                    dq.append([d, q + 1])
                    break
            else:
                break
        if len(dq) == 0:
            b = True
            for v in ad:
                if v != n - 1:
                    b = False
                    break
            if b:
                print(i + 1)
                exit()
            else:
                print(-1)
                exit()


if __name__ == '__main__':
    slove()