n = gets.to_i
a = [[]]
n.times do
  a << gets.split.map(&:to_i)
end

ans = 0
(n * 2).times do |i|
  done = []
  (1..n).each do |j|
    next if done.include?(j)
    next if a[j].empty?
    x = a[j][0]
    y = a[x][0]
    if j == y
      done << j
      done << x
    end
  end
  done.each do |x|
    a[x].shift
  end
  ans += 1 

  c = 0
  a.each do |x|
    c += 1 if x.empty?
  end
  if c > n
    puts ans
    exit
  end
end

puts -1
