def league(n, graph)
  ans = 0
  is_play_game = true
  while is_play_game
    is_play_game = false
    used = {}
    n.times do |i|
      if !graph[i].empty? && !used[i]
        opponent = graph[i][0]
        if !used[opponent] && graph[opponent][0] == i
          graph[i].shift
          graph[opponent].shift
          used[i] = true
          used[opponent] = true
          is_play_game = true
        end
      end
    end
    ans += 1 if is_play_game
  end

  ans = -1 if graph.any? { |e| !e.empty? }
  puts ans
end

n = gets.to_i
graph = []
graph << []
n.times{ graph << gets.split.map(&:to_i) }

league(n, graph)