Condorcet

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Consider an election where there are some candidates, one winner, and each voter has a complete ranked preference of the candidates. One possible method of determining the winner is to examine each pair of candidates and see which would win in a head-to-head matchup (i.e., which candidate has more voters that rank them higher than their opponent) and then see if there is a single candidate that wins all their head-to-head matchups. This is called the Condorcet Method.

For simplicity, let ABC denote a vote that prefers A over B, and B over C. As an example, imagine that there are three votes: ABC, BAC, and CAB. Then A wins in a head-to-head matchup against B 2:1, and A also wins against C 2:1, so we could declare A the winner overall.

Note that a winner does not always exist; for example, imagine that the three votes were ABC, BCA, and CAB instead. Then A wins against B 2:1, B wins against C 2:1, and C wins against A 2:1. There is no single candidate that wins all their head-to-head matchups.

You are given the candidates and a set of votes. What is the minimum number of additional voters you would need to add (whose preferences you can individually control) in order to ensure that no overall winner exists? Assume that ties are broken by some tiebreaker you do not control and cannot predict. Therefore, you need to have every candidate lose a head-to-head matchup with some other candidate.

입력

The first line of input contains two integers, nn (3n53 \le n \le 5) and mm (1mn!1 \le m \le n!), where nn is the number of candidates, and mm is the number of vote tally lines. The candidates are represented by the first nn upper-case letters of the alphabet.

Each of the next mm lines contains a string ss and an integer kk (1k1061 \le k \le 10^6). These are the vote tallies, which each consist of a string ss defining the vote, and an integer count kk indicating how many votes of type ss are represented by that tally line. The string ss describing a vote contains the first nn upper-case letters, each exactly once, in some order. The votes in the vote tally lines are unique.

출력

Output a single integer, which is the minimum number of additional voters needed to ensure that no overall winner exists.