//#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <bitset>

using namespace std;

//ifstream cin("tema.in");
//ofstream cout("tema.out");

const int MAXN = 100;
const int SIGMA = 26;
const int INF = 1000000;

char s[1 + MAXN];
int a[4][11] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'},
                {0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0},
                {0, 'Z', 'X', 'C', 'V', 'B', 'N', 'M', 0, 0, 0}};
int line[SIGMA], column[SIGMA];

int dp[1 + MAXN][SIGMA][SIGMA];

int Abs(int x) {
    if (x < 0)
        return -x;
    return x;
}

int Cost(int a, int b) {
    return Abs(line[a] - line[b]) + Abs(column[a] - column[b]);
}

int main() {
    for (int i = 1; i <= 3; i++)
        for (int j = 1; j <= 10; j++)
            if (a[i][j]) {
                line[a[i][j] - 'A'] = i;
                column[a[i][j] - 'A'] = j;
            }
    cin >> s + 1;
    int n = strlen(s + 1);
    for (int k = 0; k <= n; k++)
        for (int i = 0; i < SIGMA; i++)
            for (int j = 0; j < SIGMA; j++)
                dp[k][i][j] = INF;
    dp[0]['F' - 'A']['J' - 'A'] = 0;
    for (int i = 1; i <= n; i++) {
        for (int a = 0; a < SIGMA; a++)
            for (int b = 0; b < SIGMA; b++)
                dp[i][a][s[i] - 'A'] = min(dp[i][a][s[i] - 'A'], dp[i - 1][a][b] + Cost(b, s[i] - 'A'));
        for (int a = 0; a < SIGMA; a++)
            for (int b = 0; b < SIGMA; b++)
                dp[i][s[i] - 'A'][a] = min(dp[i][s[i] - 'A'][a], dp[i - 1][b][a] + Cost(b, s[i] - 'A'));
    }
    int answer = dp[n][0][0];
    for (int a = 0; a < SIGMA; a++)
        for (int b = 0; b < SIGMA; b++)
            answer = min(answer, dp[n][a][b]);
    cout << answer << "\n";
    return 0;
}