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

a=raw_input()
ary=list(a)
count = 0

def countStr(arys):
  cW=0
  cB=0
  for i in arys:
    if i == "B":
      cB += 1
    else:
      cW += 1
  return cW, cB

while len(set(ary)) != 1:
  count += 1
  #W, B=countStr(ary)

  if ary[len(ary)-1] == "W":
    ary.append("B")
    m = 2
    for i in ary:
      if ary[len(ary)-m] == "W":
        ary[len(ary)-m] = "B"
        m += 1 
      else:
        break
  else:
    ary.append("W")
    m=2
    for i in ary:
      if ary[len(ary)-m] == "B":
        ary[len(ary)-m] = "W"
        m += 1
      else:
        break
  
print count

### FIXED CODE ###

a=raw_input()
ary=list(a)
count = 0

tmp=ary[0]
for i,v in enumerate(ary):
  if v == tmp:
    tmp=v
  else:
    tmp=v
    count += 1

print count
### BUGGY CODE ###

from bisect import insort_left 

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

for i in range(M):
    x = A.pop()
    insort_left(A,x//2)

print(sum(A))


### FIXED CODE ###

import heapq

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

A = list(map(lambda x:-x,A))

heapq.heapify(A)

for i in range(M):
    x = heapq.heappop(A)
    heapq.heappush(A,x/2)

ans = 0

for i in range(N):
    x = heapq.heappop(A)
    ans += int(-x)

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


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

tmplist = []
T = 0
for i in range(Q):
    Query = list(input().split())
    if len(Query) == 1:
        T = T+1
    else:
        if T%2 == 0:
            if Query[1] == '1':
                S.insert(0,Query[2])
            else:
                S.append(Query[2])
        else:
            if Query[1] == '2':
                S.insert(0,Query[2])
            else:
                S.append(Query[2])
#    print("len(Query)={}, Query={}, T={}, S={}".format(len(Query), Query,T,''.join(S)))

if T%2 == 1:
    #S.reverse()
    S=S[::-1]

#print("ans={}".format(''.join(S)))
print(''.join(S))


### FIXED CODE ###

# キューを使ったアプローチ

from collections import deque

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

T = 0
for _ in range(Q):
    Query = list(input().split())
    if len(Query) == 1:
        T ^= 1
    elif T == 0:
        m = Query[2]
        if Query[1] == '1':
            S.appendleft(m)
        else:
            S.append(m)
    else:
        m = Query[2]
        if Query[1] == '2':
            S.appendleft(m)
        else:
            S.append(m)

if T==1:
    S.reverse()

print(''.join(S))
### BUGGY CODE ###

def resolve():
    '''
    code here
    '''
    import collections
    N, M, K = [int(item) for item in input().split()]
    As = [int(item) for item in input().split()]
    Bs = [int(item) for item in input().split()]

    res = 0
    time = 0

    memo_A = [0]
    for i in range(N):
        memo_A.append(As[i]+memo_A[-1])

    memo_B = [0]
    for i in range(M):
        memo_B.append(Bs[i]+memo_B[-1])

    temp_max = 0
    for i, av in enumerate(memo_A):
        for j, bv in enumerate(memo_B):
            if av + bv <= K:
                temp_max = max(temp_max, i + j)
            else:
                break
    print(temp_max)

if __name__ == "__main__":
    resolve()


### FIXED CODE ###

def resolve():
    '''
    code here
    '''

    N, M, K = [int(item) for item in input().split()]
    As = [int(item) for item in input().split()]
    Bs = [int(item) for item in input().split()]

    res = 0
    time = 0

    memo_A = [0]
    for i in range(N):
        memo_A.append(As[i]+memo_A[-1])

    memo_B = [0]
    for i in range(M):
        memo_B.append(Bs[i]+memo_B[-1])

    res = 0
    j = M
    for i, av in enumerate(memo_A):
        if av > K:
            break
        else:
            
            while av + memo_B[j] > K:
                j -= 1
            res = max(res,j+i)

    print(res)

if __name__ == "__main__":
    resolve()
### BUGGY CODE ###

n = int(input())
h = list(map(int,input().split()))
dp = [10001] * n 
dp[0] = 0
dp[1] = abs(h[1] - h[0])
a = [[-1] * n for i in range(n)]

for i in range(2, n):
    if a[i - 2][i] == -1:
        a[i][i - 2] = a[i - 2][i] = abs(h[i - 2] - h[i])
    if a[i - 1][i] == -1:
        a[i][i - 1] = a[i - 1][i] = abs(h[i - 1] - h[i])
    
    dp[i] = min(dp[i - 2] + a[i - 2][i], dp[i - 1] + a[i - 1][i])
print(dp[n - 1])




### FIXED CODE ###

n = int(input())
h = list(map(int,input().split()))
INF = float('inf')
dp = [INF] * n 
dp[0] = 0
dp[1] = abs(h[1] - h[0])

def chmin(a, b):
    if a <= b:
        return a
    else:
        return b 
    

for i in range(2, n):

    dp[i] = chmin(dp[i], dp[i - 2] + abs(h[i - 2] - h[i]))
    dp[i] = chmin(dp[i], dp[i - 1] + abs(h[i - 1] - h[i]))
    
print(dp[n - 1])
### BUGGY CODE ###

n, m = (int(x) for x in input().split())
x = [int(x) for x in input().split()]
if n >= m:
    print(0)
else:
    x.sort()
    dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
    for _ in range(n-1): dis.remove(max(dis))
    print(sum(dis))

### FIXED CODE ###

n, m = (int(x) for x in input().split())
x = [int(x) for x in input().split()]
if n >= m:
    print(0)
else:
    x.sort()
    dis = [abs(x[i+1] - x[i]) for i in range(m-1)]
    dis.sort()
    ans = dis[::-1]
    for _ in range(n-1): ans.pop(0)
    print(sum(ans))
### BUGGY CODE ###

import copy
import math
import string

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

is_reverse = False
for i in range(Q):
    A = list(map(str,input().split()))
    A[0] = int(A[0])
    if A[0] == 1:
        is_reverse = (not is_reverse)
    elif A[0] == 2:
        A[1] = int(A[1])
        if A[1] == 1:
            if is_reverse:
                S += A[2]
            else:
                S = A[2] + S
        else:
            if is_reverse:
                S = A[2] + S
            else:
                S += A[2]
if is_reverse:
    S = S[::-1]
print(S)


### FIXED CODE ###

import copy
import math
import string

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

is_reverse = False
front = ""
back = ""
for i in range(Q):
    A = list(map(str,input().split()))
    A[0] = int(A[0])
    if A[0] == 1:
        is_reverse = (not is_reverse)
    elif A[0] == 2:
        A[1] = int(A[1])
        if A[1] == 1:
            if is_reverse:
                back += A[2]
            else:
                front += A[2]
        else:
            if is_reverse:
                front += A[2]
            else:
                back += A[2]
front = front[::-1]
S = front + S + back
if is_reverse:
    S = S[::-1]
print(S)
### BUGGY CODE ###

N = int(input())
S = input()

C = [0]*3
for i in range(N):
    c = "RGB".index(S[i])
    C[c]+=1

K = 0
for i in range(N-2):
    for j in range(i,N):
        k = 2*j-i
        if k>=N:break
        if len(set([S[i],S[j],S[k]]))==3:
            K+=1
print(C[0]*C[1]*C[2]-K)

### FIXED CODE ###

N = int(input())
S = input()

C = [0]*3
for i in range(N):
    c = "RGB".index(S[i])
    C[c]+=1

K = 0
for i in range(N-2):
    for j in range(i,N):
        k = 2*j-i
        if k>=N:break
        if len(set([S[i],S[j],S[k]]))==3:
            K+=1
print(C[0]*C[1]*C[2]-K)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)

