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 memo[index]
    
    aite = days[index-1][i]
    next if memo[aite] || aite.nil?
    if days[aite-1][current[aite-1]] == index
      memo[index] = 1
      memo[aite-1] = 1
      current[index-1] += 1
      current[aite-1] += 1
    end
  end
  
  if memo.empty?
    score = -1
    break
  end
end

p score