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

K, S = map(int, input().split())
count = 0
for X in range(K + 1):
    if X > S:
        break
    for Y in range(min(K, S - X) + 1):
        if Y < 0:
            continue
        if X + Y > S:
            break
        Z = S - X - Y
        if X <= K and Y <= K and Z <= K:
            count += 1
print(count)



### FIXED CODE ###

K, S = map(int, input().split())
count = 0
for X in range(min(S + 1, K + 1)):
    for Y in range(min(S - X + 1, K + 1)):
        if S - X - Y <= K:
            count += 1
print(count)
### BUGGY CODE ###

n = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for i, l1 in enumerate(L):
    for k, l2 in enumerate(L[i+1:]):
        num = i + k + 2
        for l3 in L[num:]:
            if l1 < l2 + l3 and l2 < l1 + l3 and l3 < l2 + l1:
                count += 1
            else:
                break

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):
    for j in range(i+1, n-1):
        k = bisect.bisect_left(L[j+1:], L[i]+L[j])
        count += k

print(count)
### BUGGY CODE ###

N, K = map(int, input().split())
mod = 10**9 + 7

def com(n, r):
    p, q = 1, 1
    for i in range(r):
        p *= (n-i)%mod
        q *= (r-i)%mod
    return p*pow(q, mod-2, mod)%mod

for k in range(1, K+1):
    print(com(N-K+1, k)*com(K-1, k-1)%mod)

### FIXED CODE ###

N, K = map(int, input().split())
mod = 10**9 + 7

def com(n, r):
    p, q = 1, 1
    for i in range(r):
        p = p*(n-i)%mod
        q = q*(r-i)%mod
    return p*pow(q, mod-2, mod)%mod

for k in range(1, K+1):
    print(com(N-K+1, k)*com(K-1, k-1)%mod)
### BUGGY CODE ###


import sys

input = sys.stdin.readline

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

dp = [0] * (W + 1)

for _ in range(N):
    w, v = map(int, input().split())
    for wk in range(W, w-1, -1):
        tv = dp[wk - w] + v
        if tv > dp[wk]:
            dp[wk] = tv

print(max(dp))

### FIXED CODE ###


import sys

input = sys.stdin.readline

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

dp = [0] * (W + 1)
def knapsack(n, w):
    for _ in range(N):
        w, v = map(int, input().split())
        for wk in range(W, w-1, -1):
            tv = dp[wk - w] + v
            if tv > dp[wk]:
                dp[wk] = tv
    return dp[-1]

print(knapsack(N, W))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n=int(input())
a=list(map(int,input().split()))
ans=[i for i in range(n)]
for i in range(n):
    ans[i]=a.index(i+1)+1
print(*ans)

### FIXED CODE ###

n=int(input())
num=[i+1 for i in range(n)]
L=list(map(int,input().split()))
ans=[0 for _ in range(n)]
for j in range(n):
    ans[L[j]-1]=num[j]
print(*ans)
### BUGGY CODE ###

import collections

h,w = map(int,input().split())
tizu = [list(input()) for i in range(h)]

dy = [1,0,-1,0]
dx = [0,1,0,-1]

def f(y,x):
    for i in range(4):
        newy = y + dy[i]
        newx = x + dx[i]
        if 0 <= newy and newy < h and 0 <= newx and newx < w:
            if tizu[newy][newx] == ".":
                q.append((newy,newx))
                tizu[newy][newx] = "#"
            
q = collections.deque()
for i in range(h): 
    for k in range(w):
        if tizu[i][k] == "#":
            q.append((i,k))
ans = 0
while True:
    l = len(q)
    if l == 0:
        print(ans-1)
        exit()
    for i in range(l):
        yx = q.popleft()
        f(yx[0],yx[1])
    ans += 1

### FIXED CODE ###

import collections

h,w = map(int,input().split())
tizu = [list(input()) for i in range(h)]

dy = [1,0,-1,0]
dx = [0,1,0,-1]

def f(y,x):
    for i in range(4):
        newy = y + dy[i]
        newx = x + dx[i]
        if 0 <= newy and newy < h and 0 <= newx and newx < w:
            if tizu[newy][newx] == ".":
                q.append((newy,newx))
                tizu[newy][newx] = "#"
            
q = collections.deque()
for i in range(h): 
    for k in range(w):
        if tizu[i][k] == "#":
            q.append((i,k))
ans = 0
while True:
    l = len(q)
    if l == 0:
        print(ans-1)
        exit()
    for i in range(l):
        yx = q.popleft()
        f(yx[0],yx[1])
    ans += 1
