n = gets.to_i
arr = []
indexes = Array.new(n)
indexes.fill(0)
n.times do
  arr.push(gets.chomp.split(' ').map{ |i| i.to_i - 1 })
end
finish = false
delete_count, all_count = 0, n * (n - 1)
count = 0
until finish do
  exec = false
  map = Hash.new(-1)
  arr.each_with_index do |v, i|
    next if indexes[i] >= n - 1
    vs1 = v[indexes[i]]
    next if indexes[vs1] >= n - 1
    vs2 = arr[vs1][indexes[vs1]]
    next if map[i] == 1 || map[vs1] == 1
    if vs2 == i
      map[i] = 1
      map[vs1] = 1
      indexes[i] += 1
      indexes[vs1] += 1
      delete_count += 2
      exec = true
    end
  end
  count += 1 if exec
  finish = !exec
end

puts delete_count != all_count ? -1 : count