// Arup Guha
// 11/14/2017
// Solution to 2017 SER D1 and D2 Problem: Long Long Strings

import java.util.*;

public class longlong {

	// We do all of our work elsewhere =)
	public static void main(String[] args) {
		Scanner stdin = new Scanner(System.in);

		// Read in.
		longstr left = process(stdin);
		longstr right = process(stdin);

		// Make as short as possible for easy comparison.
		left.fuse();
		right.fuse();

		// Ta da!
		System.out.println(left.equals(right));
	}

	public static longstr process(Scanner stdin) {

		// Set up a long string.
		longstr res = new longstr();
		char cmd = stdin.next().charAt(0);

		// Process each command.
		while (cmd != 'E') {

			// Delete.
			if (cmd == 'D') {
				long idx = stdin.nextLong();
				res.delete(idx);
			}

			// Insert.
			else {
				long idx = stdin.nextLong();
				char ch = stdin.next().charAt(0);
				res.insert(idx, ch);
			}

			// Get next...
			cmd = stdin.next().charAt(0);
		}

		return res;
	}
}

class range {

	public long start;
	public long end;
	public long len;
	public char c;

	public range(long s, long e) {
		start = s;
		end = e;
		len = e-s+1;
		c = ' ';
	}

	public void updateStart(long news) {
		start = news;
		len = end-start+1;
	}

	public void updateEnd(long newe) {
		end = newe;
		len = end-start+1;
	}

	public range(char myc) {
		start = -1;
		end = -1;
		len = 1;
		c = myc;
	}

	public boolean equals(range other) {
		if (start == -1) return this.c == other.c;
		return start == other.start && end == other.end;
	}
}

class longstr {

	public ArrayList<range> str;

	public longstr() {
		str = new ArrayList<range>();
		str.add(new range(1, 100000000000L));
	}

	public void delete(long idx) {

		long terms = 0;
		int i = 0;

		// Go to the right block.
		while (i < str.size() && terms + str.get(i).len < idx) {
			terms += str.get(i).len;
			i++;
		}

		// Special case, remove the whole block.
		if (str.get(i).len == 1) {
			str.remove(i);
			return;
		}

		// Indexes into this block.
		long firstIdx = terms + 1;
		long lastIdx = terms + str.get(i).len;

		// Clip off the last char in this block.
		if (lastIdx == idx) str.get(i).updateEnd(str.get(i).end-1);

		// Clip off the first char in this block.
		else if (firstIdx == idx) str.get(i).updateStart(str.get(i).start+1);

		// Split into two ranges.
		else {
			long leftTerms = idx - firstIdx;
			long saveEnd = str.get(i).end;
			str.get(i).updateEnd(str.get(i).start + leftTerms - 1);
			range right = new range(str.get(i).end+2, saveEnd);
			if (right.len == 0) System.out.println("ERROR");
			str.add(i+1, right);
		}
	}

	public void insert(long idx, char c) {

		long terms = 0;
		int i = 0;

		// Go to the right block.
		while (terms + str.get(i).len < idx) {
			terms += str.get(i).len;
			i++;
		}

		// Indexes into this block.
		long firstIdx = terms + 1;
		long lastIdx = terms + str.get(i).len;

		// Just added in the front of this whole range.
		if (idx == firstIdx) {
			str.add(i, new range(c));
			return;
		}

		// Splits this range into 2 separate ones.
		long leftTerms = idx - firstIdx;
		long saveEnd = str.get(i).end;
		str.get(i).updateEnd(str.get(i).start + leftTerms - 1);

		// Add both a right range and the character in between.
		range right = new range(str.get(i).end+1, saveEnd);
		str.add(i+1, right);
		str.add(i+1, new range(c));
	}

	public void fuse() {

		int i = 0;
		while (i < str.size()-1) {

			// These can fuse.
			if (str.get(i).end + 1 == str.get(i+1).start) {
				str.get(i).end = str.get(i+1).end;
				str.get(i).len = str.get(i).end - str.get(i).start+1;
				str.remove(i+1);
			}

			// We can safely move on.
			else
				i++;
		}
	}

	// Returns 0 if they are equal, 1 if they are different according to spec.
	// Must call fuse beforehand.
	public int equals(longstr other) {

		// Can't be the same.
		if (str.size() != other.str.size()) return 1;

		// Compare each item one by one.
		for (int i=0; i<str.size(); i++)
			if (!str.get(i).equals(other.str.get(i)))
				return 1;

		return 0;
	}
}