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.