Custom keyboard - MarisaOJ: Marisa Online Judge

Custom keyboard

Time limit: 1000 ms
Memory limit: 256 MB
Rinnosuke wants to type a paragraph S that contains only the first m characters in the alphabet. Marisa wants to build a custom keyboard for him with m keys arranged in a row. The position of each character c in the keyboard is denoted by pc, and the tiredness of typing a paragraph s of n characters is given by the formula: ∑i=1n−1|psi+1−psi| Marisa wants to minimize the tiredness for Rinnosuke. Please help her determine the minimum tiredness! ### Input - The first line contains 2 integers n and m. - The second line contains a string S. ### Output - Print the minimum tiredness. ### Constraints - 1≤n≤105. - 1≤m≤20. ### Example Input: 63aacabc Output: 5 Note: She arranges the keyboard like this bac.