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

count = 0
day = 0
while count < (n*n - n)/2
  day += 1
  today_count = 0
  check_list = Array.new(n,false)
  n.times do |i|
    next if check_list[i] == true
    check_list[i] = true
    j = a[i][0]
    next if j == nil || check_list[j] == true
    if i == a[j][0]
      check_list[j] = true
      today_count += 1
      a[i].shift
      a[j].shift
    end
  end

  if today_count == 0
    day = -1
    break
  end
  count += today_count
end

puts(day)