#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;++i)
#define mp make_pair
#define pb push_back
#define x0 gtmsub
#define y0 gtmshb
#define x1 gtmjtjl
#define y1 gtmsf
struct data
{
	bool typ;
	long long pos;
	char c;
	friend bool operator <(const data&a,const data&b)
	{
		if(a.typ!=b.typ)return a.typ<b.typ;
		else if(a.typ)return a.pos<=b.pos;
		else return a.pos>=b.pos;
	}
	friend bool operator ==(const data&a,const data&b)
	{
		if(a.typ!=b.typ)return 0;
		if(a.typ)return a.pos==b.pos&&a.c==b.c;
		else return a.pos==b.pos;
	}
}a[2010],b[2010];
int atot,btot;
long long x;
char s[110];
bool ok[2010];
void init(data a[],int&tot)
{
	for(;;)
	{
		scanf("%s",s);
		if(s[0]=='E')break;
		if(s[0]=='D')
		{
			scanf("%lld",&x);
			a[++tot]={0,x,'a'};
		}
		else
		{
			scanf("%lld%s",&x,s);
			a[++tot]={1,x,s[0]};
		}
	}
}
void work(data a[],int&tot)
{
	int fuck=tot;
	for(int i=1;i<=fuck;i++)
	for(int j=tot-1;j>=1;j--)
	if(a[j+1]<a[j])
	{
		swap(a[j],a[j+1]);
		if(a[j].typ&&a[j+1].typ)a[j+1].pos++;
		else if(!a[j].typ&&!a[j+1].typ)a[j].pos++;
		else
		{
			if(a[j].pos>a[j+1].pos)a[j].pos--;
			else if(a[j].pos<a[j+1].pos)a[j+1].pos--;
			else
			{
				for(int p=j;p<tot-1;p++)a[p]=a[p+2];
				tot-=2;j=min(j,tot);
			}
		}
	}
	/*for(int i=1;i<=tot;i++)ok[i]=1;
	int t=-1;
	for(int i=1;i<=tot;i++)if(!a[i].typ)t=i;
	if(t==-1||t==tot)return;
	for(int i=t+1;i<=tot&&a[i].typ;i++)
	{
		for(;t&&!a[t].typ&&a[t].pos<a[i].pos;t--);
		if(t<1||a[t].typ)break;
		if(a[i].pos==a[t].pos)
		{
			ok[i]=ok[t]=0;
			for(int j=t-1;j&&!a[j].typ;j--)a[j].pos--;
			for(int j=i+1;j<=tot&&a[j].typ;j++)a[j].pos--;
			t--;
		}
			
	}
	int cnt=0;
	for(int i=1;i<=tot;i++)if(ok[i])a[++cnt]=a[i];
	tot=cnt;*/
	//for(int i=1;i<=tot;i++)printf("%d %lld %c\n",a[i].typ,a[i].pos,a[i].c);
	//puts("");
}
int check()
{
	if(atot!=btot)return 1;
	for(int i=1;i<=atot;i++)if(!(a[i]==b[i]))return 1;
	return 0;
}
int main()
{
	init(a,atot);
	init(b,btot);
	work(a,atot);
	work(b,btot);
	printf("%d\n",check());
	return 0;
}
