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 $p_c$, and the tiredness of typing a paragraph $s$ of $n$ characters is given by the formula: $$\sum_{i=1}^{n - 1} |p_{s_{i + 1}} - p_{s_i}|$$ 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 \le n \le 10^5$. - $1 \le m \le 20$. ### Example Input: ``` 6 3 aacabc ``` Output: ``` 5 ``` Note: She arranges the keyboard like this `bac`.