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

from collections import Counter
N=int(input())
A=[int(item)for item in input().split()]
c=Counter(A)
full = {i: c[i] * (c[i] - 1) / 2 for i in c.keys()}
#full = [full[item-1] for item in A]
#print(full)
#ichiheri = [c[i]-1 * (c[i]-1) / 2 for i in range(1,1+len(c))]
ichiheri = {i:(c[i]-2) * (c[i]-1) / 2 for i in c.keys()}
#print(ichiheri)

for a in A:
    sumA = sum(full.values())
    sumA-=full[a]
    sumA += ichiheri[a]
    print(int(sumA))

### FIXED CODE ###

from collections import Counter
N=int(input())
A=[int(item)for item in input().split()]
c=Counter(A)
full = {i: c[i] * (c[i] - 1) / 2 for i in c.keys()}
#full = [full[item-1] for item in A]
#print(full)
#ichiheri = [c[i]-1 * (c[i]-1) / 2 for i in range(1,1+len(c))]
sumA = sum(full.values())
val = {i:int(sumA-full[i]+(c[i]-2) * (c[i]-1) / 2 )for i in c.keys()}
#print(ichiheri)



for a in A:
    print(val[a])
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
ans = 1
for i in a:
    ans *= i

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


### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
if 0 in A:
    print(0)
    exit()
ans = 1
for a in A:
    ans *= a
    if ans > 10 ** 18:
        print(-1)
        exit()
print(ans)
### BUGGY CODE ###

import math
K = int(input())

res = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            tmp = math.gcd(i, j)
            res += math.gcd(tmp, k)

print(res)


### FIXED CODE ###

import math
K = int(input())

gl = [[0]*K for _ in range(K)]
for i in range(K):
    for j in range(K):
        gl[i][j] = math.gcd(i+1, j+1)

res = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
            tmp = gl[i][j]
            res += gl[tmp-1][k]

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

from sys import stdin
n = int(input())
s = stdin.readline()
rind = []
gind = []
bind = []
ans = 0
for i in range(n):
    if s[i] == "R":
        rind.append(i+1)
    elif s[i] == "G":
        gind.append(i+1)
    else:
        bind.append(i+1)
for i in range(len(rind)):
    for j in range(len(gind)):
        for k in range(len(bind)):
            ind = [rind[i],gind[j],bind[k]]
            a = sorted(ind)
            if a[0] + a[2] != 2*a[1]:
                ans += 1
print(ans)


### FIXED CODE ###

from sys import stdin
n = int(input())
s = stdin.readline()
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
count1 = r*g*b
count2 = 0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1:
            if (s[i] != s[j])and(s[i] != s[2*j-i])and(s[j] != s[2*j-i]):
               count2 += 1
print(count1-count2)
### BUGGY CODE ###

N = int(input())
para = [[c for c in input()] for i in range(N)]
for i in range(N):
  para[i].sort()

def check(a,b):

    if a == b :
        return True 
            
a = 0 
for i in range(N):
    for j in range(i+1,N):
        if check(para[i],para[j]):
            a += 1
print(a)

### FIXED CODE ###

N = int(input())
para = [[c for c in input()] for i in range(N)]
b = []
dict = {}
ans = 0
for i in range(N):
    para[i].sort()
    b.append("".join(para[i]))
    if b[i] not in dict:
        dict[b[i]] = 1
    else :
        ans += dict[b[i]]
        dict[b[i]] += 1 

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

def gcdf(a):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
#    print("a2:",a)
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

#n=8
#a=[746130, 1385670, 4849845, 881790, 3233230, 1939938, 570570, 510510]
#ans=[19]
#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())
a=[]
for i in map(int,input().split()):
    a.append(i)

#print(a)
if n==2:
    print(max(a))
