n = gets.chomp.to_i

hash = {}
(1..n).each do |i|
  hash[i] = gets.chomp.split(" ").map(&:to_i)
end

days = 0
while hash.size > 0 do
  set = {}
  flag = true
  hash.each do |k, arr|
    pair = arr[0]
    next if set[k] || set[pair]
    arr2 = hash[pair]
    tmp_k = arr2[0]
    if tmp_k == k then
      _ = arr.shift
      _ = arr2.shift
      hash.delete(k) if arr.length == 0
      hash.delete(pair) if arr2.length == 0
      flag = false
      set[k] = true
      set[pair] = true
    end
  end
  if flag then
    puts "-1"
    exit 0
  end
  days += 1
end
puts days
