Under construction

Still being built. Things may change

KSA String 2

Time limit1sMemory limit1024 MB

Summary
Given a string of K, S, and A, find the minimum number of remove or move-to-front operations that turn it into the longest string repeating KSA.
Level

Medium7 of 10

Topics
String, Dynamic programming, Greedy
Solved
No attempts yet

Problem

The KSAAC staff all love KSA, so they like strings that satisfy the following condition.

Let the length of the string be NN. For every 1≤i≤N1 \leq i \leq N:

  • If the remainder of ii divided by 33 is 11, the ii-th character is K.
  • If the remainder of ii divided by 33 is 22, the ii-th character is S.
  • If the remainder of ii divided by 33 is 00, the ii-th character is A.

The empty string is also a string that the KSAAC staff like.

You may perform the following operations 00 or more times. Each time, you choose one of the two options below.

  • Remove any one existing character.
  • Move any one existing character to the front.

Perform appropriate operations on the given string XX to turn XX into a liked string of the maximum possible length. Find the minimum number of operations required.

Input

The first line contains the string XX.

Output

Print the minimum number of operations needed to turn the string XX into a liked string of the maximum possible length.

Constraints

  • 1≤∣X∣≤5×1051 \le |X| \le 5 \times 10^5
  • Xi∈{K,S,A}X_i \in \{\texttt{K}, \texttt{S}, \texttt{A}\}

Examples1

  1. Example 1

    Input
    KAKSK
    
    Expected output
    3