#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
int mabs(int a)
{
	if (a < 0)
		return -a;
	else return a;
}
int get_num(int a, bool f)
{
	int cur = 0;
	if (f)
	{
		a += 753;
	}
	else a = abs(a - 752);
		while (a != 0)
		{
			if (a / 1000 > 0)
			{
		  	    cur += a / 1000;   	
		    	a %= 1000;
			}
			if (a / 900 > 0)
			{
				cur += 2;
				a -= 900;
			}
			if (a / 500 > 0)
			{
		  	    cur += a / 500;   	
		    	a %= 500;
			}
			if (a / 400 > 0 )
			{
				cur += 2;
				a -= 400;
			}
			if (a / 100 > 0)
			{
		  	    cur += a / 100;   	
		    	a %= 100;
			}
			if (a / 90 > 0)
			{
				cur += 2;
				a -= 90;
			}
			if (a / 50 > 0)
			{
		  	    cur += a / 50;   	
		    	a %= 50;
			}
			if (a / 40 > 0)
			{
				
				cur += 2;
				a -= 40;
			}
			if (a / 10 > 0)
			{
		  	    cur += a / 10;   	
		    	a %= 10;
			}
			if (a / 9 > 0)
			{
				cur += 2;
				a -= 9;
			}
			if (a / 5 > 0)
			{
		  	    cur += a / 5;   	
		    	a %= 5;
			}
			if (a / 4 > 0)
			{
				cur += 2;
				a -= 4;
			}
			cur += a;
			a = 0;
		}
	return cur;
}

int main()
{
  
// freopen("in.txt", "r", stdin);
 // freopen("out.txt", "w", stdout);
  int a, b;
  char d, t;
  cin >> a >> d >> t >> d;
  bool fa, fb;
  if (t == 'D')
	  fa = true;
  else fa = false;
  cin >> b >> d >> t >> d;
  if (t == 'D')
	  fb = true;
  else fb = false;
int mmax = 0;
  //if (a == 1 && !fa) mmax = 6;
  //if (b == 1 && !fb) mmax = 6;
  
  if (fa)
  {
	  for(int i = a; i <= b; i++)
		  mmax = max(mmax, get_num(i, fa));
	  cout << mmax;
  }
  if (fa == 0 &&  fb == 0)
  {
	  for(int i = a; i >= b; i--)
		  mmax = max(mmax, get_num(i, fa));
	  cout << mmax;
  }
  if (fa ==0 && fb == 1)
  {
	  mmax = max(mmax, get_num(0, fa));
	  mmax = max(mmax, get_num(0, fb));
	for(int i = a; i <= 0; i--)
		 mmax = max(mmax, get_num(i, fa));
	for(int i = 0; i <= b; i++)
		  mmax = max(mmax, get_num(i, fb));
	cout << mmax;
  }

} 