require "set"
n = gets.chomp.to_i

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

days = 0
set = nil
while hash.size > 0 do
  set = Set.new
  flag = true
  hash.each do |k, arr|
    next if arr.length == 0
    pair = arr[0]
    arr2 = hash[pair]
    if !set.include?(k) && !set.include?(pair) && arr2[0] == k then
      hash[k] = arr[1..-1]
      hash[pair] = arr2[1..-1]
      hash.delete(k) if arr.length == 1
      hash.delete(pair) if arr2.length == 1
      flag = false
      set.add(k)
      set.add(pair)
    end
  end
  days += 1
  if flag then
    puts "-1"
    exit 0
  end
end
puts days
