CodeEff/ECCO huggingface dataset:
# Original solution
N, K = [int(nk) for nk in input().split()]

ans = 0
# a+b=pK b+c=qK c+a=rK
for p in range(1, ((2*N)//K)+1):#1<=p<=(2*N)//K
  for q in range(1, ((2*N)//K)+1):
    b0 = (( p+q-1)*K)/2
    if b0 < 1:
      continue
    for r in range(1, ((2*N)//K)+1):
      a = (( p-q+r)*K)/2
      b = (( p+q-r)*K)/2
      c = ((-p+q+r)*K)/2
      if b < 1:
        break
      elif a > N or c > N:
        break
      elif b > N:
        continue
      elif a < 1 or c < 1:
        continue
      elif a == int(a) and b == int(b) and c == int(c):
        ans += 1
        
print(ans)

# Optimized solution
N, K = [int(nk) for nk in input().split()]

if K % 2 == 1:
  print(((N//K)**3))
else:
  ans1 = (N//K)**3
  ans2 = ((N-(K//2))//K + 1)**3
  print((ans1 + ans2))
# Original solution
N, K = list(map(int,input().split()))
a, b = 0, 0
for i in range(1, N+1):
  if(i%K == 0):
    a += 1
if(K%2 == 0):
  for i in range(1, N+1):
    if(i%K == K//2):
      b += 1
print((a**3 + b**3))

# Optimized solution
N, K = list(map(int,input().split()))
k = N//K
ans = k**3
if(K%2 == 0):
  if(K*k+K//2 <= N):
    ans += (k+1)**3
  else:
    ans *= 2
print(ans)
# Original solution
N, K = list(map(int, input().split()))

ans = 0
for i in range(1,N+1):
  for j in range(1,N+1):
      for k in range(1,N+1):
        if ((i + j) % K == 0 and (j + k) % K == 0 and (k + i) % K == 0):
          ans += 1
print(ans)

# Optimized solution
N, K = list(map(int, input().split()))

ans = 0
if (K % 2 != 0):
  ans = (N // K) ** 3
else:
  for i in range(1,N+1):
    if (i % K == int(K / 2)):
      ans += 1
  ans = ans ** 3 + (N // K) ** 3
print(ans)
# Original solution
n,k=list(map(int,input().split()))
count,point=0,0
if k%2!=0:
    while k*(count+1)<=n:
        count +=1
    print((count**3))
else:
    for i in range(1,n+1):
        if i%k==0:point +=1
        elif i*2%k==0:count +=1
    print((count**3+point**3))

# Optimized solution
n,k=list(map(int,input().split()))
A,B=0,0
for i in range(1,n+1):
    if i%k==0:A +=1
    elif 2*i%k==0:B +=1
print((A**3+B**3))
# Original solution
N,K=list(map(int,input().split()))
ans=0
a=int(2*N/K)
for i in range(1,a+1):
  if i*K%2==1:
    continue
  for j in range(1,a+1):
    if j*K%2==1:
      continue
    for l in range(1,a+1):
      if l*K%2==1:
        continue
      if ((i+j)*K//2)%K==((l+j)*K//2)%K==((i+l)*K//2)%K==0:
        ans+=1
print(ans)

# Optimized solution
N,K=list(map(int,input().split()))
ans=0
if K%2==1:
  print(((N//K)**3))
else:
  ans=(N//K)**3
  k=K//2
  j=(N+k)//K
  print((j**3+ans))
# Original solution
n,k=list(map(int,input().split()))
ans=0
if k%2==0:
    hanbun=len([i for i in range(k//2,n+1,k)])
    ans+=hanbun**3
baisuu=len([i for i in range(k,n+1,k)])
ans+=baisuu**3
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
ans=0
baisuu=n//k
ans+=baisuu**3
if k%2==0:
    if baisuu*k+k//2<=n:
      hanbun=baisuu+1
    else:
      hanbun=baisuu
    ans+=hanbun**3
print(ans)
# Original solution
n,k=list(map(int,input().split()))
ans=0
baisuu=n//k
ans+=baisuu**3
if k%2==0:
    if baisuu*k+k//2<=n:
      hanbun=baisuu+1
    else:
      hanbun=baisuu
    ans+=hanbun**3
print(ans)

# Optimized solution
def main():
    n,k=list(map(int,input().split()))
    ans=0
    baisuu=n//k
    ans+=baisuu**3
    if k%2==0:
        if baisuu*k+k//2<=n:
            hanbun=baisuu+1
        else:
            hanbun=baisuu
        ans+=hanbun**3
    print(ans)
if __name__=="__main__":
    main()
# Original solution
n, k = list(map(int, input().split()))
ll = 2*n // k
ans = 0
for i in range(1, ll+1):
    for j in range(1, ll+1):
        for m in range(1, ll+1):
            if ((m+i-j)*k) % 2 == 0 and ((i-m+j)*k) % 2 == 0 and ((m-i+j)*k) % 2 == 0:
                if (m+i-j) > 0 and (i-m+j) > 0 and (m-i+j) > 0:
                    if (m+i-j)*k//2 <= n and (i-m+j)*k//2 <= n and (m-i+j)*k//2 <= n:
                        ans += 1
print(ans)


# Optimized solution
n, k = list(map(int, input().split()))
ll = n // k
if ll % 2 == 0:
    l_odd = ll // 2
    l_even = ll // 2
else:
    l_odd = ll // 2 + 1
    l_even = ll // 2
if k % 2 == 0:
    ans = ll ** 3
    if n >= ll*k + k/2:
        ans += (ll+1)**3
    else:
        ans += ll**3
    print(ans)
else:
    print((ll ** 3))
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())

def main():
  n,k=LI()

  ans=0
  ans+=(n//k)**3

  if k%2==0:
    _a=0
    for i in range(1,n+1):
      if i%k==k//2:
        _a+=1
    ans+=_a**3

  return ans

# main()
print((main()))


# Optimized solution
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())

def main():
  n,k=LI()
  if k%2==0:
    return (n//(k//2)-(n//k))**3+(n//k)**3
  return (n//k)**3

# main()
print((main()))
# Original solution
n,k = list(map(int,input().split()))
num = [0] * k
ans = 0
for i in range(1,n + 1):
  num[i%k] += 1
for a in range(k):
  b = (k - a) % k
  c = (k - a) % k
  if (b + c) % k != 0:
    continue
  ans += num[a] * num[b] * num[c]
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
def gcd(a,b):
  if b == 0:
    return a
  return gcd(b,a % b)
def lcm(a,b):
  return a * b // gcd(a,b)
if k % 2 != 0:
  elements = n // k
  print((elements**3))
else:
  a = n // k
  b = (n + (k//2)) // k
  print((a**3 + b ** 3))
# Original solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = list(map(int, readline().split()))

    R = [0] * K
    for i in range(1, N + 1):
        R[i % K] += 1

    ans = 0
    for a in range(K):
        if 2 * (K - a) % K == 0:
            ans += R[a] * R[(K - a) % K] ** 2

    print(ans)
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = list(map(int, readline().split()))

    ans = pow(N // K, 3)
    if K % 2 == 0:
        ans += pow((N + K // 2) // K, 3)

    print(ans)
    return


if __name__ == '__main__':
    main()
# Original solution
N, K = list(map(int, input().split()))
c = 0
for i in range(1, N + 1):
    a = 0
    if (i + i) % K == 0:
        for j in range(1, N + 1):
            if (i + j) % K == 0:
                a += 1
    c += a * a
print(c)


# Optimized solution
N, K = list(map(int, input().split()))
c = N // K
d = 0
for i in range(1, N + 1):
    if K % 2 == 0 and i % K == K // 2:
        d += 1
print((c ** 3 + d ** 3))
# Original solution
N,K = list(map(int, input().split()))

cnt = 0
ans = []

for i in range(1,N+1):
    for j in range(1,N+1):
        for k in range(1,N+1):
            if (i + j) % K == 0 and (j + k) % K == 0 and (k + i) % K == 0:
                cnt += 1
                ans.append([i,j,k])
print(cnt)


# Optimized solution
n,k = list(map(int,input().split()))

if k%2==1:
    ans = (n//k)**3
else:
    even = n//k
    odd  = (2*n)//k - even
    ans = even**3 + odd**3
print(ans)
# Original solution
def calc(n, k, x):
    c = 0

    while x <= n:
        y = z = x

        while 0 < y:
            z = y

            while 0 < z:
                if x == y and y == z:
                    c += 1
                elif x == y or y == z:
                    c += 3
                else:
                    c += 6

                z -= k

            y -= k

        x += k

    return c

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

if k % 2 == 0:
    print((calc(n, k, k) + calc(n, k, k // 2)))
else:
    print((calc(n, k, k)))

# Optimized solution
def calc(n, k, x):
    c = 0

    while x <= n:
        a  = (x - 1) // k
        c += 1 + 6 * (1 + a) * a // 2
        x += k

    return c

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

if k % 2 == 0:
    print((calc(n, k, k) + calc(n, k, k // 2)))
else:
    print((calc(n, k, k)))
# Original solution
n, k = list(map(int, input().split()))

if k % 2 == 1:
    ans = (n // k) ** 3
else:
    num = n // k
    num2 = n // (k // 2) - num
    ans = num ** 3 + num2 ** 3
    
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))

ans = 0
if k % 2 == 1:
    ans += (n // k) ** 3
else:
    ans += (n // k) ** 3
    ans += ((n // (k // 2)) - (n // k)) ** 3
    
print(ans)
# Original solution
N, K = list(map(int, input().split()))
ret = 0
for x in range(1, N + 1):
    for y in range(K - x, N + 1, K):
        for z in range(K - y, N + 1, K):
            if  y > 0 and z > 0 and (z + x) % K == 0:
                ret += 1
print(ret)

# Optimized solution
N, K = list(map(int, input().split()))
n = sum([1 for x in range(K, N + 1, K)])
if K % 2 == 1:
    print((n ** 3))
else:
    m = sum([1 for x in range(K // 2, N + 1, K)])
    print((n ** 3 + m ** 3))
# Original solution
N,K = [int(i) for i in input().split()]

candidates = []
for i in range(1,N+1) :
	i = int(i)
	if (i % K == 0) :
		# Kの倍数は候補にする
		candidates.append(i)
		continue
	
	if (K % 2 == 0 and i % (K/2) == 0) :
		# Kが偶数だったらK/2も候補としてよい
		candidates.append(i)
		continue

count = 0
for a in candidates :
	for b in candidates :
		for c in candidates :
			if (int(a+b) % K == 0 and int(b+c) % K == 0 and int(c+a) % K == 0) :
				count += 1

print(count)

# Optimized solution
import itertools as it
N,K = [int(i) for i in input().split()]

candidates = []
even = []
odd = []
for i in range(1,N+1) :
	i = int(i)
	if (i % K == 0) :
		# Kの倍数は候補にする
		even.append(i)
		continue
	
	if (K % 2 == 0 and i % (K/2) == 0) :
		# Kが偶数だったらK/2も候補としてよい
		odd.append(i)
		continue

# a,b,cの組み合わせ(Kが奇数のとき)
count = len(even) * len(even) * len(even)

# a,b,cの組み合わせ(Kが偶数のとき)
# すべての要素がK/2で揃っているケース
if (K % 2 == 0) :
	count += (len(odd) * len(odd) * len(odd))

print(count)
# Original solution
import sys
n=int(eval(input()))
l=[list(map(int,input().split())) for i in range(n)]
ct=[0 for i in range(n)]
fin=0
day=0
count=0
check=[0,0]
while count!=n*(n-1)//2:
  L=[0 for i in range(n)]
  f1=0
  for i in range(n):
    if L[i]==0 and ct[i]!=n-1:
      x=l[i][ct[i]]
      if l[x-1][ct[x-1]]-1==i and L[x-1]==0:
        ct[i]+=1
        ct[x-1]+=1
        L[i]=1
        L[x-1]=1
        count+=1
  day+=1
  check[0]=check[1]
  check[1]=count
  if check[0]==check[1]:
    print((-1))
    sys.exit()
print(day)

# Optimized solution
import sys
n=int(eval(input()))
l=[list(map(int,input().split())) for i in range(n)]
ct=[0 for i in range(n)]
fin=0
day=0
count=0
check=[0,0]
while count!=n*(n-1)//2 and day<=10**4:
  L=[0 for i in range(n)]
  f1=0
  for i in range(n):
    if L[i]==0 and ct[i]!=n-1:
      x=l[i][ct[i]]
      if l[x-1][ct[x-1]]-1==i and L[x-1]==0:
        ct[i]+=1
        ct[x-1]+=1
        L[i]=1
        L[x-1]=1
        count+=1
  day+=1
  check[0]=check[1]
  check[1]=count
  if check[0]==check[1]:
    print((-1))
    sys.exit()
if day>10**4:
  print((n*(n-1)//2))
  sys.exit()
print(day)
# Original solution
import sys

input = sys.stdin.readline
N = int(eval(input()))
import collections

As = [collections.deque()]
for i in range(N):
    As.append(collections.deque(list(map(int, input().split()))))

Ks = [False] * (N + 1)
d = 1
ms = 0
msMax = N * (N - 1) // 2
sw = True
tab = set()
wa = [0] * (N + 1)
while sw:
    wac = 0
    c = 0
    e = N + 1
    f = N - 1
    for i in range(1, e):
        if Ks[i] or len(As[i]) == 0:
            continue
        p = As[i].popleft()
        if i > p:
            a = p
            b = i
        else:
            a = i
            b = p
        if (a, b) in tab:
            ms = ms + 1
            c = c + 1
            if ms == msMax:
                sw = False
                print(d)
                break
            tab.remove((a, b))
            Ks[a] = False
            wa[wac] = b
            wac = wac + 1
        else:
            tab.add((a, b))
            Ks[i] = True
    for i in range(N):
        if wa[i] == 0:
            break
        Ks[wa[i]] = False
        wa[i] = 0
    d = d + 1
    if c == 0:
        print((-1))
        break

# Optimized solution
import sys

input = sys.stdin.readline
N = int(eval(input()))
As = [list(map(int, input().split())) + [-1] for _ in range(N)]

import collections

que = collections.deque([i for i in range(N)])
counters = [0] * N
days = [0] * N
while que:
    player = que.popleft()
    opponent = As[player][counters[player]] - 1
    if As[opponent][counters[opponent]] - 1 == player:
        days[player] = max(days[player], days[opponent]) + 1
        days[opponent] = days[player]
        counters[player] += 1
        counters[opponent] += 1
        que.append(player)
        que.append(opponent)
for c in counters:
    if c < N - 1:
        print((-1))
        break
else:
    print((max(days)))
# Original solution
from collections import deque
import sys
import copy

def main():
  input = sys.stdin.readline
  n = int(eval(input()))
  a = [None]*n
  for i in range(n):
    a[i] = [int(x)-1 for x in input().split()]
  
  count = 0
  ans = 0
  bef_checked = [int(x) for x in range(n)]
  index = [0]*n
  while True:
    ans += 1
    check = copy.deepcopy(bef_checked)
    bef_checked = []
    already = set()
    flag = True
    for v in check:
      if v in already:
        continue
      flag = False
      v2 = a[v][index[v]]
      if v2 in already:
        continue
      if v != a[v2][index[v2]]:
        continue
      already = already|{v, v2}
      index[v] += 1
      index[v2] += 1
      if index[v] == n-1:
        count += 1
      else:
        bef_checked.append(v)
      if index[v2] == n-1:
        count += 1
      else:
        bef_checked.append(v2)
        
    if flag:
      print((-1))
      sys.exit()
    if count == n:
      break
  print(ans)

  
if __name__ == "__main__":
  main()

# Optimized solution
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(eval(input()))
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print((-1))
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()
# Original solution


from collections import deque


def main():
        
    N = int(eval(input()))
    A = [deque([int(x) - 1 for x in input().split()]) for _ in range(N)]

    match_order = {k:v for k,v in enumerate(A)}

    cnt_finished = 0

    days = 1
    flag = True
    while flag:
        flag = False
        used = set()
        complete = []
        for i, order in list(match_order.items()):

            if not order:
                continue
            if i in used:
                continue
            j = order[0]
            if j in used:
                continue
            # それぞれの人について、残りの順番で初めに戦う相手がお互いになっている = その段階で試合ができる
            if A[j][0] == i:
                A[i].popleft()
                A[j].popleft()
                used.add(i)
                used.add(j)
                flag = True
                # N-1人と戦い終わった人は次以降調べない
                if not A[i]:
                    cnt_finished += 1
                    complete.append(i)
                if not A[j]:
                    cnt_finished += 1
                    complete.append(j)
                #print(i,j, cnt_finished)

        if not flag:
            print((-1))
            exit()
        
        if cnt_finished == N:
            print(days)
            exit()
        else:
            for i in complete:
                del match_order[i]
            days += 1

if __name__ == "__main__":
    main()

# Optimized solution

from collections import deque

N = int(eval(input()))
match_order = [deque([int(x) - 1 for x in input().split()]) for _ in range(N)]


q = deque(list(range(N)))
finish_by = [0] * N
next_opponent = [None] * N

while q:
    a = q.popleft()
    # aの残りの順で一番初めに戦う対戦相手を取り出す
    b = match_order[a].popleft()
    if next_opponent[b] == a:
        finish_by[a] = finish_by[b] = max(finish_by[a], finish_by[b]) + 1
        # a,bについて、まだ対戦相手が残っているならキューに積む
        if match_order[a]:
            q.append(a)
        if match_order[b]:
            q.append(b)
    # bの残りの順で戦う相手がaではないなら、順番が来るまで待つ
    else:
        next_opponent[a] = b

# 対戦相手が残っている人がいる場合、条件通りに終わることができない
if any(match_order):
    print((-1))
else:
    print((max(finish_by)))
# Original solution

from collections import deque

N = int(eval(input()))
match_order = [deque([int(x) - 1 for x in input().split()]) for _ in range(N)]


q = deque(list(range(N)))
finish_by = [0] * N
next_opponent = [None] * N

while q:
    a = q.popleft()
    # aの残りの順で一番初めに戦う対戦相手を取り出す
    b = match_order[a].popleft()
    # 互いの初めに戦うべき相手がお互いであれば、そのペアで試合を行う
    if next_opponent[b] == a:
        # a,bが今までに試合した回数の多いほう+1（同じ人が日に二度試合を行えないので）
        finish_by[a] = finish_by[b] = max(finish_by[a], finish_by[b]) + 1
        # a,bについて、まだ対戦相手が残っているならキューに積む
        if match_order[a]:
            q.append(a)
        if match_order[b]:
            q.append(b)
    # bの残りの順で戦う相手がaではないなら、順番が来るまで待つ
    else:
        next_opponent[a] = b

# 対戦相手が残っている人がいる場合、条件通りに終わることができない
if any(match_order):
    print((-1))
else:
    print((max(finish_by)))

# Optimized solution
"""

ある試合(a,b)が行われるまでに消化しておくべき試合を管理して、それが0になった組からキューに積む
みたいな感じで雑にやるとTLEが取れなかった。


LeetCodeのTwoSum的に、
a -> bで試合ができない場合は、情報を残しておいて、b -> a が回ってきた時に試合が実施されるようにするとよいっぽい
"""
from collections import deque

N = int(eval(input()))
match_order = [deque([int(x) - 1 for x in input().split()]) for _ in range(N)]


q = deque(list(range(N)))
finish_by = [0] * N
next_opponent = [None] * N

while q:
    a = q.popleft()
    # aの残りの順で一番初めに戦う対戦相手を取り出す
    b = match_order[a].popleft()
    # 互いの初めに戦うべき相手がお互いであれば、そのペアで試合を行う
    if next_opponent[b] == a:
        # a,bが今までに試合した回数の多いほう+1（同じ人が日に二度試合を行えないので）
        finish_by[a] = finish_by[b] = max(finish_by[a], finish_by[b]) + 1
        # a,bについて、まだ対戦相手が残っているならキューに積む
        if match_order[a]:
            q.append(a)
        if match_order[b]:
            q.append(b)
    # bの残りの順で一番初めに戦う相手がaではないなら、順番が来るまで待つ
    # このあと(b,c)の試合が行われて、キューにb,cが積まれるとかで、aがbと試合するとかがあれば、消化される
    else:
        next_opponent[a] = b

# 対戦相手が残っている人がいる場合、条件通りに終わることができない
if any(match_order):
    print((-1))
else:
    print((max(finish_by)))
# Original solution
"""

ある試合(a,b)が行われるまでに消化しておくべき試合を管理して、それが0になった組からキューに積む
みたいな感じで雑にやるとTLEが取れなかった。


LeetCodeのTwoSum的に、
a -> bで試合ができない場合は、情報を残しておいて、b -> a が回ってきた時に試合が実施されるようにするとよいっぽい
"""
from collections import deque

N = int(eval(input()))
match_order = [deque([int(x) - 1 for x in input().split()]) for _ in range(N)]


q = deque(list(range(N)))
finish_by = [0] * N
next_opponent = [None] * N

while q:
    a = q.popleft()
    # aの残りの順で一番初めに戦う対戦相手を取り出す
    b = match_order[a].popleft()
    # 互いの初めに戦うべき相手がお互いであれば、そのペアで試合を行う
    if next_opponent[b] == a:
        # a,bが今までに試合した回数の多いほう+1（同じ人が日に二度試合を行えないので）
        finish_by[a] = finish_by[b] = max(finish_by[a], finish_by[b]) + 1
        # a,bについて、まだ対戦相手が残っているならキューに積む
        if match_order[a]:
            q.append(a)
        if match_order[b]:
            q.append(b)
    # bの残りの順で一番初めに戦う相手がaではないなら、順番が来るまで待つ
    # このあと(b,c)の試合が行われて、キューにb,cが積まれるとかで、aがbと試合するとかがあれば、消化される
    else:
        next_opponent[a] = b

# 対戦相手が残っている人がいる場合、条件通りに終わることができない
if any(match_order):
    print((-1))
else:
    print((max(finish_by)))

# Optimized solution


from collections import deque


N = int(eval(input()))
A = [deque([int(x) - 1 for x in input().split()]) for _ in range(N)]


next_vs = [None] * N
day_finished = [0] * N
q = deque()
for i in range(N):
    q.append(i)

"""
・試合したらそいつらをキューに積む
・試合できなかったら、次はこいつと戦いたいよリストにいれる

キューを全て確認したのに、　次はこいつと戦いたいよリストが残っている/未試合の相手が残っている　状態であれば、与えられた順では試合ができない
"""

while q:
    x = q.popleft()
    opp_x = A[x].popleft()

    if next_vs[opp_x] == x:
        day_finished[x] = day_finished[opp_x] = max(day_finished[x], day_finished[opp_x]) + 1
        if A[x]:
            q.append(x)
        if A[opp_x]:
            q.append(opp_x)

    else:
        next_vs[x] = opp_x

for i in range(N):
    if A[i]:
        print((-1))
        exit()


print((max(day_finished)))
# Original solution
import sys
# input = sys.stdin.buffer.readline


N = int(eval(input()))
A = [list(map(int, input().split()))[::-1] for _ in range(N)]
day = 0
cnt = N
Last = [True] * N
while cnt:
    flag = True
    Done = [False] * N
    for i, a in enumerate(A):
        if not Last[i]:
            continue
        if len(a) == 0:
            continue
        if Done[i]:
            continue
        x = a[-1] - 1
        if A[x] and (not Done[x]) and (A[x][-1] - 1 == i):
            A[i].pop()
            A[x].pop()
            Done[i] = True
            Done[x] = True
            flag = False
            if not A[i]:
                cnt -= 1
            if not A[x]:
                cnt -= 1
    Last = Done
    if flag:
        break
    day += 1
if cnt:
    print((-1))
else:
    print(day)

# Optimized solution
import sys
# input = sys.stdin.buffer.readline


N = int(eval(input()))
A = [list(map(int, input().split()))[::-1] for _ in range(N)]
day = 0
cnt = N
Last = set([i for i in range(N)])
while cnt:
    flag = True
    Done = [False] * N
    L = set()
    for i in Last:
        if len(A[i]) == 0:
            continue
        if Done[i]:
            continue
        x = A[i][-1] - 1
        if A[x] and (not Done[x]) and (A[x][-1] - 1 == i):
            A[i].pop()
            A[x].pop()
            Done[i] = True
            Done[x] = True
            L.add(i)
            L.add(x)
            flag = False
            if not A[i]:
                cnt -= 1
            if not A[x]:
                cnt -= 1
    Last = L
    if flag:
        break
    day += 1
if cnt:
    print((-1))
else:
    print(day)