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

from heapq import heappush, heappop

N = int(input())
A = [int(input()) for i in range(N)]
hq = []
for i in range(N):
    if A[i] not in hq:
        heappush(hq, -A[i])
max1 = -heappop(hq)
max2 = -heappop(hq)
ans = [0] * N
for i in range(N):
    if A[i] == max1:
        ans[i] = max2
    else:
        ans[i] = max1
print(*ans, sep="\n")


### FIXED CODE ###

from heapq import heappush, heappop

N = int(input())
A = [int(input()) for i in range(N)]
hq = []
for i in range(N):
    heappush(hq, -A[i])
max1 = -heappop(hq)
max2 = -heappop(hq)
ans = [0] * N
for i in range(N):
    if A[i] == max1:
        ans[i] = max2
    else:
        ans[i] = max1
print(*ans, sep="\n")
### BUGGY CODE ###

N,M = map(int,input().split())
tempL,tempR = map(int,input().split())
gate_id = set(range(tempL,tempR+1))
for i in range(M-1):
  tempL,tempR = map(int,input().split())
  temp_id = set(range(tempL,tempR+1))
  gate_id = gate_id & temp_id
print(len(gate_id))

### FIXED CODE ###

N,M = map(int,input().split())
gateid = [list(map(int,input().split())) for i in range(M)]
idleft = max(gateid,key=lambda x:x[0])
idright = min(gateid,key=lambda x:x[1])
ans = idright[1]-idleft[0]+1
if ans<0:
  ans = 0
print(ans)
### BUGGY CODE ###

N = int(input())
S = input()
sum = 0
r = 0
g = 0
b = 0

for i in range(N):
    if S[i] == "R":
        r+=1
    elif S[i] == "G":
        g+=1
    else:
        b+=1

sum = r*g*b

