n = gets.to_i
a = Array.new(n+1)
for i in 1..n
  a[i] = gets.split.map(&:to_i)
end

ids = Array.new(n+1, 0)
days = 0
cnt = 0
flag = 0
while cnt < n*(n-1)
  flag = 0
  incid = Array.new(0)
  inccnt = 0
  for i in 1..n
    if ids[i] <= n-2
      if a[a[i][ids[i]]][ids[a[i][ids[i]]]] == i
        flag = 1
        cnt += 1
        incid.push(i)
        inccnt += 1
      end
    end
  end
  days += 1
  if flag == 0
    break
  else
    for i in 0..(inccnt-1)
      ids[incid[i]] += 1
    end
  end
end

if flag == 0
  puts -1
else
  puts days
end