#include <bits/stdc++.h>
#define NMAX 1005
using namespace std;

inline void debugMode() {
    #ifndef ONLINE_JUDGE
    freopen("debug.in", "r", stdin);
    #endif // ONLINE_JUDGE
}


struct Qwerty{

    int x,y;

}V[30];

int mod(int x){
    return max(x,-x);
}

int Distance(int a,int b){

    return mod(V[a].x - V[b].x) + mod(V[a].y - V[b].y);
}

int C(char x){

    return int (x - 'A');

}

int main()
{
    ios :: sync_with_stdio(false);
    debugMode();

    V[C('Q')] = {1,1};
    V[C('W')] = {1,2};
    V[C('E')] = {1,3};
    V[C('R')] = {1,4};
    V[C('T')] = {1,5};
    V[C('Y')] = {1,6};
    V[C('U')] = {1,7};
    V[C('I')] = {1,8};
    V[C('O')] = {1,9};
    V[C('P')] = {1,10};
    V[C('A')] = {2,1};
    V[C('S')] = {2,2};
    V[C('D')] = {2,3};
    V[C('F')] = {2,4};
    V[C('G')] = {2,5};
    V[C('H')] = {2,6};
    V[C('J')] = {2,7};
    V[C('K')] = {2,8};
    V[C('L')] = {2,9};
    V[C('Z')] = {3,1};
    V[C('X')] = {3,2};
    V[C('C')] = {3,3};
    V[C('V')] = {3,4};
    V[C('B')] = {3,5};
    V[C('N')] = {3,6};
    V[C('M')] = {3,7};

    int R = C('J'),L = C('F');

    string S;

    cin >> S;
    int sol = 0;
    for(auto const &it : S){
        if(Distance(C(it),R) < Distance(C(it),L)){

            sol += Distance(C(it),R);
            R = C(it);

        } else {

            sol += Distance(C(it),L);
            L = C(it);


        }
    }

    cout << sol;

    return 0;
}