n = gets.to_i
a = n.times.map{gets.chomp.split.map(&:to_i)}
pos = Array.new(n,0)
count = 0
comp = Array.new(n, n - 1)

while pos != comp && count < n * n
  count += 1
  n.times do |i|
    if pos[i] != n - 1 && a[a[i][pos[i]] - 1] [pos[a[i][pos[i]] - 1]] == i + 1 then
      a[a[i][pos[i]] - 1] [pos[a[i][pos[i]] - 1]] = 0
      a[i][pos[i]] = 0
    end
  end
  n.times do |i|
    pos[i] += 1 if pos[i] != n - 1 && a[i][pos[i]] == 0
  end
end

puts count != n * n ? count : -1