cursordistance
Cursor Distance
There is a string of lowercase English letters. A cursor is positioned on one of the characters.
The cursor can be moved with the following operation: choose a letter and a direction (left or right).
The cursor is then moved to the closest occurence of in the chosen direction. If there is no letter in that direction, the cursor stays in place.
For example, if abaab with the cursor on the second character (a[b]aab), then:
- moving to the closest letter a to the left places the cursor on the first character ([a]baab);
- moving to the closest letter a to the right places the cursor the third character (ab[a]ab);
- moving to the closest letter b to the right places the cursor on the fifth character (abaa[b]);
- any other operation leaves the cursor in place.
Let be the smallest number of operations needed to move the cursor from the character to the character. Compute .
Input format
The only line contains a non-empty string of lowercase English letters.
Output format
Print a single integer .
Limits
Subtask #
|
Score
|
Constraints
|
1 |
6 |
|
2 |
12 |
|
3 |
14 |
contains only letters a and b. |
4 |
18 |
contains only letters a to e. |
5 |
50 |
No additional constraints |
Samples
Sample Input 1
|
Sample Output 1
|
abcde
|
20
|
for any pair , and for all other pairs.
Sample Input 2
|
Sample Output 2
|
abacaba
|
58
|