n = gets.to_i
a = n.times.map{gets.split.map(&:to_i)}

total_games = n * (n - 1) / 2
games = 0
today_games = 0
day = 0
possible_player = {}
today_played = {}
played_count = Array.new(n, 0)

for i in 0...n do
  possible_player[i] = 1
end

while games < total_games do
  today_games = 0
  possible_player.each_key do |i|
    if played_count[i] < (n - 1) && !today_played.include?(i)
      j = a[i][played_count[i]] - 1
      if played_count[j] < (n - 1) && !today_played.include?(j) && (a[j][played_count[j]] - 1) == i
        today_played[i] = 1
        today_played[j] = 1
        played_count[i] += 1
        played_count[j] += 1
        today_games += 1
      end
    end
  end
  if today_games == 0
    day = -1
    break
  else
    games += today_games
  end
  possible_player = today_played
  today_played = {}
  day += 1
end
puts day
