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

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

puts A.all?{|a| a.empty?} ? ans - 1 : -1