elif n<2**10:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=2**8
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)

    gcdv=0

    for mi in range(m):
        rgcd1=rgcd.copy()
        del rgcd1[mi]
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        for i in range(len(am)):
            acpy=am.copy()
            del acpy[i]
            acpy=acpy+rgcd1
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx

#    print(gcdv,a1)
    print(gcdv)

### FIXED CODE ###

def gcdf(a):
    a.sort()
#    print("a2:",a)
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
#    print("a2:",a)
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

#n=8
#a=[746130, 1385670, 4849845, 881790, 3233230, 1939938, 570570, 510510]
#ans=[19]
#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())
a=[]
for i in map(int,input().split()):
    a.append(i)
if n==2:
    print(max(a))
elif n<2**10:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=int(n**(1/2))
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==m-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)
    gcdv=0

    for mi in range(m):
        rgcd1=list(rgcd)
        del rgcd1[mi]
        gcdrg1=gcdf(rgcd1)
        ms=ns*mi
        if mi==m-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
#        print(mi,gcdf(am),len(am),gcdf(rgcd1))
        for i in range(len(am)):
            acpy=list(am)
#            del acpy[i]
#            acpy.append(gcdrg1)
            acpy[i]=gcdrg1
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx
    print(gcdv)
### BUGGY CODE ###

import heapq

inf = 10**12

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

data = [[] for _ in range(N)]

maxA = 0

for i in range(M):
    U,V,A,B = map(int,input().split())
    maxA = max(maxA,A)
    U -= 1
    V -= 1
    data[U].append((B,V,A))###(距離, 行き先, 費用)
    data[V].append((B,U,A))###(距離, 行き先, 費用)

P = maxA * (N-1)

C = [0]*N
D = [0]*N

for i in range(N):
    C[i],D[i] = map(int,input().split())

dist = [[inf]*(P+1) for _ in range(N)]
#visit = [[0]*(P+1) for _ in range(N)]
###heapにぶち込んだことがあるかないか

q = [(0,0,min(S,P))]###(仮の最短距離, 頂点番号, 所持金)
heapq.heapify(q)

while q:
    v = heapq.heappop(q)
    if v[0] > dist[v[1]][v[2]]:
        continue
    dist[v[1]][v[2]] = v[0]

    if v[2] + C[v[1]] <= P:
        heapq.heappush(q,(v[0]+D[v[1]],v[1],v[2]+C[v[1]]))

    for x in data[v[1]]:
        if x[2] > v[2]:
            continue
        heapq.heappush(q,(v[0]+x[0],x[1],v[2]-x[2]))
    
    ###
    #print(P)
    #print(q)
    #print(v)
    ###

for i in range(1,N):
    print(min(dist[i]))
        



### FIXED CODE ###

import heapq
import sys

input = sys.stdin.readline

inf = 10**13

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

data = [[] for _ in range(N)]

sumA = 0

for i in range(M):
    U,V,A,B = map(int,input().split())
    sumA += A
    U -= 1
    V -= 1
    data[U].append((B,V,A))###(距離, 行き先, 費用)
    data[V].append((B,U,A))###(距離, 行き先, 費用)

P = sumA

C = [0]*N
D = [0]*N

for i in range(N):
    C[i],D[i] = map(int,input().split())

dist = [[inf]*(P+1) for _ in range(N)]
#visit = [[0]*(P+1) for _ in range(N)]
###heapにぶち込んだことがあるかないか

q = [(0,0,min(S,P))]###(仮の最短距離, 頂点番号, 所持金)
heapq.heapify(q)

while q:
    v = heapq.heappop(q)
    if v[0] >= dist[v[1]][v[2]]:
        continue
    dist[v[1]][v[2]] = v[0]

    if v[2] + C[v[1]] <= P:
        if dist[v[1]][v[2]+C[v[1]]] == inf:
            heapq.heappush(q,(v[0]+D[v[1]],v[1],v[2]+C[v[1]]))

    for x in data[v[1]]:
        if x[2] > v[2]:
            continue
        if dist[x[1]][v[2]-x[2]] == inf:
            heapq.heappush(q,(v[0]+x[0],x[1],v[2]-x[2]))
    
    ###
    #print(P)
    #print(q)
    #print(v)
    ###

