n = gets.chomp.to_i

days = []
n.times {|_| days << gets.split(' ').map(&:to_i) }
current = n.times.map{|i| 0 }
finish = n.times.map{|i| n-1 }


score = 0
while current != finish do
  score += 1
  memo = []
  current.each.with_index(1) do |i, index|
    next if i == n - 1
    
    aite = days[index-1][i]
    if days[aite-1][current[aite-1]] == index
      memo << index
    end
  end
  
  if memo.empty?
    score = -1
    break
  end
  
  memo.each do |i|
    current[i-1] += 1
  end
end

p score