#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string input;

typedef pair<int, int> point;

int dist(point a, point b) {
    return abs(a.first - b.first) + abs(a.second - b.second);
}

const char *qwerty[] = {
    "QWERTYUIOP",
    "ASDFGHJKL",
    "ZXCVBNM"
};

point keys[256];

point f1, f2;

int main() {
    cin >> input;

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < strlen(qwerty[i]); j++)
            keys[qwerty[i][j]] = make_pair(i, j);

    f1 = keys['F'];
    f2 = keys['J'];

    int sum = 0;
    for (int i = 0; i < input.size(); i++) {
        char next = input[i];
        point to = keys[next];
        int d1 = dist(f1, to);
        int d2 = dist(f2, to);
        sum += min(d1, d2);
        if (d1 <= d2) {
            f1 = to;
        } else {
            f2 = to;
        }
    }

    cout << sum << endl;
}