### BUGGY CODE ###

import math
import time


t = time.time()
def iip():
    return [int(i) for i in input().split()]

def main():
    H, N = iip()
    mp_for_damage = [10**9 for i in range(H+1)]
    mp_for_damage[0] = 0
    ab = []

    for i in range(N):
        ab.append(iip())

    ab= sorted(ab, key=lambda x:x[0]/x[1], reverse=True)

    for i in range(N):
        a, b = ab[i]
        for j in range(H+1):
            if a < H and mp_for_damage[a] < b:
                continue

            for k in range(1, a):
                if k > H:
                    break
                mp_for_damage[k] = min(mp_for_damage[k], b)

            if j + a > H:
                break
            mp_for_damage[j+a] = min(mp_for_damage[j+a], mp_for_damage[j] + b)

    #print(mp_for_damage)
    print(mp_for_damage[H])

    #print(time.time() - t)

main()

### FIXED CODE ###

import math
import time


t = time.time()
def iip():
    return [int(i) for i in input().split()]

def main():
    H, N = iip()
    mp_for_damage = [10**9 for i in range(H+1)]
    mp_for_damage[0] = 0
    ab = []

    for i in range(N):
        ab.append(iip())

    ab= sorted(ab, key=lambda x:x[0]/x[1], reverse=True)

    for i in range(N):
        a, b = ab[i]

        for k in range(1, min(a, H) + 1):
            mp_for_damage[k] = min(mp_for_damage[k], b)
            # print(k, b)

        if a >= H:
            mp_for_damage[H] = min(mp_for_damage[H], b)
            continue

        if a < H and mp_for_damage[a] < b:
            continue

        for j in range(H-a+1):
            mp_for_damage[j+a] = min(mp_for_damage[j+a], mp_for_damage[j] + b)

    #print(mp_for_damage)
    #print(mp_for_damage)
    print(mp_for_damage[H])

    #print(time.time() - t)

main()
### BUGGY CODE ###

N, M = map(int, input().split())
PY = []
array = [[] for _ in range(N)]

for _ in range(M):
    P, Y = map(int, input().split())
    PY.append([P, Y])
    array[P-1].append(Y)

for i in PY:
    print('{:06}'.format(i[0]), end='')
    print('{:06}'.format(sorted(array[i[0]-1]).index(i[1])+1))


### FIXED CODE ###

import bisect

N, M = map(int, input().split())
PY = []
array = [[] for _ in range(N)]

for _ in range(M):
    P, Y = map(int, input().split())
    PY.append([P, Y])
    array[P-1].append(Y)
    
for i in range(N):
    array[i].sort()

for p, y in PY:
    n = bisect.bisect_left(array[p-1], y) + 1
    print('%06d%06d' % (p, n))
### BUGGY CODE ###

n=input()
m=0
from itertools import combinations
for i,j in combinations(map(int,input().split()),2):
    k=i*j
    m+=k
v=m%1000000007
if m<=1000000007:
    print(m)
else:
    print(v)

### FIXED CODE ###

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

cumsum = [A[0]]
for i in range(1, N):
    cumsum.append(A[i] + cumsum[i - 1])

ans = 0
for i in range(N):
    ans += (A[i] * (cumsum[-1] - cumsum[i])) % MOD
    ans %= MOD

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

def resolve():
    n = int(input())
    a = list(map(int, input().split()))

    ans = 0
    num = (10 ** 9) + 7

    for i in range(n):
        tmp = 0
        for j in range(i+1, n):
            tmp = (a[i] * a[j]) % num
            ans += tmp
    
    ans = int(ans % num)
    print(ans)
 
resolve()

### FIXED CODE ###

def resolve():
    n = int(input())
    a = list(map(int, input().split()))

    ans = 0
    mod = (10 ** 9) + 7
    csum = sum(a)

    for i in range(n):
        csum -= a[i]
        ans += a[i] * csum

    ans = int(ans % mod)
    print(ans)
 
resolve()
### BUGGY CODE ###

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

graph1 = [[] for _ in range(n)]
graph2 = [[] for _ in range(n)]

for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    graph1[u].append((1, v))


# def dfs(v0, v, k):
#     if k == 0:
#         graph2[v0].append((1, v))
#         return
#
#     for i in range(len(graph1[v])):
#         dfs(v0, graph1[v][i][1], k-1)
#
# for i in range(n):
#     dfs(i, i, 3)
#     # print(graph1[i])

