n = gets.to_i
list = Array.new(n)
check = Array.new(n,0)
day = 0
n.times do |i|
list[i] = gets.strip.split(" ").map{|e|e.to_i}
end

while(1)

n.times do |i|
  next if list[i].size == 0
  next if check[i] == 1 or check[list[i][0]-1] == 1

  if list[list[i][0]-1][0] == i+1
    if check[i] == 1 or check[list[i][0]-1] == 1 
      next
    else
      check[i] = 1
      check[list[i][0]-1] = 1
      list[list[i][0]-1].shift
      list[i].shift
    end
  else
    p -1
    exit
  end
end

day += 1
check = Array.new(n,0)
break if list.flatten.size == 0

end

p day