for i in range(1,N):
    print(min(dist[i]))
### BUGGY CODE ###

import numpy as np

n, x, y = map(int, input().split())

ans = []
for i in range(1, n+1):
  for j in range(i, n+1):
    ji = j - i
    xi = x - i
    jy = j - y
    l = [ji,  np.abs(xi) + 1 + np.abs(jy)]
    m = min(l)
    ans.append(m)
      
    
for k in range(n-1):
  print(ans.count(k+1))


### FIXED CODE ###

n, x, y = map(int, input().split())

ans = [0]*n
for i in range(1, n+1):
  for j in range(i, n+1):
    m = min(j-i,  abs(x-i) + 1 + abs(j-y))
    ans[m] += 1
      
print(*ans[1:], sep = '\n')
### BUGGY CODE ###

import sys
HK,WK,M = map(int, input().split())
hw_s = [list(map(int, input().split())) for _ in range(M)]
W = {}
H = {}
HW = [[0 for i in range(WK+1)] for _ in range(HK+1)]
for i, hw in enumerate(hw_s):
    h=hw[0]
    w=hw[1]

    if not h in H:
        H[h] = 1
    else:
        H[h] += 1
    
    if not w in W:
        W[w] = 1
    else:
        W[w] += 1

    HW[h][w] = 1

answer = 0
HMAXES = []
WMAXES = []
HMAX = max(H.values())
WMAX = max(W.values())

answer = HMAX + WMAX

for ki, vi in H.items():
    if vi == HMAX:
        HMAXES.append(ki)
for ki, vi in W.items():
    if vi == WMAX:
        for h in HMAXES:
            if HW[h][ki] == 0:
                print(answer)
                sys.exit(0)
print(answer-1)

### FIXED CODE ###

import sys
HK,WK,M = map(int, input().split())
W = [0] * (WK + 1)
H = [0] * (HK + 1)
HW = {}
# 3*10^5

for i in range(M):
    h, w = map(int, input().split())

    H[h] += 1
    W[w] += 1

    HW[(h,w)] = 1

answer = 0
HMAX = max(H)
WMAX = max(W)

HEMAX = H.count(HMAX)
WEMAX = W.count(WMAX)
if HEMAX * WEMAX > M:
    print(HMAX + WMAX)
    sys.exit(0)

HMAXES = [i for i, _ in enumerate(H) if _ == HMAX]
WMAXES = [i for i, _ in enumerate(W) if _ == WMAX]

# 3*10^5
answer = HMAX + WMAX
for h in HMAXES:
    for w in WMAXES:
        if not (h,w) in HW:
            print(answer)
            sys.exit(0)
print(answer-1)
### BUGGY CODE ###

n, x, y = map(int, input().split())

cnt = [0] * n
for i in range(1, n + 1):
    for j in range(i + 1, n + 1):
        dist1 = j - i
        dist2 = min(abs(x - i) + abs(y - j), abs(x - j) + abs(y - i)) + 1
        dist = min(dist1, dist2)
        cnt[dist] += 1

print(*cnt[1:], sep="\n")


### FIXED CODE ###

n, x, y = map(int, input().split())

cnt = [0] * n
cycle = y - x + 1

for i in range(1, n + 1):
    for j in range(i + 1, n + 1):
        if i < x and j > y:
            cnt[j - y + x - i + 1] += 1
        elif i < x:
            d = min(j - x, y - j + 1)
            cnt[x - i + d] += 1
        elif j > y:
            d = min(y - i, i - x + 1)
            cnt[j - y + d] += 1
        else:
            d = min(j - i, y - j + 1 + i - x)
            cnt[d] += 1

