n = gets.to_i
arr = []
n.times do
  arr.push(gets.chomp.split(' ').map{ |i| i.to_i - 1 })
end
impossible, finish = false, false
count = 0
until finish do
  exec = false
  all_zero = true
  map = {}
  arr.each_with_index do |v, i|
    vs1 = v.first
    next if vs1.nil?
    vs2 = arr[vs1].first
    next if vs2.nil?
    if vs2 == i
      map[i] = 1
      map[vs1] = 1
    end
  end
  map.each_key do |k|
    arr[k].delete_at(0)
    exec = true
  end
  arr.each do |i|
    if i.count != 0
      all_zero = false
      next
    end
  end
  count += 1
  finish = !exec || all_zero
  impossible = !all_zero
end

puts impossible ? -1 : count