Long Long Strings

Decide whether two sequences of insertions and deletions, applied to any sufficiently long string, produce identical results.

Hard8StringMathImplementationSimulationNo attempts yetTime limit1sMemory limit512 MB

Problem

To store long DNA sequences, a company built a LongLongString class that holds strings of more than ten billion characters. The class supports two operations.

  • Ins(p, c): insert the character cc at position pp.
  • Del(p): delete the character at position pp.

A DNA editing program is a sequence of Ins and Del operations. Write a program that decides whether two DNA editing programs are identical, meaning that applying them to any sufficiently long string always gives the same result. For example:

  • Del(1) Del(2) and Del(3) Del(1) are identical.
  • Del(2) Del(1) and Del(1) Del(2) are different.
  • A program with no operations and Ins(1, X) Del(1) are identical.
  • Ins(14, B) Ins(14, A) and Ins(14, A) Ins(15, B) are identical.
  • Ins(14, A) Ins(15, B) and Ins(14, B) Ins(15, A) are different.

Input

The input holds two DNA editing programs. Each program has between 0 and 2,000 operations, one operation per line. The first character of a line is D for a Del operation, I for an Ins operation, or E for the end of that program.

A D line holds D, a space, and the position of the character to delete. The position is an integer between 1 and 101010^{10}. Every character after the deleted one moves one position lower.

An I line holds I, a space, the position where the new character goes, another space, and the character itself, which is an uppercase letter. The character already at that position and everything after it move one position higher.

Output

Print 0 on a single line if the two programs are identical, and 1 otherwise.