import java.io.PrintWriter;
import java.util.Scanner;

public class nH {

	Scanner in;
	PrintWriter out;

	String getRoman(int x) {
		String res = "";
		int d4 = x / 1000;
		x %= 1000;
		int d3 = x / 100;
		x %= 100;
		int d2 = x / 10;
		int d1 = x % 10;
		if (d4 == 1) {
			res += "M";
		} else if (d4 == 2) {
			res += "MM";
		} else if (d4 == 3) {
			res += "MMM";
		}
		//
		if (d3 == 1) {
			res += "C";
		} else if (d3 == 2) {
			res += "CC";
		} else if (d3 == 3) {
			res += "CCC";
		} else if (d3 == 4) {
			res += "CD";
		} else if (d3 == 5) {
			res += "D";
		} else if (d3 == 6) {
			res += "DC";
		} else if (d3 == 7) {
			res += "DCC";
		} else if (d3 == 8) {
			res += "DCCC";
		} else if (d3 == 9) {
			res += "CM";
		}
		//
		if (d2 == 1) {
			res += "X";
		} else if (d2 == 2) {
			res += "XX";
		} else if (d2 == 3) {
			res += "XXX";
		} else if (d2 == 4) {
			res += "XL";
		} else if (d2 == 5) {
			res += "L";
		} else if (d2 == 6) {
			res += "LX";
		} else if (d2 == 7) {
			res += "LXX";
		} else if (d2 == 8) {
			res += "LXXX";
		} else if (d2 == 9) {
			res += "XC";
		}
		//
		if (d1 == 1) {
			res += "I";
		} else if (d1 == 2) {
			res += "II";
		} else if (d1 == 3) {
			res += "III";
		} else if (d1 == 4) {
			res += "IV";
		} else if (d1 == 5) {
			res += "V";
		} else if (d1 == 6) {
			res += "VI";
		} else if (d1 == 7) {
			res += "VII";
		} else if (d1 == 8) {
			res += "VIII";
		} else if (d1 == 9) {
			res += "IX";
		}
		return res;
	}

	void solve() {
		String s = in.nextLine();
		String s1 = "";
		int pos = 0;
		while (s.charAt(pos) != '-') {
			s1 += s.charAt(pos);
			pos++;
		}
		pos++;
		String s2 = s.substring(pos);
		String era1 = "" + s1.charAt(s1.length() - 2)
				+ s1.charAt(s1.length() - 1);
		String era2 = "" + s2.charAt(s2.length() - 2)
				+ s2.charAt(s2.length() - 1);
		s1 = s1.substring(0, s1.length() - 2);
		s2 = s2.substring(0, s2.length() - 2);
		int left = 0;
		if (era1.equals("BC")) {
			left = 753 - Integer.parseInt(s1);
		} else if (era1.equals("AD")) {
			left = 753 + Integer.parseInt(s1);
		}
		int right = 0;
		if (era2.equals("BC")) {
			right = 753 - Integer.parseInt(s2);
		} else if (era2.equals("AD")) {
			right = 753 + Integer.parseInt(s2);
		}
		int max = 1;
		for (int i = left; i <= right; i++) {
			String rom = getRoman(i);
			max = Math.max(max, rom.length());
		}
		out.println(max);
	}

	void run() {
		in = new Scanner(System.in);
		out = new PrintWriter(System.out);
		try {
			solve();
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) {
		new nH().run();
	}
}
