#include using namespace std; string s1 = "QWERTYUIOP", s2 = "ASDFGHJKL", s3 = "ZXCVBNM"; vectors = {s1, s2, s3}; int x[666], y[666]; void go(int l) { int cont = 0; for (auto &i : s[l]) { x[i] = l; y[i] = cont; cont++; } } int mh(int XX, int YY, int X, int Y) { return abs(X - XX) + abs(YY - Y); } int main() { #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif // ONLINE_JUDGE ios::sync_with_stdio(0); go(0), go(1), go(2); string s; cin >> s; int xf = x['F'], yf = y['F']; int xj = x['J'], yj = y['J']; int ans = 0; for (auto i : s) { int curx = x[i], cury = y[i]; if (mh(xf, yf, curx, cury) < mh(xj, yj, curx, cury)) { ans += mh(xf, yf, curx, cury); xf = curx, yf = cury; } else { ans += mh(xj, yj, curx, cury); xj = curx, yj = cury; } } cout << ans; }