#include using namespace std; string s="QWERTYUIOPASDFGHJKLZXCVBNM"; int ans; struct a { int x,y; } poz[300],poz1,poz2; int dist(a x, a y) { return (abs(x.x - y.x) + abs(x.y - y.y)); } int rec(int cur, int ans, a f, a j) { if(cur >= s.length())return ans; a next = poz[s[cur]]; if(dist(f,next) == dist(j,next)) return min(rec(cur+1,ans+dist(j,next),next,j), rec(cur+1,ans+dist(f,next),f,next)); else if(dist(f,next) < dist(j,next)) ans += dist(f,next),f = next; else ans+=dist(j,next),j = next; return rec(cur+1,ans,f,j); } int main() { int o = 0; for (int i = 1; i<=10; i++) { poz[s[o]].x = 1; poz[s[o]].y = i; o++; } for (int i = 1; i<=9; i++) { poz[s[o]].x = 2; poz[s[o]].y = i; o++; } for (int i = 1; i<=8; i++) { poz[s[o]].x = 3; poz[s[o]].y = i; o++; } poz1.x = poz['F'].x, poz1.y=poz['F'].y; poz2.x = poz['J'].x, poz2.y=poz['J'].y; cin >> s; cout << rec(0,0,poz1,poz2); }