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 = -1
today_played = {}
yesterday_played = {}
played_count = Array.new(n, 0)

for i in 0...n do
  if !today_played.include?(i)
    j = a[i][played_count[i]] - 1
    if !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
  games += today_games
  day = 1
  while games < total_games do
    yesterday_played = today_played
    today_played = {}
    today_games = 0
    yesterday_played.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
    day += 1
  end
end
puts day