print(*cnt[1:], sep="\n")
### BUGGY CODE ###

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

# a5 - b5 = (a-b)(a4 + a3b + a2b2 + ab3 + b4)

def check(x):
  for a in range(n//x+1):
    b = a - x
    c = a**4 + a**3 * b + a**2 * b**2 + a * b**3 + b**4
    d = x * c
    if d == n:
      return [a, b]

    a += 1
  return None

for i in range(1, n+1):
  res = check(i)
  if res:
    break

r1, r2 = res
print(r1, r2)
    


### FIXED CODE ###

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

# a5 - b5 = (a-b)(a4 + a3b + a2b2 + ab3 + b4)

def check(x):
  for a in range(200):
    b = a - x
    c = a**4 + a**3 * b + a**2 * b**2 + a * b**3 + b**4
    d = x * c
    if d == n:
      return [a, b]
  return None

for i in range(1, n+1):
  res = check(i)
  if res:
    break

r1, r2 = res
print(r1, r2)
### BUGGY CODE ###

K, S = map(int, input().split())

cnt = 0
for x in range(K+1):
    for y in range(K+1):
        for z in range(K+1):
            if x + y + z == S:
                cnt += 1
print(cnt)


### FIXED CODE ###

K, S = map(int, input().split())

cnt = 0
for x in range(K+1):
    for y in range(K+1):
        z = S - x - y
        if z >= 0 and z <= K:
            cnt += 1
print(cnt)
### BUGGY CODE ###

k, s = map(int, input().split())
count=0
for i in range(k+1):
    for j in range(k+1):
        for l in range(k+1):
            t=i+j+l
            if t == s:
                count += 1
            else:
                pass
print (count)


### FIXED CODE ###

k, s = map(int, input().split())
count=0
for i in range(k+1):
    for j in range(k+1):
        t = s-i-j
        if t <= k and t >= 0:
            count += 1
        else:
            pass
print (count)
### BUGGY CODE ###

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

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

def bis(x,vec):
   left_index_NG=-1
   right_index_OK=len(vec)
   while(left_index_NG<right_index_OK-1):
      mid=int((left_index_NG+right_index_OK)/2)
      if x < vec[mid]:
         right_index_OK=mid
      else:
         left_index_NG=mid
   return right_index_OK

alltypes=0
for i in range(N):
   a=A[i]
   for j in range(N):
      if(A[i]>=B[j]):
         continue
      else:
         b=B[j]
         alltypes+=len(C)-bis(b,C)

print(alltypes)

### FIXED CODE ###

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

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

def bisA(x,vec):
   left_index_OK=-1
   right_index_NG=len(vec)
   while(left_index_OK<right_index_NG-1):
      mid=int((left_index_OK+right_index_NG)/2)
      if x > vec[mid]:
         left_index_OK=mid
      else:
         right_index_NG=mid
   #print(vec[left_index_OK])
   return left_index_OK

def bisC(x,vec):
   left_index_NG=-1
   right_index_OK=len(vec)
   while(left_index_NG<right_index_OK-1):
      mid=int((left_index_NG+right_index_OK)/2)
      if x < vec[mid]:
         right_index_OK=mid
      else:
         left_index_NG=mid
   #print(vec[right_index_OK])
   return right_index_OK

alltypes=0
atypes=0
ctypes=0
for i in range(N):
   b=B[i]
   if b<=A[0]:
      continue
   else:
      atypes=bisA(b,A)+1
      ctypes=len(C)-bisC(b,C)
      alltypes+=atypes*ctypes

print(alltypes)
### BUGGY CODE ###

S = input()
Q = int(input())
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    S = S[::-1]
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      S = C + S
    else:  # F == '2'
      S = S + C
print(S)

### FIXED CODE ###

from collections import deque

S = input()
pre, post = deque(), deque()
Q = int(input())
reversed = False
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    reversed = not reversed
    pre, post = post, pre
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      if reversed:
        pre.append(C)
      else:
        pre.appendleft(C)
    else:  # F == '2'
      if reversed:
        post.appendleft(C)
      else:
      	post.append(C)
if reversed:
  print(''.join(pre)[::-1] + S[::-1] + ''.join(post)[::-1])
else:
  print(''.join(pre) + S + ''.join(post))
### BUGGY CODE ###

INF = float('inf')
N, X, Y = map(int, input().split())
G = [[0 if i == j else INF for j in range(N)] for i in range(N)]
for i in range(N-1):
  G[i][i+1] = G[i+1][i] = 1
G[X-1][Y-1] = G[Y-1][X-1] = 1
for k in range(N):
  for i in range(N):
    for j in range(N):
      G[i][j] = min(G[i][j], G[i][k] + G[k][j])
for k in range(1, N):
  rk = 0
  for i in range(N):
    for j in range(N):
      if j <= i: continue
      if G[i][j] == k:
        rk += 1
  print(rk)

### FIXED CODE ###

N, X, Y = map(int, input().split())
X -= 1
Y -= 1
R = [0 for j in range(N)]
for i in range(N-1):
  for j in range(N):
    if j <= i:
      continue
    xd = abs(i - X)
    yd = abs(j - Y)
    dist = min(
      j - i,
      xd + yd + 1
    )
    R[dist] += 1
for r in R[1:]:
  print(r)
### BUGGY CODE ###

from bisect import bisect_left

N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
ans=0
for i in range(N-2):
  a=L[i]
  for j in range(i+1,N-1):
    b=L[j]
    l=-1
    r=N
    while r-l>1:
      mid=(l+r)//2
      if L[mid]<a+b:
        l=mid
      else:
        r=mid
    ans+=r-(j+1)    
print(ans)


### FIXED CODE ###

from bisect import bisect_left

N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
ans=0

for i in range(N-2):
  a=L[i]
  for j in range(i+1,N-1):
    b=L[j]
    ans+=bisect_left(L,a+b)-(j+1)
print(ans)
### BUGGY CODE ###

N,P = map(int,input().split())
S = list(map(int,list(input())))
S=S[::-1]
ans = 0
from collections import Counter
if P==2:
  l = [0,2,4,6,8]
  for i,s in enumerate(S):
    if s in l:
      ans+=N-i
elif P==5:
  l=[0,5]
  for i,s in enumerate(S):
    if s in l:
      ans+=N-i

else:
  l = [0]*(N+2)
  for i in range(N):
    l[i+1]=l[i]
    l[i+1]+=(S[i]*10**i)%P
    l[i+1]%=P
  l = l[1:N+1:]
  c = Counter(l)
  l = c.items()
  l = sorted(l)
  for k,v in l:
    if k==0:
      ans+=v
    if v==1:
      continue
    else:
      ans+=v*(v-1)//2
      
print(ans)

### FIXED CODE ###

def main():
  N,P = map(int,input().split())
  S = list(map(int,list(input())))
  S=S[::-1]
  ans = 0
  if P==2:
    l = [0,2,4,6,8]
    for i,s in enumerate(S):
      if s in l:
        ans+=N-i
  elif P==5:
    l=[0,5]
    for i,s in enumerate(S):
      if s in l:
        ans+=N-i

  else:
    l = [0]*P
    a=0
    for i in range(N):
      b = pow(10,i,P)
      b *= S[i]
      b %= P
      a+=b
      a%=P
      l[a]+=1
    ans+=l[0]
    for i in l:
      ans+=i*(i-1)//2

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

a, b, c, k = map(int, input().split())

ans = 0
for i in range(k):
    if a != 0:
        ans += 1
        a += -1
        continue
    if b != 0:
        ans += 0
        b += -1
        continue
    if c != 0:
        ans += -1
        c += -1
        continue
print(ans)


### FIXED CODE ###

a, b, c, k = map(int, input().split())

ans = 0

if a > k:
    ans += 1*k
    k = 0
else:
    ans += 1*a
    k -= a

if b > k:
    ans += 0*k
    k = 0
else:
    ans += 0*b
    k -= b

if c > k:
    ans += -1*k
    k = 0
else:
    ans += -1*c
    k -= c


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

import bisect
N, M = list(map(int, input().split(' ')))
As = list(map(int, input().split(' ')))
#  sortしていちばん大きいのをとって半額にしてAsに戻してもう一回ソートするのをM回やれば1e5 にN log N +alpha
As = sorted(As)
trimeed = len(As) - 1
for i in range(M):
    hoge = As[-1] // 2
    As = As[:-1]
    t=bisect.insort(As, hoge)
print(sum(As))

### FIXED CODE ###

import math
import heapq
n, k = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = [-a for a in aaa]
heapq.heapify(aaa)
for i in range(k):  # TLE?
    t = heapq.heappop(aaa)
    t = math.ceil(t/2)
    heapq.heappush(aaa, t)
print(-sum(aaa))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n = int(input())
S = sorted(map(int, input().split()), reverse=True)
L = [S[0]]
used = [False]*(1<<n)
used[0] = True
for i in range(n):
  T = []
  for l in L:
    for j, s in enumerate(S):
      if used[j]:
        continue
      if l > s:
        T.append(s)
        used[j] = True
        break
    else:
      print("No")
      exit()
  L += T
print("Yes")

### FIXED CODE ###

import sys
input = sys.stdin.readline

n = int(input())
S = sorted(map(int, input().split()), reverse=True)
L = [S[0]]
used = [False]*(1<<n)
used[0] = True
for i in range(n):
  L.sort(reverse=True)
  idx = 0
  for j, s in enumerate(S):
    if used[j]:
      continue
    if L[idx] > s:
      L.append(s)
      used[j] = True
      idx += 1
      if idx == 1<<i:
        break
  else:
    print("No")
    exit()
print("Yes")
### BUGGY CODE ###

import sys; readline = sys.stdin.readline
from collections import deque
sys.setrecursionlimit(1000000000)

N, U, V = map(int, readline().split())
AB = [list(map(int, readline().split())) for i in range(N - 1)]

A = [a - 1 for a, b in AB]
B = [b - 1 for a, b in AB]

U, V = U - 1, V - 1

conn = {}
for a, b in zip(A, B):
    if not a in conn:
        conn[a] = []
    conn[a].append(b)

    if not b in conn:
        conn[b] = []
    conn[b].append(a)

t = [0 for i in range(N)]

def dfsU(u, parent, depth):
    t[u] = depth

    for _u in conn[u]:
        if _u != parent:
            dfsU(_u, u, depth + 1)

dfsU(V, -1, 0)

maxv = 0
maxu = U
def dfsV(v, parent, depth):
    global maxv
    global maxu
    if depth < t[v]:
        if t[v] > maxv:
            maxv = t[v]
            maxu = depth
        for _v in conn[v]:
            if _v != parent:
                dfsV(_v, v, depth + 1)

dfsV(U, -1, 0)
if (maxv - maxu) % 2 == 1:
    print(maxv - 1)
else:
    print(maxv - 1)

### FIXED CODE ###

import sys; readline = sys.stdin.readline
from collections import deque
sys.setrecursionlimit(1000000)

N, U, V = map(int, readline().split())
AB = [list(map(int, readline().split())) for i in range(N - 1)]

A = [a - 1 for a, b in AB]
B = [b - 1 for a, b in AB]

U, V = U - 1, V - 1

conn = {}
for a, b in zip(A, B):
    if not a in conn:
        conn[a] = []
    conn[a].append(b)

    if not b in conn:
        conn[b] = []
    conn[b].append(a)

t = [0 for i in range(N)]

def dfsU(u, parent, depth):
    t[u] = depth

    for _u in conn[u]:
        if _u != parent:
            dfsU(_u, u, depth + 1)

dfsU(V, -1, 0)

maxv = 0
maxu = U
def dfsV(v, parent, depth):
    global maxv
    global maxu
    if depth < t[v]:
        if t[v] > maxv:
            maxv = t[v]
            maxu = depth
        for _v in conn[v]:
            if _v != parent:
                dfsV(_v, v, depth + 1)

dfsV(U, -1, 0)
if (maxv - maxu) % 2 == 1:
    print(maxv - 1)
else:
    print(maxv - 1)
### BUGGY CODE ###

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

mod = 1000000007
def pow(x, n):
    ret = 1
    while n > 0:
        if (n & 1) == 1:
            ret = (ret * x) % mod
        x = (x * x) % mod
        n //= 2
    return ret

fac = [1]
inv = [1]
for i in range(1, n * m + 1):
    fac.append((fac[-1] * i) % mod)
    inv.append(pow(fac[i], mod - 2))

def cmb(n, k):
    return ((fac[n] * inv[k]) % mod * inv[n - k]) % mod

def doit(n, m, k):
    ret = 0
    for d in range(m):
        ret = (ret + d * (m - d)) % mod
    return (ret * n * n * cmb(n * m - 2, k - 2)) % mod

print((doit(n, m, k) + doit(m, n, k)) % mod)

### FIXED CODE ###

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

mod = 1000000007
def pow(x, n):
    ret = 1
    while n > 0:
        if (n & 1) == 1:
            ret = (ret * x) % mod
        x = (x * x) % mod
        n //= 2
    return ret

fac = [1]
inv = [1]
for i in range(1, n * m + 1):
    fac.append((fac[-1] * i) % mod)
    inv.append(pow(fac[i], mod - 2))

def cmb(n, k):
    return ((fac[n] * inv[k]) % mod * inv[n - k]) % mod

def doit(n, m, k):
    ret = 0
    for d in range(m):
        ret = (ret + d * (m - d) * n * n * cmb(n * m - 2, k - 2)) % mod
    return ret

print((doit(n, m, k) + doit(m, n, k)) % mod)
### BUGGY CODE ###

a=int(input())
s=input()
count=0
for i in range(a-2):
    for j in range(i+1,a-1):
        if s[i]!=s[j]:
            for h in range(j+1,a):
                if s[i]!=s[h] and s[j]!=s[h] and h+i!=2*j:
                    count+=1
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=int(input())
a=[]
b=[]
c=[0]*N
cnt=0
for i in range(N):
  a.append(list(map(int, input().split())))
  a[i].append(i+1)
#print(a)

for i in range(N):
  b.append(a[i][0])
#print('b='+str(b))
for j in range(N*(N-1)//2):
  for i in range(N):
    if b[i]!=i+1:
      if i==b[b[i]-1]-1:
        c[i]+=1
        c[b[i]-1]+=1
  #print('c='+str(c))
  for i in range(N):
    b[i]=a[i][c[i]//2]
  #print('b='+str(b))
  cnt+=1
  if c==[(N-1)*2]*N:
    print(cnt)
    exit()

print('-1')

### FIXED CODE ###

import sys
from collections import deque

n = int(input())
matches = [[a - 1 for a in map(int, line.split())] for line in sys.stdin]
#print(matches)
q = deque(range(n))
#print(q)
depth = [0] * n
waiting = [-1] * n

while q:
    a = q.popleft()
    b = matches[a].pop()
    #print(a, b)
    if waiting[b] == a:
        depth[a] = depth[b] = max(depth[a], depth[b]) + 1
        if matches[a]:
            q.append(a)
        if matches[b]:
            q.append(b)
    else:
        waiting[a] = b

if any(matches):
    print(-1)
else:
    print(max(depth))