def main():
    N,M,K = [int(i) for i in input().strip().split()]
    A = [int(i) for i in input().strip().split()]
    B = [int(i) for i in input().strip().split()]

    pre_A = [0] * (N + 1)
    pre_B = [0] * (M + 1)


    for i in range(1,N+1):
        pre_A[i] = pre_A[i-1] + A[i-1]
        if pre_A[i] > K:
            idx_a = i-1
            break
    else:
        idx_a = N

    for i in range(1,M+1):
        pre_B[i]= pre_B[i-1] + B[i-1]
        if pre_B[i] > K:
            idx_b = i-1
            break
    else:
        idx_b = M

    ans = max(idx_a,idx_b)

    for i in range(idx_a + 1):
        for j in range(idx_b+1): 
            if pre_A[i] + pre_B[j] <= K:
                ans = max(ans,i+j)
            else:
                break

    return ans

if __name__ == "__main__":
    print(main())


### FIXED CODE ###

import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)

def main():
    N,M,K = [int(i) for i in input().strip().split()]
    A = [int(i) for i in input().strip().split()]
    B = [int(i) for i in input().strip().split()]

    pre_A = [0] * (N + 1)
    pre_B = [0] * (M + 1)


    for i in range(1,N+1):
        pre_A[i] = pre_A[i-1] + A[i-1]
        if pre_A[i] > K:
            idx_a = i-1
            break
    else:
        idx_a = N

    for i in range(1,M+1):
        pre_B[i]= pre_B[i-1] + B[i-1]
        if pre_B[i] > K:
            idx_b = i-1
            break
    else:
        idx_b = M

    ans = max(idx_a,idx_b)

    j = idx_b
    for i in range(idx_a + 1):
        while pre_B[j] > K - pre_A[i]:
            j -= 1
        ans = max(ans,i+j)

    return ans

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

