Text on the fence#4528

Text on the fence

1 second
64 megabytes
Medium

The mayor of Rechuysk has implemented fines for using certain undesirable words and has published a list of these words along with the fine amount for each. All these words consist only of the letters , , and .

Someone is constructing an open fence consisting of boards (N \leq 100). Each board displays one of the letters , , or . The sequence of letters on the fence forms a pattern. For every occurrence of an undesirable word formed by consecutive letters (read from left to right), a fine must be paid for each time it appears in the fence.

For example, if the forbidden words are with a fine of hryvnia and with a fine of hryvnias, then constructing the fence would incur a total fine of hryvnias.

The task is to write a program that determines the sequence of boards for the fence that results in the minimal possible fine.

Constraints

Input

The first line contains the length of the fence .

The second line contains the number of words in the mayor's list .

Each of the following lines contains an undesirable word, followed by its corresponding fine, separated by a space. All words are distinct and consist solely of the uppercase Latin letters , , or .

The input data is guaranteed to be correct.

Output

The first line of the output should contain the value of the minimal fine.

Examples

Example 1

Loading
Loading