#include <iostream>
#include <cstdio>

#include <algorithm>
#include <cmath>
#include <cstring>

#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>

using namespace std;

#define pb(x) push_back(x)
#define mp(a, b) make_pair (a, b)
#define sqr(x) ((x)*(x))

const string s_ans[] = {
	"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", 
	"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
	"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM",
	"M", "MM", "MMM" };

const int MAXN = 1000;
char s[MAXN+1];

int getans (int y) {
	string s = "";

	int x = 0, pow = 1;
	while (pow <= y) x++, pow *= 10;
	x--, pow /= 10;

	while (x >= 0) {
		s += s_ans[x * 9 + (y / pow) % 10 - 1];
		pow /= 10;
		x--;
	}

	return s.size ();
}

int main () {
	//freopen ("input.txt", "r", stdin);	
	//freopen ("output.txt", "w", stdout);

	gets (s);

	int f, l, i; f = l = 0;

	for (i = 0; s[i] >= '0' && s[i] <= '9'; i++) f = f * 10 + s[i] - '0';	
	if (s[i] == 'B') f = -f; else f--;

	for (i += 3; s[i] >= '0' && s[i] <= '9'; i++) l = l * 10 + s[i] - '0';
	if (s[i] == 'B') l = -l; else l--;

	f += 754; l += 754;

	int ans = 0;
	for (int i = f; i <= l; i++) {
		int x = getans (i);
		if (x > ans) ans = x;
	}

	printf ("%d", ans);

	return 0;
}