s = input()
mini = s
maxi = s
for i in range(len(s)):
    if s[i] == "?":
        if i != 0 or i!= len(s) - 1:
            mini = mini[:i] + "0" + mini[i+1:]
            maxi = maxi[:i] + "9" + maxi[i+1:]
        elif i == 0:
            mini = "0" + mini[1:]
            maxi = "9" + maxi[1:]
        elif i == len(s) - 1:
            mini = mini[:len(s)-1] + "0"
            maxi = maxi[:len(s)-1] + "9"
        
    
mini = int(mini)
maxi = int(maxi)

count = 0
for i in range(mini,maxi+1):
    if i % 13 == 5:
        flag = True
        i_str = str(i)
        if len(i_str) < len(s):
            i_str = "0" * (len(s) - len(i_str)) + i_str
        for j in range(len(s)):
            if s[j] == "?" or i_str[j] == s[j]:
                continue
            else:
                flag = False
                break
        if flag == True:
            count += 1
print(count % (10**9 + 7))


### FIXED CODE ###

s = input()
dp = [[0 for i in range(13)]]
if s[-1] != "?":
    dp[0][int(s[-1]) % 13] += 1
else:
    for k in range(10):
        dp[0][k % 13] += 1
        
d = [1]   
for i in range(1,len(s)):
    dp.append([0]*13)
    d.append(d[i-1]*10 % 13)
    for r in range(13):
        if s[-(i+1)] == "?":
            for k in range(10): 
                dp[i][(k * d[i] + r) % 13] += dp[i-1][r] % (10**9 + 7)
                
        else:
            dp[i][(int(s[-(i+1)]) * d[i] + r) % 13] += dp[i-1][r] % (10**9 + 7)

print(dp[len(s)-1][5]%(10**9+7))
### BUGGY CODE ###

n=int(input())
p=1
for i in range(1,n+1):
    p=p*i
print(p%(10**9+7))

### FIXED CODE ###

n=int(input())
p=1
for i in range(1,n+1):
    p=(p*i)%(10**9+7)
print(p)
### BUGGY CODE ###