for i in range(n):
    q = [(i, 3)]
    idx = 0
    while True:
        if idx == len(q):
            break
        v, c = q[idx]
        idx += 1
        if c == 0:
            graph2[i].append((1, v))
            continue
        for _, j in graph1[v]:
            q.append((j, c-1))



# for i in range(n):
#     print(i)
#     for _, v in graph1[i]:
#         print(" ->", v)
#
#
# for i in range(n):
#     graph2[i] = list(set(graph2[i]))
# print()
# for i in range(n):
#     print(graph2[i])


s, t = map(int, input().split())
s -= 1
t -= 1


from heapq import heappush, heappop
def dijkstra(graph:list, node:int, start:int) -> list:
    # graph[node] = [(cost, to)]
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist
    # dist = [costs to nodes]

inf = float('inf')
dist = dijkstra(graph2, n, s)
if dist[t] == inf:
    print(-1)
else:
    print(dist[t])


### FIXED CODE ###

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

graph1 = [[] for _ in range(n*3)]

for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    graph1[u*3+0].append(v*3+1)
    graph1[u*3+1].append(v*3+2)
    graph1[u*3+2].append(v*3+0)

s, t = map(int, input().split())
s -= 1
t -= 1
s = s*3
t = t*3

from heapq import heappush, heappop
def dijkstra(graph:list, node:int, start:int) -> list:
    # graph[node] = [(cost, to)]
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    NextCost = 1
    while heap:
        cost,thisNode = heappop(heap)
        for NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist
    # dist = [costs to nodes]

inf = float('inf')
dist = dijkstra(graph1, n*3, s)
if dist[t] == inf:
    print(-1)
