require "set"
n = gets.chomp.to_i

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

count = 0
set = nil
while hash.size >= 1 do
  set = Set.new
  hash.each do |k, arr|
    next if set.include?(k)
    a = arr[0]
    next if set.include?(a)
    set.add(k)
    set.add(a)
    if a.nil? then
      puts hash.inspect
      exit 0
    end
    arr2 = hash[a]
    if arr2[0] == k then
      hash[k] = arr[1..-1]
      hash[a] = arr2[1..-1]
      if arr.length == 1 then
        hash.delete(k)
      end
      if arr2.length == 1 then
        hash.delete(a)
      end
    end
  end
  count += 1
  if count >= n * n then
    puts -1
    exit 0
  end
end
puts count
