#include<bits/stdc++.h>
using namespace std;
string s="QWERTYUIOPASDFGHJKLZXCVBNM";
int poz1x, poz1y, poz2x, poz2y, ans;
struct a
{
	int x,y;
} poz[300];
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++;
	}
	poz1x = poz['F'].x, poz1y=poz['F'].y;
	poz2x = poz['J'].x, poz2y=poz['J'].y;
	cin >> s;
	for(int i = 0 ; i < s.length(); i++)
	{
		ans += min(abs(poz1x-poz[s[i]].x) + abs(poz1y-poz[s[i]].y),abs(poz2x-poz[s[i]].x) + abs(poz2y-poz[s[i]].y));
		if (abs(poz1x-poz[s[i]].x) + abs(poz1y-poz[s[i]].y) < abs(poz2x-poz[s[i]].x) + abs(poz2y-poz[s[i]].y)) 
		{
			poz1x = poz[s[i]].x;
			poz1y = poz[s[i]].y;
		}else
			{
				poz2x = poz[s[i]].x;
				poz2y = poz[s[i]].y;
			}
	}
	cout << ans;
}