else:
    print(dist[t]//3)
### BUGGY CODE ###

# coding: utf-8

n = int(input())
s = tuple(map(int, input().rstrip().split()))
q = int(input())
t = tuple(map(int, input().rstrip().split()))
# print(s)
# print(t)
count = 0
for x in t:
    if x in s:
        count += 1
print(count)

### FIXED CODE ###

# coding: utf-8

n = int(input())
s = tuple(map(int, input().rstrip().split()))
q = int(input())
t = tuple(map(int, input().rstrip().split()))
# print("s:",s)
count = 0

def binary_search(array, value):
    ''' array must be sorted'''
    # print("value:{}".format(value))
    size = len(array)
    start = 0
    end = size - 1
    found = False
    while True:
        middle = (start + end)//2
        # print("s:{0}, e:{1}, m:{2}".format(start, end, middle))
        v = array[middle]
        if v == value:
            # print("v[{}]={} == {}".format(middle, v, value))
            return True
        elif start == end:
            # print("s:{} == e:{}".format(start, end)) 
            return False
        elif v > value:
            # print("v[{}]={} > {}".format(middle, v, value))
            end = middle
        elif v < value:
            # print("v[{}]={} < {}".format(middle, v, value))
            start = middle + 1

for i in range(q):
    if binary_search(s, t[i]):
        count += 1
print(count)
### BUGGY CODE ###

import fractions
def main():
    N = int(input())
    A = list(map(int, input().split()))
    g = A[0]
    l = A[0]
    for i in A[1:]:
        g = fractions.gcd(g, i)
        l = l * i // fractions.gcd(l, i)
    r = 0
    mod = 10 ** 9 + 7
    n = l // g
    for i in A:
        r += (n // (i // g)) % mod
    print(r % mod)
main()




### FIXED CODE ###

import fractions
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

def modinv(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m
def main():
    N = int(input())
    A = list(map(int, input().split()))
    g = A[0]
    l = A[0]
    for i in A[1:]:
        g = fractions.gcd(g, i)
        l = l * i // fractions.gcd(l, i)
    r = 0
    mod = 10 ** 9 + 7
    n = (l // g) % mod
    for i in A:
        r += n * modinv(i//g, mod)
    print(r % mod)
main()
### BUGGY CODE ###

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class UnionFind:
    def __init__(self, n):
        self.state = [-1] * n
        self.size_table = [1<<i for i in range(n)]
        # cntはグループ数
        # self.cnt = n

    def root(self, u):
        v = self.state[u]
        if v < 0: return u
        self.state[u] = res = self.root(v)
        return res

    def merge(self, u, v):
        ru = self.root(u)
        rv = self.root(v)
        if ru == rv: return
        du = self.state[ru]
        dv = self.state[rv]
        if du > dv: ru, rv = rv, ru
        if du == dv: self.state[ru] -= 1
        self.state[rv] = ru
        # self.cnt -= 1
        self.size_table[ru] |= self.size_table[rv]
        return

    # グループの要素数
    def size(self, u):
        return self.size_table[self.root(u)]

def main():
    n,k,l=MI()
    road=UnionFind(n)
    train=UnionFind(n)
    for _ in range(k):
        u,v=MI1()
        road.merge(u,v)
    for _ in range(l):
        u,v=MI1()
        train.merge(u,v)
    ans=[bin(road.size(i)&train.size(i)).count("1") for i in range(n)]
    print(*ans)

main()

### FIXED CODE ###

import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class UnionFind:
    def __init__(self, n):
        self.state = [-1] * n
        #self.size_table = [1<<i for i in range(n)]
        # cntはグループ数
        # self.cnt = n

    def root(self, u):
        v = self.state[u]
        if v < 0: return u
        self.state[u] = res = self.root(v)
        return res

    def merge(self, u, v):
        ru = self.root(u)
        rv = self.root(v)
        if ru == rv: return
        du = self.state[ru]
        dv = self.state[rv]
        if du > dv: ru, rv = rv, ru
        if du == dv: self.state[ru] -= 1
        self.state[rv] = ru
        # self.cnt -= 1
        #self.size_table[ru] |= self.size_table[rv]
        #self.size_table[rv]=0
        return

def main():
    n,k,l=MI()
    road=UnionFind(n)
    train=UnionFind(n)
    for _ in range(k):
        u,v=MI1()
        road.merge(u,v)
    for _ in range(l):
        u,v=MI1()
        train.merge(u,v)
    cnt=defaultdict(int)
    for u in range(n):
        cnt[road.root(u),train.root(u)]+=1
    ans=[cnt[road.root(u),train.root(u)] for u in range(n)]
    print(*ans)

main()
### BUGGY CODE ###

def solve(n):
  l = [0]*(n+1)
  for i in range(1,n+1):
    for j in range(i,n+1,i):
      if j % i == 0:
        l[j] += 1
  s = 0
  for i in range(n):
    s += (i+1)*l[i+1]
  return s

def main():
  N = int(input())
  print(solve(N))
  return

main()

### FIXED CODE ###

def solve(n):
  ans = 0
  for i in range(1,n+1):
    for j in range(i,n+1,i):
      if j % i == 0:
        ans += j
  return ans

def main():
  N = int(input())
  print(solve(N))
  return

main()
### BUGGY CODE ###

n=int(input())
d=list(map(int,input().split()))
dd=sorted(d)
N=dd[n-1]
sum1=0
sum2=0
count=0
for k in range(N+1):
    for i in range(n):
        if d[i]>=k:
            sum1+=1
        else:
            sum2+=1
    if sum1==sum2:
        count+=1
    sum1=0
    sum2=0
print(count)

### FIXED CODE ###

n=int(input())
d=list(map(int,input().split()))
dd=sorted(d)
N=dd[int((n/2))]-dd[int((n/2)-1)]
print(N)
### BUGGY CODE ###

from collections import Counter
from collections import defaultdict

def li():
    return [int(x) for x in input().split()]

H, W, M = li()
PH = []
PW = []
PHF = defaultdict(int)
PWF = defaultdict(int)
PF = defaultdict(int)

for i in range(M):
    h, w = li()
    PH.append(h)
    PW.append(w)
    PHF[h] += 1
    PWF[w] += 1
    PF[h, w] += 1

sorted_PHF = sorted(PHF.items(), key=lambda x: x[1], reverse=True)
sorted_PWF = sorted(PWF.items(), key=lambda x: x[1], reverse=True)
most_common_h_cnt = sorted_PHF[0][1]
most_common_w_cnt = sorted_PWF[0][1]

most_common_h_list = []
for x in sorted_PHF:
    if x[1] < most_common_h_cnt:
        break
    most_common_h_list.append(x[0])

most_common_w_list = []
for x in sorted_PWF:
    if x[1] < most_common_w_cnt:
        break
    most_common_w_list.append(x[0])

add = -1
for h in most_common_h_list:
    for w in most_common_w_list:
        if PF[h, w] == 0:
            add = 0

cnt = most_common_h_cnt + most_common_w_cnt + add

print(cnt)

### FIXED CODE ###

from collections import Counter
from collections import defaultdict

def li():
    return [int(x) for x in input().split()]

H, W, M = li()
PH = []
PW = []
PHF = defaultdict(int)
PWF = defaultdict(int)
PF = defaultdict(int)

for i in range(M):
    h, w = li()
    PH.append(h)
    PW.append(w)
    PHF[h] += 1
    PWF[w] += 1
    PF[h, w] += 1

sorted_PHF = sorted(PHF.items(), key=lambda x: x[1], reverse=True)
sorted_PWF = sorted(PWF.items(), key=lambda x: x[1], reverse=True)
most_common_h_cnt = sorted_PHF[0][1]
most_common_w_cnt = sorted_PWF[0][1]

most_common_h_list = []
for x in sorted_PHF:
    if x[1] < most_common_h_cnt:
        break
    most_common_h_list.append(x[0])

most_common_w_list = []
for x in sorted_PWF:
    if x[1] < most_common_w_cnt:
        break
    most_common_w_list.append(x[0])

add = -1
for h in most_common_h_list:
    for w in most_common_w_list:
        if PF[h, w] == 0:
            add = 0
            break

cnt = most_common_h_cnt + most_common_w_cnt + add

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

from collections import Counter

n, m = map(int, input().split())
ab = (list(map(int, input().split())) for _ in range(m))

cnt = Counter(sum(ab, []))
ans =  all(cnt[i] % 2 == 0 for i in cnt)
print('YES' if ans else 'NO')


### FIXED CODE ###

from collections import defaultdict

n, m = map(int, input().split())
ab = (list(map(int, input().split())) for _ in range(m))

cnt = defaultdict(int)
for a, b in ab:
    cnt[a] += 1
    cnt[b] += 1
ans = all(cnt[i] % 2 == 0 for i in cnt)
print('YES' if ans else 'NO')
### BUGGY CODE ###

X = int(input())

if X == 1:
    print(0, -1)
    exit()

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def solve(num, X):
    a = 1
    for i in range(1, X+1):
        if (i * 100) ** 5 - (i * 100 - num) ** 5 >= X:
            break
    max_a = 100 * i
    a = max_a - 99
    while a <= max_a:
        if (a) ** 5 - (a -num) ** 5 == X:
            return True, a, a - num
        a += 1
    return False, 0, 0

arr = make_divisors(abs(X))
for num in arr:
    if (num - X) % 5:
        continue
    flag, a, b = solve(num , X)
    if flag:
        print(a, b)
        exit()
    flag, a, b = solve(-num , X)
    if flag:
        print(a, b)
        exit()

### FIXED CODE ###

X = int(input())

if X == 1:
    print(0, -1)
    exit()

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def solve(num, X):
    a = 1
    for i in range(1, X+1):
        if (i * 100) ** 5 - (i * 100 - num) ** 5 >= X:
            break
    max_a = 100 * i
    a = max_a - 99
    while a <= max_a:
        if (a) ** 5 - (a -num) ** 5 == X:
            return True, a, a - num
        a += 1
    return False, 0, 0

arr = make_divisors(abs(X))
for num in arr:
    if (num - X) % 5:
        continue
    flag, a, b = solve(num , X)
    if flag:
        print(a, b)
        exit()
    #flag, a, b = solve(-num , X)
    #if flag:
    #    print(a, b)
    #    exit()
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**8)
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def solve():
    N = inp()
    AA = [inpl() for _ in range(N)]
    itrs = [0]*N
    ans = cnt = 0

    while True:
        next = set()
        for x,itr in enumerate(itrs):
            if itr < N-1:
                t = AA[x][itr] - 1
                if AA[t][itrs[t]]-1 == x:
                    next.add(x)

        for n in next:
            itrs[n] += 1
            if itrs[n] == N-1:
                cnt += 1

        if len(next) == 0:
            for x in itrs:
                if x != N-1:
                    return -1
            return ans

        ans += 1

if __name__ == '__main__':
    print(solve())


### FIXED CODE ###

import sys,time
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def solve():
    start = time.time()

    N = inp()
    AA = [inpl() for _ in range(N)]
    itrs = [0]*N
    ans = cnt = 0

    while True:
        next = set()
        for x,itr in enumerate(itrs):
            if itr < N-1:
                t = AA[x][itr] - 1
                if AA[t][itrs[t]]-1 == x:
                    next.add(x)

        for n in next:
            itrs[n] += 1
            if itrs[n] == N-1:
                cnt += 1

        if len(next) == 0:
            for x in itrs:
                if x != N-1:
                    return -1
            return ans

        now = time.time() - start
        if now > 1.6:
            return N*(N-1)//2

        ans += 1

if __name__ == '__main__':
    print(solve())