require "set"
n = gets.chomp.to_i

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

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