N = gets.to_i
A = [[]]
N.times{|i|
  A << gets.chomp.split(" ").map(&:to_i)
}

ans = 0
pair = []
begin
  pair.clear
  (0...N).each{|i|
    next if A[i].empty?
    o = A[i][0]
    (pair << i; pair << o) if A[o][0] == i
  }
  pair.uniq.sort.reverse.each{|i|
    A[i].shift
  }
  ans += 1
end until pair.empty? || A.all?{|a| a.empty?}
ans = -1 if pair.empty?

puts ans