for i in range(N):
    for j in range(N//2 +1):
        try:
            a = S[i]
            b = S[i+j]
            c = S[i+2*j]
            if a!=b and b!=c and c!=a:
                sum -= 1
        except:
            continue

print(sum)


### FIXED CODE ###

N = int(input())
S = input()
sum = 0
r = 0
g = 0
b = 0

for i in range(N):
    if S[i] == "R":
        r+=1
    elif S[i] == "G":
        g+=1
    else:
        b+=1

sum = r*g*b

for i in range(N):
    for j in range((N-i)//2 +1):
        try:
            a = S[i]
            b = S[i+j]
            c = S[i+2*j]
            if a!=b and b!=c and c!=a:
                sum -= 1
        except:
            continue

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

n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
total = sum(a)
total %= mod
total = total ** 2
total %= mod
cnt = 0
for i in range(n):
  cnt += a[i]**2
  cnt %= mod

ans = total - cnt
ans %= mod
ans *= pow(2, mod - 2)
ans %= mod
print(ans)
  


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
total = sum(a)
total = total ** 2
cnt = 0
for i in range(n):
  cnt += a[i]**2

ans = (total - cnt) // 2
ans %= mod

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

N = int(input())
S = input()
l = N//2+1
f = 0
while f == 0:
    l -= 1
    if l == 0:
        f = 1
    for i in range(N-2*l+1):
        P = S[i:i+l]
        for j in range(l+i,N-l+1):
            Q = S[j:j+l]
            if P == Q:
                f = 1
print(l)

### FIXED CODE ###

N = int(input())
S = input()
def Z_algo(S):
    n = len(S)
    LCP = [0]*n
    i = 1
    j = 0
    c = 0#最も末尾側までLCPを求めたインデックス
    for i in range(1, n):
        #i番目からのLCPが以前計算したcからのLCPに含まれている場合
        if i+LCP[i-c] < c+LCP[c]:
            LCP[i] = LCP[i-c]
        else:
            j = max(0, c+LCP[c]-i)
            while i+j < n and S[j] == S[i+j]: j+=1
            LCP[i] = j
            c = i
    for i in range(1,n):
        if i <= LCP[i]:
            LCP[i] = i
    return LCP
ans = 0
for i in range(N):
    T = S[i:]
    R = Z_algo(T)
    R.pop(0)
    R.append(0)
    ans = max(max(R),ans)
print(ans)
### BUGGY CODE ###

mod = 10**9+7
n, m = list(map(int,input().split()))
s = list(map(int,input().split()))
t = list(map(int,input().split()))
dp = [[0]*(m+1) for _ in range(n+1)]
for i in range(n)[::-1]:
    for j in range(m)[::-1]:
        if s[i] == t[j]:
            dp[i][j] += 1 + dp[i+1][j+1] 
            dp[i][j] %= mod
        dp[i][j] += dp[i+1][j] + dp[i][j+1] - dp[i+1][j+1]
        dp[i][j] %= mod
print((dp[0][0]+1)%mod)

### FIXED CODE ###

mod = 10**9+7
n, m = list(map(int,input().split()))
s = list(map(int,input().split()))
t = list(map(int,input().split()))
dp = [0]*(m+1)
for i in range(n)[::-1]:
    old = 0
    for j in range(m)[::-1]:
        tmp = dp[j] + dp[j+1] - old
        tmp %= mod
        if s[i]== t[j]:
            tmp += 1 + old
            tmp %= mod
        dp[j], old = tmp, dp[j]
print((dp[0]+1)%mod)
### BUGGY CODE ###

n=int(input())
dic={i:{j:2 for j in range(1,n+1)}for i in range(1,n+1)}
proof={k:0 for k in range(n+1)}
       
for i in range(1,n+1):
    a=int(input())
    for _ in range(a):
        x,y=map(int,input().split())
        dic[i][x]=y
                    
#dicに誰々が正直者か嘘つきであるかということをまとめるdic[x][y]=? xからｙは正直者あるいは真偽不明の者である

maxi=0

for _ in range(1,2**n): 
    #計算量 3* (10)**4
    choice=format(_,"0%ib"%n)
    #1が正直者,0がよくわからない人
    #正直者の発言が全て一致していなくてはいけない　それであれば、矛盾は生じない
    flag=True
    honest=[0 for _ in range(n+1)]
    for i in range(n):
        if choice[i]=="1":
            honest[i+1]=1
    for i in range(1,n+1):
      for j in range(1,n+1):
        if (i!=j and dic[i][j]==0 and honest[i] and honest[j]) or(i!=j and dic[i][j]==1 and honest[i] and (not honest[j])):
          flag=False
          break
         
    if flag:
      maxi=max(maxi,sum(honest))
      
      	

print(maxi)

### FIXED CODE ###

n=int(input())
lists=[[] for _ in range(n)]
for i in range(n):
    a=int(input())
    for j in range(a):
        x,y=map(int,input().split())
        lists[i].append([x,y])
ans=0
for k in range(2**n):
    subans=0
    flag=True
    string=format(k,"0%ib"%n)
    listss=["unknown" for i in range(n)]
    for i in range(n):
        if string[i]=="1":
            #正直者だとした場合の全探索
            subans+=1
            for some in lists[i]:
                if listss[some[0]-1]=="unknown":
                    listss[some[0]-1]=some[1]
                elif listss[some[0]-1]!="unknown":
                    if listss[some[0]-1]!=some[1]:
                        flag=False
                        break

    if flag:
        flags=True
        for i in range(n):
            if  listss[i]!="unknown" and listss[i]!=int(string[i]):
                flags=False
        if flags:
            ans=max(ans,subans)

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

N, *a = map(int, open(0).read().split())

ans = 1
for x in a:
    ans = ans * x

if 10 ** 18 < ans:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

N, *a = map(int, open(0).read().split())

if 0 in a:
    print(0)
    exit()

ans = 1
for x in a:
    ans = ans * x
    if 10 ** 18 < ans:
        print(-1)
        exit()

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

#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

#from fractions import gcd

def gcd(a, b): 
    if a == 0: return b 
    if b == 0: return a 
    k = 0
    while ((a | b) & 1) == 0: 
        a >>= 1
        b >>= 1
        k += 1
    while (a & 1) == 0: 
        a >>= 1
    while b:  
        while (b & 1) == 0: 
            b >>= 1
        if a > b : 
            a,b = b,a
        b = b - a 
    return a << k 

from math import sqrt
def pfact(num):
    d = set()
    if num&1 == 0:
        d.add(2)
    while num&1 == 0:
        num >>= 1
    for i in range(3,int(sqrt(num))+1,2):
        if num%i==0:
            d.add(i)
        while num%i==0:
            num //= i
    if num>2:
        d.add(num)
    return list(d)

n = inp()
x = ip()
x.sort(reverse=True)
t = x[0]
for i in range(1,n):
    t = gcd(t,x[i])
    if t == 1:
        break
if t != 1:
    ans = 'not coprime'
else:
    ans = 'pairwise coprime'
    dt = {i+1:1 for i in range(10**6)}
    for i in x:
        for j in pfact(i):
            if dt[j]:
                dt[j] = 0
            else:
                ans = 'setwise coprime'
                break
print(ans)


### FIXED CODE ###

#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

#from fractions import gcd

def gcd(a, b): 
    if a == 0: return b 
    if b == 0: return a 
    k = 0
    while ((a | b) & 1) == 0: 
        a >>= 1
        b >>= 1
        k += 1
    while (a & 1) == 0: 
        a >>= 1
    while b:  
        while (b & 1) == 0: 
            b >>= 1
        if a > b : 
            a,b = b,a
        b = b - a 
    return a << k 

from math import sqrt
def pfact(num):
    global dt
    if num&1 == 0:
        num >>= 1
        if dt[2]:
            dt[2] = 0
        else:
            return 'setwise coprime'
    while num&1 == 0:
        num >>= 1
    for i in range(3,int(sqrt(num))+1,2):
        if num%i == 0:
            num //= i
            if dt[i]:
                dt[i] = 0
            else:
                return 'setwise coprime'
        while num%i == 0:
            num //= i
    if num>2:
        if dt[num]:
            dt[num] = 0
        else:
            return 'setwise coprime'
    return 'pairwise coprime'

n = inp()
x = ip()
x.sort(reverse=True)
t = x[0]
for i in range(1,n):
    t = gcd(t,x[i])
    if t == 1:
        break
if t != 1:
    ans = 'not coprime'
else:
    ans = 'pairwise coprime'
    dt = {i+1:1 for i in range(10**6)}
    for i in x:
        if pfact(i) == 'setwise coprime':
            ans = 'setwise coprime'
            break
print(ans)
### BUGGY CODE ###

N,P=[int(s) for s in input().split()]
DP=[[0 for _ in range(N-i)] for i in range(N)]
ls=[int(s)%P for s in list(input())]

count=0

for j in range(N):
  DP[0][j]=ls[j]
  if DP[0][j]==0:
    count+=1


for i in range(1,N):
  for j in range(N-i):
    DP[i][j]=(DP[i-1][j]*10+DP[0][i+j])%P
    if DP[i][j]==0:
      count+=1
    
print(count)

### FIXED CODE ###

N,P=[int(s) for s in input().split()]
S=list(input())
if P!=2 and P!=5:
  ls=[0 for _ in range(N)]
  mod=[0 for _ in range(P)]
  d=10
  ls[0]=int(S[-1])%P
  mod[ls[0]]+=1
  for i in range(1,N):
    ls[i]=(ls[i-1]+d*int(S[-1-i]))%P
    d=(d*10)%P
    mod[ls[i]]+=1
  #print(ls)
  #print(mod)  
  ans=mod[0]
  for i in range(N):
    m=ls[i]
    mod[ls[i]]-=1
    ans+=mod[ls[i]]
else:
  ans=0
  for i in range(N):
    if int(S[i])%P==0:
      ans+=i+1
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

def main():
  N_a, N_b, N_q = map(int, input().strip().split(' '))
  As = list(map(int, [input().strip() for _ in range(N_a)]))
  Bs = list(map(int, [input().strip() for _ in range(N_b)]))
  Qs = list(map(int, [input().strip() for _ in range(N_q)]))

  for q in Qs:
    min_a_l = -(2*(10**10)+1)
    min_b_l = -(2*(10**10)+1)
    min_a_r = 2*(10**10)+1
    min_b_r = 2*(10**10)+1
    for a in As:
      da = a - q
      if 0 <= da < min_a_r:
          min_a_r = da
          break
      if 0 > da >= min_a_l:
          min_a_l = da

    for b in Bs:
      db = b - q
      if 0<= db < min_b_r:
          min_b_r = db
          break
      if 0 > db >= min_b_l:
          min_b_l = db
    score = min([
      min_a_r if min_a_r > min_b_r else min_b_r,
      abs(min_a_l) if min_a_l < min_b_l else abs(min_b_l),
      2*min_a_r + abs(min_b_l) if min_a_r < abs(min_b_l) else min_a_r + 2*abs(min_b_l),
      2*abs(min_a_l) + abs(min_b_r) if abs(min_a_l) < abs(min_b_r) else abs(min_a_l) + 2*abs(min_b_r)])
    print(score)

  pass

if __name__=='__main__':
  main()

### FIXED CODE ###

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

from bisect import bisect_left

def main():
  N_a, N_b, N_q = map(int, input().strip().split(' '))
  As = list(map(int, [input().strip() for _ in range(N_a)]))
  Bs = list(map(int, [input().strip() for _ in range(N_b)]))
  Qs = list(map(int, [input().strip() for _ in range(N_q)]))

  for q in Qs:
    min_a_l = -(2*(10**10)+1)
    min_b_l = -(2*(10**10)+1)
    min_a_r = 2*(10**10)+1
    min_b_r = 2*(10**10)+1
    a_index = bisect_left(As, q)
    if len(As) > a_index-1 >= 0:
      min_a_l = As[a_index-1] - q
    if a_index < len(As):
      min_a_r = As[a_index] - q

    b_index = bisect_left(Bs, q)
    if len(Bs) > b_index-1 >= 0:
      min_b_l = Bs[b_index-1] - q
    if b_index < len(Bs):
      min_b_r = Bs[b_index] - q

    score = min([
      min_a_r if min_a_r > min_b_r else min_b_r,
      abs(min_a_l) if min_a_l < min_b_l else abs(min_b_l),
      2*min_a_r + abs(min_b_l) if min_a_r < abs(min_b_l) else min_a_r + 2*abs(min_b_l),
      2*abs(min_a_l) + abs(min_b_r) if abs(min_a_l) < abs(min_b_r) else abs(min_a_l) + 2*abs(min_b_r)])
    print(score)

  pass

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

N, K = map(int, input().rstrip().split())
route = [0] + list(map(int, input().rstrip().split()))

# 愚直解 計算時間大
current = 1
for _ in range(K):
    current = route[current]

print(current)

### FIXED CODE ###

from collections import deque
N, K = map(int, input().rstrip().split())
route = [0] + list(map(int, input().rstrip().split()))

# 町1から町nまでの距離
dis = [0] * (N+1)

# 訪問済み
checked = [0] * (N+1)

queue = deque([1])
hop_count = 0
offset = 0

ans = 1
while queue:
    i = queue.popleft()

    if hop_count == K:
        ans = i
        break

    A_i = route[i]

    if checked[A_i] == 1:
        roop_node_count = abs(hop_count+1 - dis[A_i])
        offset = (K - hop_count) % roop_node_count

        for _ in range(offset):
            i = route[i]
        ans = i
        break

    queue.append(A_i)
    hop_count += 1
    dis[A_i] = dis[i] + 1
    checked[i] = 1

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

def get_ac(n,q,s,l,r):

    for i in range(q):
        left = l[i]
        right = r[i]
        #print s[left-1:right]
        count = 0
        for j in range(left-1,right-1):
            if s[j] == 'A' and s[j+1] == 'C':
                count += 1
        else:
            print count

if __name__ == '__main__':
    n,q = map(int,raw_input().split())
    s = raw_input()

    l = [0]*q
    r = [0]*q

    for i in range(q):
        l[i],r[i] = map(int,raw_input().split())


    get_ac(n,q,s,l,r)

### FIXED CODE ###

def get_ac(n,q,s,l,r):
    pos = [0]*n

    for j in range(0,n-1):
        if s[j] == 'A' and s[j+1] == 'C':
            pos[j+1] = pos[j] + 1
        else:
            pos[j+1] = pos[j]

    for i in range(q):
        left = l[i]
        right = r[i]
        count = pos[right - 1] - pos[left - 1]

        print count

if __name__ == '__main__':

    n,q = map(int,raw_input().split())
#    t1 = time.time()
    s = raw_input()

    l = [0]*q
    r = [0]*q

    for i in range(q):
        l[i],r[i] = map(int,raw_input().split())

#    t2 = time.time()
#    print('{0:.9f}'.format(t2 - t1))
    get_ac(n,q,s,l,r)
### BUGGY CODE ###

n=int(input())
vs=[int(i) for i in input().split()]
vs1=[v for i,v in enumerate(vs) if i%2==0]
vs2=[v for i,v in enumerate(vs) if i%2==1]

ans=0
if len(list(set(vs)))==1:
  ans+=len(vs2)
if len(list(set(vs1)))!=1:
  tmp=[]
  for uniq in list(set(vs1)):
    tmp.append(n/2-vs1.count(uniq))
  ans+=min(tmp)
if len(list(set(vs2)))!=1:
  tmp=[]
  for uniq in list(set(vs2)):
    tmp.append(n/2-vs2.count(uniq))
  ans+=min(tmp)

print(int(ans))

### FIXED CODE ###

from collections import Counter
n=int(input())
V=list(map(int,input().split()))

E=Counter(V[::2]).most_common() + [(0, 0)]
O=Counter(V[1::2]).most_common() + [(0, 0)]
if E[0][0]!=O[0][0]:
  print(n-E[0][1]-O[0][1])
else:
  print(n-max(E[1][1]+O[0][1],E[0][1]+O[1][1]))
### BUGGY CODE ###

n = int(input())
s = input()
r = [-1]*n
g = [-1]*n
b = [-1]*n
count = 0
for i in range(len(s)):
    if s[i] == 'R':
        r[count] = i+1
    elif s[i] == 'G':
        g[count] = i+1
    else:
        b[count] = i+1
    count += 1
ans = 0
for i in r:
    if i == -1:
        continue
    for j in g:
        if j == -1:
            continue
        for k in b:
            if k == -1:
                continue
            if j-i != k-j and i-j != k-i and k-i != j-k:
                ans += 1
print(ans)

### FIXED CODE ###

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

N = int(input())
S = str(input())
list1 = []
cnt = 0
for x in range(len(S)):
  for y in range(x+1, len(S)):
    for z in range(y+1, len(S)):
      if (S[x]+S[y]+S[z] in list1) == False:
        cnt += 1
        list1.append(S[x]+S[y]+S[z])
print(cnt)

### FIXED CODE ###

N = int(input())
S = input()
  
ans = 0
for i1 in range(10):
  for i2 in range(10):
    for i3 in range(10):
      p = 1
      for f in range(N):
        if(p == 1 and S[f] == str(i1)):
          p = 2
        elif(p == 2 and S[f] == str(i2)):
          p = 3
        elif(p == 3 and S[f] == str(i3)):
          ans += 1
          break
        
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
from bisect import bisect, bisect_left
from itertools import product, combinations
from operator import itemgetter
def inpl(): return tuple(map(int, input().split()))

# B以下の数-A未満の数→bisect(L, B) - bisect_left(L, A)
N, K = inpl()
X = []
Y = []
Px = []
Py = []

for i in range(N):
    x, y = inpl()
    X.append(x)
    Y.append(y)
    Px.append((x, i))
    Py.append((y, i))

X = sorted(X)
Y = sorted(Y)
Px = sorted(Px)
Py = sorted(Py)

get = itemgetter(1)
res = []

for (l, r), (d, u) in product(combinations(X, 2), combinations(Y, 2)):
    sx = set(map(get, Px[bisect_left(X, l):bisect(X, r)]))
    sy = set(map(get, Py[bisect_left(Y, d):bisect(Y, u)]))
    if len(sx.intersection(sy)) >= K:
        res.append((r-l)*(u-d))

print(min(res))

### FIXED CODE ###

# -*- coding: utf-8 -*-
from itertools import product, combinations
from operator import itemgetter
def inpl(): return tuple(map(int, input().split()))

N, K = inpl()
X = []
Y = []
Px = []
Py = []

for i in range(N):
    x, y = inpl()
    X.append(x)
    Y.append(y)
    Px.append((x, i))
    Py.append((y, i))

X = sorted(X)
Y = sorted(Y)

dX = {x:i for i, x in enumerate(X)}
dY = {y:i for i, y in enumerate(Y)}
Px = sorted(Px)
Py = sorted(Py)

get = itemgetter(1)
res = 5 * 10**18

for (l, r), (d, u) in product(combinations(X, 2), combinations(Y, 2)):
    if (dX[r] - dX[l] < K-1) or (dY[u] - dY[d] < K-1):
        continue
    if res < (r-l)*(u-d):
        continue

    sx = set(map(get, Px[dX[l]:dX[r]+1]))
    sy = set(map(get, Py[dY[d]:dY[u]+1]))
    if len(sx.intersection(sy)) >= K:
        res = min(res, (r-l)*(u-d))

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

def judge(A, B):
  i=0
  if len(A) != len(B):
    return False
  else:
    while i<len(A):
      if A[i] != B[len(B)-i-1]:
        return False
    return True
 
A=input()
B=input()
 
if judge(A, B):
  print("YES")
else:
  print("NO")

### FIXED CODE ###

def judge(A, B):
  i=0
  if len(A) != len(B):
    return False
  else:
    while i<len(A):
      if A[i] != B[len(B)-i-1]:
        return False
      i+=1
    return True

A=input()
B=input()
 
if judge(A, B):
  print("YES")
else:
  print("NO")
### BUGGY CODE ###

n = int(input())
ans = 1
flag = 0
A = map(int,input().split())
for a in A:
    ans *= a

if ans > 1e18:
    print(-1)
else:
    print(ans)



### FIXED CODE ###

n = int(input())
ans = 1
flag = 0
A = map(int,input().split())
A = list(A)
list.sort(A)
for a in A:
    ans *= a
    if ans == 0:
        print(0)
        flag = 1
        break
    if ans > 1e18:
        print(-1)
        flag = 1
        break

if flag == 0:
    print(ans)
### BUGGY CODE ###

import sys,heapq

input=sys.stdin.readline

N=int(input())
node=[]
for i in range(0,N-1):
    for j in range(i+1,N):
        node.append((i,j))

_to={key:set([]) for key in node}
_from={key:set([]) for key in node}


for i in range(0,N):
    A=list(map(int,input().split()))
    for j in range(0,N-2):
        if A[j]-1>i:
            if A[j+1]-1>i:
                _to[(i,A[j]-1)].add((i,A[j+1]-1))
                _from[(i,A[j+1]-1)].add((i,A[j]-1))
            else:
                _to[(i,A[j]-1)].add((A[j+1]-1,i))
                _from[(A[j+1]-1,i)].add((i,A[j]-1))
        else:
            if A[j+1]-1>i:
                _to[(A[j]-1,i)].add((i,A[j+1]-1))
                _from[(i,A[j+1]-1)].add((A[j]-1,i))
            else:
                _to[(A[j]-1,i)].add((A[j+1]-1,i))
                _from[(A[j+1]-1,i)].add((A[j]-1,i))

day=0
conduct=set([])
subconduct=set([])
q=set([])
for key in node:
    if not _from[key]:
        conduct.add(key)
        q.add(key)
sub=set([])
while q:
    while q:
        x=q.pop()
        for n in _to[x]:
            if conduct>=_from[n]:
                subconduct.add(n)
                sub.add(n)
        if not q:
            q=sub
            sub=set([])
            conduct=conduct|subconduct
            subconduct=set([])
            day+=1
            break

if conduct==set(node):
    print(day)
else:
    print(-1)

### FIXED CODE ###

N=int(input())
A=[[] for i in range(0,N)]
nodes=[]
for i in range(0,N-1):
    for j in range(i+1,N):
        nodes.append((i,j))

order={i:[0 for j in range(0,N)] for i in range(0,N)}
for i in range(0,N):
    A[i]=list(map(int,input().split()))
    for j in range(0,N-1):
        order[i][A[i][j]-1]=j
        #i番目の人がj番目の人と戦うのが何番目か


q=set([])
for battle in nodes:
    i,j=battle
    if order[i][j]==0 and order[j][i]==0:
        q.add(battle)

flag=[0 for i in range(0,N)]

day=0
sub=set([])
while q:
    while q:
        i,j=q.pop()
        flag[i]+=1
        flag[j]+=1
        if flag[i]!=N-1:
            s=A[i][order[i][j]+1]-1
            if flag[s]>=order[s][i]:
                sub.add((i,s))
        if flag[j]!=N-1:
            t=A[j][order[j][i]+1]-1
            if flag[t]>=order[t][j]:
                sub.add((j,t))

        if not q:
            day+=1
            q=sub
            sub=set([])
            break

able=True
for i in range(0,N):
    if flag[i]!=N-1:
        able=False

if able:
    print(day)
else:
    print(-1)
### BUGGY CODE ###

# coding: utf-8 
r, g, b, n = map(int, input().split())
a = [r, g, b]
a.sort()
cnt = 0
for i in range(n//a[0]+1):
    for j in range(n//a[1]+1):
        if n-i*a[0]-j*a[1]>=0 and (n-i*a[0]-j*a[1])%a[2]==0:
            cnt+=1
print(cnt)


### FIXED CODE ###

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

X = int(input())
dp = {}
dp[0] = 0

for i in range(1,X+1):
    dp_ = dp.copy()
    for key in dp.keys():
        if not key+i in dp.keys(): dp_[key+i] = i
        else: dp_[key+i] = min(dp_[key+i], i)

        #if not key-i in dp.keys(): dp_[key-i] = i
        #else: dp_[key-i] = min(dp_[key-i],i)
    dp = dp_
ans = dp[X]
print(ans)



### FIXED CODE ###

import math
X = int(input())
n =  math.ceil((math.sqrt(1+8*X) - 1)/2)
ans = n
print(ans)
### BUGGY CODE ###

N = int(input())
A = [int(i) for i in input().split()]	# 1 2 3 4 5 ...
B = [int(i) for i in input().split()]	# 1 2 3 4 5 ...
C = [int(i) for i in input().split()]	# 1 2 3 4 5 ...

A.sort()
B.sort()
C.sort()

#print(A)
#print(B)
#print(C)
##exit(0)

ab=[0]*N
bc=[0]*N

b=N-1
c=N-1
count=0
while (0<=b):
	while (0<=c):
		if B[b]<C[c]:
			count += 1
			c -= 1
		else:
			break
	bc[b] = count
	b -= 1

a=N-1
b=N-1
count=0
while (0<=a):
	while (0<=b):
		if A[a]<B[b]:
			count += 1
			b -= 1
		else:
			break
	ab[a] = count
	a -= 1


#print(ab)
#print(bc)

ans=0

for i in ab:
#	print(i, bc[N-i:N])
	ans += sum(bc[N-i:N])

print(ans)


### FIXED CODE ###

N = int(input())
A = [int(i) for i in input().split()]	# 1 2 3 4 5 ...
B = [int(i) for i in input().split()]	# 1 2 3 4 5 ...
C = [int(i) for i in input().split()]	# 1 2 3 4 5 ...

A.sort()
B.sort()
C.sort()

#print(A)
#print(B)
#print(C)
##exit(0)

ab=[0]*N
bc=[0]*N

b=N-1
c=N-1
count=0
while (0<=b):
	while (0<=c):
		if B[b]<C[c]:
			count += 1
			c -= 1
		else:
			break
	bc[b] = count
	if b<N-1:
		bc[b] += bc[b+1]
	b -= 1

a=N-1
b=N-1
count=0
while (0<=a):
	while (0<=b):
		if A[a]<B[b]:
			count += 1
			b -= 1
		else:
			break
	ab[a] = count
	a -= 1


#print(ab)
#print(bc)

ans=0

for i in ab:
#	print(i, bc[N-i:N])
#	ans += sum(bc[N-i:N])
	if i>0:
		ans += bc[N-i]

print(ans)