def solve():
	n, m = map(int, raw_input().split())
	arr = [[0 for i in range(n+1)] for j in range(n+1)]

	for i in range(m):
		x, y = map(int, raw_input().split())
		arr[x][y] = arr[y][x] = 1

	for i in range(n):
		if arr[1][i] == 1 and arr[n][i] == 1:
			print "POSSIBLE"
			return
			
	print "IMPOSSIBLE"

solve()

### FIXED CODE ###

from sets import Set

def solve():
	n, m = map(int, raw_input().split())
	to1 = Set()
	toN = Set()

	for i in range(m):
		x, y = map(int, raw_input().split())
		if x == 1:
			to1.add(y)
		if x == n:
			toN.add(y)
		if y == 1:
			to1.add(x)
		if y == n:
			toN.add(x)

	for i in range(n):
		if i in to1 and i in toN:
			print "POSSIBLE"
			return
			
	print "IMPOSSIBLE"

solve()
### BUGGY CODE ###

#!/usr/bin/env python3
import sys


def solve(N: int, K: int, p: "List[int]"):
    p2 = [(i + 1) / 2 for i in p]
    m = -1
    for i in range(N - (K - 1)):
        s = 0
        for j in range(K):
            s += p2[i + j]
        m = max(m, s)
    print(m)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, p)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys


def solve(N: int, K: int, p: "List[int]"):
    p2 = [(i + 1) / 2 for i in p]
    l = []
    for i in range(N - (K - 1)):
        if i == 0:
            s = 0
            for j in range(K):
                s += p2[j]
            l.append(s)
        else:
            l.append(l[-1] - p2[i - 1] + p2[i + (K - 1)])
    print(max(l))
    # m = -1
    # for i in range(N - (K - 1)):
    #     s = 0
    #     for j in range(K):
    #         s += p2[i + j]
    #     m = max(m, s)
    # print(m)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, p)

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

s = input()
n = len(s)
t = ''
for i in range(n):
  if s[i] == 'P':
    t += 'P'
  else:
    t += 'D'
print(t)

### FIXED CODE ###

s = input()
n = len(s)
t = ''
for i in range(n):
  if s[i] == 'P':
    t += 'P'
  else:
    t += 'D'
print(t)
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
count = [0] * N
for i in range(N):
  count[i]  = A.count(i + 1)
  
nanimo = 0 
for i in range(N):
  nanimo += count[i] * (count[i] - 1) // 2
