n = gets.chomp.to_i
a = []
n.times do |i|
  a[i] = gets.chomp.split(" ").map{|tmp| tmp.to_i - 1}
end

current_indexes = Array.new(n, 0)

days = 0

loop do
  days += 1
  current_drop_indexes = {}
  current_indexes.each_with_index do |index, i|
    next if current_drop_indexes[index] == 1
    next if index == n
    target = a[index][i]
    current = current_indexes[target]
    next if current >= n
    target_a = a[target]
    if target_a[current] == i
      current_drop_indexes[i] = 1
      current_drop_indexes[target] = 1
    end
  end
  keys = current_drop_indexes.keys
  if keys.size == 0
    # failed
    puts "-1"
    exit
  end
  keys.each do |i|
    current_indexes[i] += 1
  end
  if current_indexes.all?{|index| index == n}
    # success
    puts days
    exit
  end
end
