N = gets.to_i
As = readlines.map{|l| l.split.map(&:to_i)}

as = [nil] + As
q, x = [*1 .. N], 0
loop do
  d = q.each_with_object([]){|a, o| b = as[a].first; o << a << b if b && as[b].first == a}
  break if d.empty?
  x += 1
  q = d.uniq
  q.each{|a| as[a].shift}
end
puts as.compact.flatten.empty? ? x : -1