for i in range(N):
  tekito = count[A[i] - 1]
  if tekito == 2:
    s = nanimo - 1
  elif tekito == 1:
    s = nanimo
  else:
    s = nanimo -  ((tekito * (tekito - 1) //2) - ((tekito - 1) * (tekito - 2) //2))
  print(s)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
count = [0] * N
for i in range(N):
  count[A[i] - 1]  += 1
  
nanimo = 0 
for i in range(N):
  nanimo += count[i] * (count[i] - 1) // 2
for i in range(N):
  tekito = count[A[i] - 1]
  if tekito == 2:
    s = nanimo - 1
  elif tekito == 1:
    s = nanimo
  else:
    s = nanimo -  ((tekito * (tekito - 1) //2) - ((tekito - 1) * (tekito - 2) //2))
  print(s)
### BUGGY CODE ###

import numpy as np
s = list(map(int,input()))
n = len(s)
ans = 0
mod = 2019

for i in range(n-3):
    nn = (s[i]*1000+s[i+1]*100+s[i+2]*10+s[i+3])%mod
    if nn == 0:
        ans += 1
    for j in range(i+4,n):
        nn = (nn*10+s[j])%mod
        if nn == 0:
            ans += 1
print(ans)


### FIXED CODE ###

import numpy as np
s = list(map(int,input()))
n = len(s)
ans = 0
mod = 2019
past = s[n-1]
ima = 0
d = dict()
d[s[n-1]] = 1
now = 10
for i in range(1,n):
    ima = (past+s[n-i-1]*now)%mod
    if ima == 0:
        ans += 1
    if ima in d:
        d[ima] += 1
    else:
        d[ima] = 1
    past = ima
    now = (now*10)%mod

for i in d.values():
    ans += i*(i-1)//2

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

N, L, T = [int(i) for i in input().split()]

ants = list(map(lambda x: [x[0] * 2, x[1]], [[int(i) for i in input().split()] for j in range(N)]))

for i in range(T * 2):
	for j in range(N):
		if ants[j][1] == 1:
			if j != N - 1:
				if ants[j][0] == 2 * L - 1:
					if ants[j + 1][0] == 1 and ants[j + 1][1] == 2:
						ants[j][0] = 0
						ants[j][1] = 2
						ants[j + 1][0] = 2 * L - 1
						ants[j + 1][1] = 1
					else:
						ants[j][0] = 0
				elif ants[j][0] == 2 * L - 2:
					if ants[j + 1][0] == 0 and ants[j + 1][1] == 2:
						ants[j][0] = 2 * L - 1
						ants[j][1] = 2
						ants[j + 1][0] = 2 * L - 2
						ants[j + 1][1] = 1
					else:
						ants[j][0] = 2 * L - 1
				else:
					if ants[j + 1][0] == ants[j][0] + 2 and ants[j + 1][1] == 2:
						ants[j][0] += 1
						ants[j][1] = 2
						ants[j + 1][0] -= 2
						ants[j + 1][1] = 1
					else:
						ants[j][0] += 1
			else:
				if ants[j][0] == 2 * L - 1:
					ants[j][0] = 0
				else:
					ants[j][0] += 1
				if ants[0][0] == ants[j][0]:
					ants[0][1] = 1
					ants[j][1] = 2
		else:
			if ants[j][0] == 0:
				ants[j][0] = 2 * L - 1
			else:
				ants[j][0] -= 1

for i in range(N):
	print(ants[i][0] // 2)

### FIXED CODE ###

N, L, T = map(int, input().split())
X = []
W = []
for _ in range(N):
    x, w = map(int, input().split())
    X.append(x)
    if w == 2:
        w = -1
    W.append(w)

C = 0
for i in range(1, N):
    if W[i] != W[0]:
        C += T // (L / 2)
        if W[0] == 1:
            D = X[i] - X[0]
        else:
            D = X[0] + L - X[i]
        if T % (L / 2) * 2 >= D:
            C += 1

for i in range(N):
    X[i] = (X[i] + T * W[i]) % L

x = X[0]

X.sort()

ng = -1
ok = N
while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    if X[mid] >= x:
        ok = mid
    else:
        ng = mid
if W[0] == 1 and ok < N-1:
    if X[ok] == X[ok+1]:
        ok += 1

start = int((ok - C * W[0]) % N)
for i in X[start:]:
    print(i)
for i in X[:start]:
    print(i)
### BUGGY CODE ###

class UnionFind():
  def __init__(self, n):
    self.n = n
    self.parents = [-1] * n

  def find(self, x):
    if self.parents[x] < 0:
      return x
    else:
      self.parents[x] = self.find(self.parents[x])
    return self.parents[x]

  def union(self, x, y):
    x = self.find(x)
    y = self.find(y)

    if x == y:
      return

    if self.parents[x] > self.parents[y]:
          x, y = y, x

    self.parents[x] += self.parents[y]
    self.parents[y] = x

  def same(self, x, y):
    return self.find(x) == self.find(y)

  def roots(self):
    return [i for i, x in enumerate(self.parents) if x < 0]

  def num_roots(self):
    return len([i for i, x in enumerate(self.parents) if x < 0])

  def members(self, x):
    root = self.find(x)
    return [i for i in range(self.n) if self.find(i) == root]

  def num_members(self,x):
    return abs(self.parents[self.find(x)])

  def __str__(self):
    return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def solve():
  ans = 0
  N, M = map(int, input().split())
  A = list(map(int, input().split()))
  uf = UnionFind(N)
  for _ in range(M):
    x,y = map(int, input().split())
    uf.union(x,y)
  if N>2*(M+1):
    return 'Impossible'
  if N-M==1:
    return 0
  roots = uf.roots()
  lis1 = []
  for i,r in enumerate(roots):
    lis = uf.members(r)
    for j in range(len(lis)):
      lis[j] = A[lis[j]]
    lis.sort(reverse=True)
    ans += lis.pop(-1)
    lis1.extend(lis)
  lis1.sort()
  ans += sum(lis1[:(N-M-2)])
  return ans
print(solve())

### FIXED CODE ###

class UnionFind():
  def __init__(self, n):
    self.n = n
    self.parents = [-1] * n

  def find(self, x):
    if self.parents[x] < 0:
      return x
    else:
      self.parents[x] = self.find(self.parents[x])
    return self.parents[x]

  def union(self, x, y):
    x = self.find(x)
    y = self.find(y)

    if x == y:
      return

    if self.parents[x] > self.parents[y]:
          x, y = y, x

    self.parents[x] += self.parents[y]
    self.parents[y] = x

  def same(self, x, y):
    return self.find(x) == self.find(y)

  def roots(self):
    return [i for i, x in enumerate(self.parents) if x < 0]

  def num_roots(self):
    return len([i for i, x in enumerate(self.parents) if x < 0])

  def members(self, x):
    root = self.find(x)
    return [i for i in range(self.n) if self.find(i) == root]

  def num_members(self,x):
    return abs(self.parents[self.find(x)])

  def __str__(self):
    return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
from bisect import *
from collections import defaultdict
def solve():
  ans = 0
  N, M = map(int, input().split())
  A = list(map(int, input().split()))
  uf = UnionFind(N)
  for _ in range(M):
    x,y = map(int, input().split())
    uf.union(x,y)
  if N>2*(M+1):
    return 'Impossible'
  if N-M==1:
    return 0
  d = defaultdict(lambda: [])
  for i in range(N):
    r = uf.find(i)
    if r<0:
      d[i].append(A[i])
    else:
      d[r].append(A[i])
  lis1 = []
  for v in d.values():
    mini = min(v)
    ans += mini
    v.remove(mini)
    for l in v:
      insort_right(lis1,l)
  ans += sum(lis1[:(N-M-2)])
  return ans
print(solve())
### BUGGY CODE ###

N, M = map(int, input().split())
H = list(map(int, input().split()))
AB = []
cnt = 0
for i in range(M):
  a, b = map(int, input().split())
  ab = sorted([a, b])
  if AB.count(ab) == 0:
    AB.append(ab)
  
for i in range(1, N+1):
  flag = True
  for ab in AB:
    if ab[0] == i:
      x = ab[1]
      if H[i - 1] <= H[x - 1]:
        flag = False
        break
    elif ab[1] == i:
      x = ab[0]
      if H[i - 1] <= H[x - 1]:
        flag = False
        break
  if flag:
    cnt += 1
      
print(cnt)

### FIXED CODE ###

N, M = map(int, input().split())
H = list(map(int, input().split()))
m = [0] * N
cnt = 0

for _ in range(M):
  a, b = map(int, input().split())
  m[a - 1] = max(m[a - 1], H[b - 1])
  m[b - 1] = max(m[b - 1], H[a - 1])

for i in range(N):
  if H[i] > m[i] or m[i] == 0:
    cnt += 1

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

n = int(input())
var = 0
res = []
for i in range(n):
    s = input()
    if s in res:
        continue
    res.append(s)
    var += 1

print(var)

### FIXED CODE ###

n = int(input())
res = set()
for i in range(n):
    s = input()
    res.add(s)
var = len(res)
print(var)
### BUGGY CODE ###

n = int(input())
table = [[] for _ in range(n+1)]
for i in range(1, n+1):
  a = list(map(int, input().split()))
  table[i] = a
cursor = [0]*(n+1)
is_matched = set()
d = 0
match = 0
while match < n * (n-1) //2:
  now_match = match
  for i in range(1, n):
    if cursor[i] < n-1 and i not in is_matched:
      j = table[i][cursor[i]]
      if i < j and j not in is_matched and table[j][cursor[j]] == i :
        match += 1
        cursor[i] += 1
        is_matched.add(i)
        cursor[j] += 1
        is_matched.add(j)
  if now_match == match:
    print(-1)
    exit()
  if d > 300000:
    print(499500)
    exit()  
  d += 1
  is_matched.clear()
print(d)

### FIXED CODE ###

n = int(input())
table = [[] for _ in range(n+1)]
for i in range(1, n+1):
  a = list(map(int, input().split()))
  table[i] = a
cursor = [0]*(n+1)
is_matched = set()
d = 0
match = 0
while match < n * (n-1) //2:
  now_match = match
  for i in range(1, n):
    if cursor[i] < n-1 and i not in is_matched:
      j = table[i][cursor[i]]
      if i < j and j not in is_matched and table[j][cursor[j]] == i :
        match += 1
        cursor[i] += 1
        is_matched.add(i)
        cursor[j] += 1
        is_matched.add(j)
  if now_match == match:
    print(-1)
    exit()
  if d > 30000:
    print(499500)
    exit()  
  d += 1
  is_matched.clear()
print(d)
### BUGGY CODE ###

def solve(N, W):
    dp = [0] * (W + 1)
    for item in range(1, N + 1):
        w, v = list(map(int, input().split()))
        for i in range(W, w - 1, -1):  #
            if v + dp[i - w] > dp[i]:
                dp[i] = v + dp[i - w]

    print(max(dp))


N, W = map(int, input().split())
solve(N, W)


### FIXED CODE ###


N,W=map(int,input().split())
dp=[0]*(W+1)
L = sorted([list(map(int,input().split())) for i in range(N)])
s=0
 
for w, v in L:
    s+=w
    for j in range(min(W,s),w-1,-1):
        if dp[j-w]+v > dp[j]: dp[j]=dp[j-w]+v
    
print(max(dp))
### BUGGY CODE ###

h,w=map(int,input().split())
grid=[None]*h
seen=[[-1 for i in range(w)] for j in range(h)]
from collections import deque
que=deque()
for i in range(h):
  grid[i]=input()
  for j in range(len(grid[i])):
    if grid[i][j]=="#":
      que.append([i,j,0])
      
ans=0
while que:
  node=que.popleft()
  y=node[0]
  x=node[1]
  p=node[2]
  if seen[y][x]==-1:
    seen[y][x]=p
  elif seen[y][x]<=p:
    continue
  else:
    seen[y][x]=p
  ans=p
  if y-1>=0 and seen[y-1][x]==-1:
    que.append([y-1,x,p+1])
  if y+1<h and seen[y+1][x]==-1:
    que.append([y+1,x,p+1])
  if x-1>=0 and seen[y][x-1]==-1:
    que.append([y,x-1,p+1])
  if x+1<w and seen[y][x+1]==-1:
    que.append([y,x+1,p+1])
print(ans)
    
    

### FIXED CODE ###

H,W=map(int,input().split())
INF=10**4
import numpy as np
a=np.full((H,W),INF,dtype=int)
for i in range(H):
  s=input()
  for j in range(len(s)):
    if s[j]=="#":
      a[i,j]=0

for i in range(0,W-1):
  np.minimum(a[:,i]+1,a[:,i+1],out=a[:,i+1])
for i in range(W-1,0,-1):
  np.minimum(a[:,i]+1,a[:,i-1],out=a[:,i-1])
for j in range(0,H-1):
  np.minimum(a[j,]+1,a[j+1,],out=a[j+1,])
for j in range(H-1,0,-1):
  np.minimum(a[j,]+1,a[j-1,],out=a[j-1,])

print(a.max())