#include #include using namespace std; char keyboard[3][100] = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"}; int posX[26], posY[26]; int abs(int x) { return x < 0 ? -x : x; } int manhattan(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); } int main() { //freopen("in.txt", "r", stdin); char s[101]; scanf("%s", s); for (int i = 0; i < 3; ++i) { int j = 0; char *p = keyboard[i]; while (*p) { posX[*p - 'A'] = i; posY[*p - 'A'] = j; ++p; ++j; } } int leftX = 1, leftY = 3, rightX = 1, rightY = 6, ans = 0; char* p = s; while (*p) { int left = manhattan(leftX, leftY, posX[*p - 'A'], posY[*p - 'A']); int right = manhattan(rightX, rightY, posX[*p - 'A'], posY[*p - 'A']); if (left < right) { ans += left; leftX = posX[*p - 'A']; leftY = posY[*p - 'A']; } else { ans += right; rightX = posX[*p - 'A']; rightY = posY[*p - 'A']; } ++p; } cout << ans; return 0; }