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 = 0
while pos != comp && flag == count
  count += 1
  n.times do |i|
    next if pos[i] == n - 1 || a[i][pos[i]] == true
    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 = count if flag != count
    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
