cho.sh
Notes
Loading...

Unknown Sentence

Time limit

2s

Memory limit

128 MB

Problem

A group of friends created a new language so that other people could not easily understand their conversations.

This language has N words. A sentence in the language is made by writing those words consecutively without spaces. Each word may appear zero or more times in the sentence.

When a word is used, the order of its letters may be rearranged. The cost of using the rearranged word is the number of letters whose positions differ from the original word. For instance, abc can become abc with cost 0; it can become acb, cba, or bac with cost 2; and it can become bca or cab with cost 3.

A sentence can therefore be interpreted in several ways. Compute the minimum total cost needed to interpret the given sentence.

Input

The first line contains the sentence to interpret. Its length is at most 50.

The second line contains the number of words N, where N is a positive integer not greater than 50.

Each of the next N lines contains one word. Each word has length at most 50.

The sentence and all words consist only of lowercase English letters.

Output

If the sentence can be interpreted, output the minimum required cost.

If it cannot be interpreted, output -1.