// rome.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cctype>

using namespace std;

int getn(int k)
{
	if (k == 1 || k == 5)
		return 1;
	else if (k == 2 || k == 4 || k == 6 || k == 9)
		return 2;
	else if (k == 3 || k == 7)
		return 3;
	else if (k == 8)
		return 4;

	return 0;
}

int number(int num)
{
	int result = 0, k;

	k = num / 1000;
	num %= 1000;
	result += k;

	k = num / 100;
	num %= 100;
	result += getn(k);

	k = num / 10;
	num %= 10;
	result += getn(k);

	result += getn(num);

	return result;
}

int main()
{
	char buffer;
	int from, to, cmax;

	from = 0, to = 0;

	buffer = cin.get();
	while (isdigit(buffer)) {
		from = from * 10 + (buffer - '0');
		buffer = cin.get();
	}

	if (buffer == 'B')
		from = -from + 754;
	else
		from += 753;

	cin.get(); cin.get();
	buffer = cin.get();
	while (isdigit(buffer)) {
		to = to * 10 + (buffer - '0');
		buffer = cin.get();
	}
	if (buffer == 'B')
		to = -to + 754;
	else
		to += 753;

	cmax = number(to);
	for (int i = from; i < to; ++i) {
		cmax = max(cmax, number(i));
	}

	cout << cmax; // << endl << from << ", " << to << endl;
//	cin.get(); cin.get(); cin.get();

	return 0;
}

