#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>

using namespace std;

typedef long long int64;

#define fore(i,a,b) for (int i = (int)a; i < (int)b; i++)
#define forf(i,n) for (int i = 0; i < (int)n; i++)
#define forb(i,n) for (int i = (int)n-1; i >= 0; i--)

#define INF (int)1E9
#define INF64 (int64)1E18

int main()
{
	//freopen("input.txt","rt",stdin);
	//freopen("output.txt","wt",stdout);
	int A = 0,B = 0,p = 0,mx = 0,k,t;
	int z[10] = {0,1,2,3,2,1,2,3,4,2};
	char *s = new char [20];
	cin >> s;
	while (s[p] >= '0' && s[p] <= '9') A = 10*A+(int)s[p++]-(int)('0');
	if (s[p] == 'A') A += 753;
	else A = 754-A;
	p += 3;
	while (s[p] >= '0' && s[p] <= '9') B = 10*B+(int)s[p++]-(int)('0');
	if (s[p] == 'A') B += 753;
	else B = 754-B;
	for (int i = A; i <= B; i++)
	{
		k = 0;
		t = i;
		k += z[t/1000];
		t %= 1000;
		k += z[t/100];
		t %= 100;
		k += z[t/10];
		t %= 10;
		k += z[t];
		if (k > mx) mx = k;
	}
	cout << mx << endl;
	return 0;
}