n = gets.to_i
a = n.times.map{gets.chomp.split.map{|i| i.to_i - 1}}
pos = Array.new(n,0)
count = 0
comp = Array.new(n, n - 1)
flag = true
while pos != comp && flag == true
  flag = false
  count += 1
  n.times do |i|
    if pos[i] != n - 1 && a[i][pos[i]] != true then
      if a[a[i][pos[i]]] [pos[a[i][pos[i]]]] == i then
        a[a[i][pos[i]]][pos[a[i][pos[i]]]] = true
        a[i][pos[i]] = true
        flag = true if flag != true
      end
    end
  end
  n.times do |i|
    pos[i] += 1 if pos[i] != n - 1 && a[i][pos[i]] == true
  end
end

puts flag ? count : -1
