require 'set'

N = gets.to_i
A = Array.new(N) { gets.split.map(&:to_i) }
Game = Struct.new(:id, :before_game_ids, :after_game_ids) do
  def ready?
    before_game_ids.empty?
  end
end

games = Hash.new {|h, k| h[k] = Game.new(k, Set.new, Set.new)}
A.each.with_index(1) do |a, i|
  a.each_cons(2) do |j, after_j|
    id = [i, j].sort.join(?-)
    after_game_id = [i, after_j].sort.join(?-)
    games[id].after_game_ids << after_game_id
    games[after_game_id].before_game_ids << id
  end
end

day = 1
loop do
  today_games = games.values.select(&:ready?)
  break(day = -1) if today_games.empty?

  today_games.each do |game|
    games.delete(game.id)
    game.after_game_ids.each do |after_game_id|
      games[after_game_id].before_game_ids.delete(game.id)
    end
  end

  break if games.empty?

  day = day.next
end

puts day