This page is still under construction.

Parts of this page are still being built. What you see may change.

Adhoc Translation

Time limit8sMemory limit512 MB

Summary
Given text words and dictionary words, assign each distinct text word a unique dictionary word, minimizing the total edit distance summed over all occurrences.
Level

Hard8 of 10

Topics
Dynamic programming, String, Brute force, Implementation
Solved
No attempts yet

Problem

One day, while surfing the web, you found a web page written in a language you had never seen before. The character set of that language was the same as your native language, and the grammar and words looked almost the same. Excited, you started to "decipher" the web page. The first approach you tried was to guess the meaning of each word by picking a similar word from a dictionary of your native language. The closer two words are, even if they belong to different languages, the more similar their meanings will be.

You decided to use edit distance as the measure of similarity between two words. The edit distance between two strings is the minimum number of insertions, deletions, and substitutions needed to turn one string into the other. For example, the pair "point" and "spoon" has edit distance 3: deleting 't' from "point", substituting 'i' with 'o', and inserting 's' at the front gives "spoon".

You want to assign one word of your native language to each word in the web text so that the whole assignment has the minimum edit distance. The edit distance of an assignment is the sum of the edit distances between each word in the text and its assigned word in your native language. A word that appears more than once in the text is counted once for each appearance.

The translation must be consistent across the whole text. Even if a word of the text appears multiple times, you may not assign different dictionary words to different occurrences. Likewise, different words of the text must not have the same meaning in your native language.

Suppose the web page says "qwerty asdf zxcv" and the dictionary contains "qwert", "asf", "tyui", "zxcvb", "ghjk". Then you can match the words of the page as follows, and the edit distance of this translation is 3: "qwert" for "qwerty", "asf" for "asdf", and "zxcvb" for "zxcv".

Given the text of a web page and the set of words in a dictionary, write a program that finds the minimum edit distance over all possible translations.

Input

The first line contains two integers N and M.

The following N lines give the text of the web page you found. This text contains only lowercase alphabets and spaces. Then M lines, each containing one word, describe the dictionary to use. Every word consists of lowercase alphabets only and is no longer than 20 characters.

It is guaranteed that 1 ≤ N ≤ 100 and 1 ≤ M ≤ 400. It is also guaranteed that the dictionary has enough words: the number of words in the dictionary is no less than the number of distinct words in the text to translate. The length of each line in the text does not exceed 1000.

Output

Print the minimum possible edit distance on one line.

Examples1

  1. Example 1

    Input
    1 5
    qwerty asdf zxcv
    qwert
    asf
    tyui
    zxcvb
    ghjk
    
    Expected output
    3