Funny Language

Time limit1sMemory limit128 MB

Summary
Choose n new words (avoiding the given m words) to maximize the total count of formable-subword matches across all given words, using their letter multisets.
Level

Hard8 of 10

Topics
Combinatorics, Greedy, Math
Solved
No attempts yet

Problem

There is a well-known word game. Given a word, you may form other words using only the letters of that word, where each letter may be used at most as many times as it appears in the original word; the order of the letters does not matter. For example, from the word CONTEST you can form NOTE, NET, ON, TEST, SET, and so on.

You are compiling a new dictionary and may add exactly nn brand-new words to it. You already know the mm words W1,W2,…,WmW_1, W_2, \ldots, W_m that you will later play this game with. Choose a set SS of exactly nn distinct non-empty words such that no chosen word equals any WiW_i, in order to maximize

∑i=1m∣Si∣,\sum_{i=1}^{m} |S_i|,

where Si⊆SS_i \subseteq S is the set of chosen words that can be formed from the letters of WiW_i.

Because many different sets SS may reach the maximum (and any word may be freely reordered), report only the optimal value of this sum — the largest total number of formable words — rather than the words themselves.

Input

The first line contains two integers nn and mm (1≤n≤1001 \le n \le 100, 1≤m≤10001 \le m \le 1000): the number of new words you may add and the number of game words. Each of the next mm lines contains one word WiW_i consisting of at most 100100 uppercase letters from A to Z.

Output

Print a single integer: the maximum possible value of ∑i=1m∣Si∣\displaystyle\sum_{i=1}^{m} |S_i|.

Examples7

  1. Example 1

    Input
    3 5
    A
    ACM
    ICPC
    CONTEST
    NEERC
    
    Expected output
    8
    
  2. Example 2

    Input
    1 1
    AAA
    
    Expected output
    1
    
  3. Example 3

    Input
    2 1
    A
    
    Expected output
    0
    
  4. Example 4

    Input
    5 1
    AB
    
    Expected output
    3
    
  5. Example 5

    Input
    1 5
    AB
    AB
    AB
    A
    B
    
    Expected output
    3
    
  6. Example 6

    Input
    4 3
    AAB
    ABB
    ABC
    
    Expected output
    12
    
  7. Example 7

    Input
    6 3
    AAB
    ABB
    ABC
    
    Expected output
    14