Under construction

Still being built. Things may change

KSA String

Interview

Time limit1sMemory limit1024 MB

Summary
Given a string X, find the fewest deletions and front or end insertions that turn it into the KSA-periodic string of the same length.
Level

Medium4 of 10

Topics
String, 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 ii with 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.

You can perform the following operations on a string zero or more times.

  • Remove one existing character.
  • Add one arbitrary character at the front.
  • Add one arbitrary character at the end.

Perform appropriate operations on the given string XX to turn it into a string of the same length as XX that the KSAAC staff like. Find the minimum number of operations needed.

Input

The first line contains the string XX.

Output

Print the minimum number of operations needed to turn XX into a string of the same length as XX that the KSAAC staff like.

Constraints

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

Examples1

  1. Example 1

    Input
    KKSKASKA
    
    Expected output
    8