#include using namespace std; struct abc{int x,y;} carac[40], pos1, pos2; string qwe = "QWERTYUIOPASDFGHJKLZXCVBNM"; int main() { string str; //freopen("txt.in", "r", stdin); int lin = 1, col = 1; for(const auto& it : qwe) { carac[it - 'A'] = {lin, col}; ++col; if(it == 'P' || it == 'L') { lin++; col = 1; } } cin >> str; int sum = 0; pos1 = carac['F' - 'A']; pos2 = carac['J' - 'A']; for(const auto& it : str) { int dist1 = abs(pos1.x - carac[it - 'A'].x) + abs(pos1.y - carac[it - 'A'].y); int dist2 = abs(pos2.x - carac[it - 'A'].x) + abs(pos2.y - carac[it - 'A'].y); if(dist1 > dist2) { pos2 = carac[it - 'A']; sum += dist2; } else { pos1 = carac[it - 'A']; sum += dist1; } } cout << sum << "\n"; return 0; }