Rendezvous on a Tetrahedron

Two worms start at vertex A of a regular tetrahedron, crawl straight across faces reflecting off edges, and stop after integer trail lengths; decide whether they end on the same face.

Hard8GeometryImplementationMathNo attempts yetTime limit1sMemory limit512 MB

Problem

Two worms, P and Q, crawl on the surface of a regular tetrahedron with vertices A, B, C and D. Both worms start at the vertex A, move straight ahead, and stop after a while.

When a worm reaches an edge of the tetrahedron, it moves onto the adjacent face and keeps going, holding the same angle with the edge it just crossed (Figure 1).

Write a program that decides whether P and Q were on the same face of the tetrahedron when they stopped.

Each worm is a point with no length, area or volume.

Figure 1. Crossing an edge

The length of each trail is an integer multiple of the unit length, where the unit length is the edge length of the tetrahedron. Apart from its start point and the neighborhood of that point, every trail stays more than 0.001 unit away from every vertex, so each worm crossed at least one edge. Both worms stopped more than 0.001 unit away from every edge.

The initial direction of a worm is given by two values. One is the edge XY, the first edge the worm met after its start. The other is the angle dd in degrees between the edge AX and the direction of the worm.

Figure 2. Trails of the two worms in the first example, where P crosses the edge CD with d=30d = 30 and l=1l = 1, and Q crosses the edge DB with d=30d = 30 and l=1l = 1.

In that example P goes over the edge CD and stops on the face opposite the vertex A, and Q goes over the edge DB and stops on the same face.

Input

The input is a single test case in the following format.

XPYP dP lP
XQYQ dQ lQ

The first line describes worm P and the second line describes worm Q. Each line holds the edge name, the angle and the length, separated by spaces. The edge name is written as two letters with no space between them.

XWYWX_W Y_W (W is P or Q) is the first edge that worm W crossed after its start. It is one of BC, CD, DB.

The integer dWd_W (1dW591 \le d_W \le 59) is the angle in degrees between the edge AXWA X_W and the initial direction of worm W on the face AXWYW\triangle A X_W Y_W.

The integer lWl_W (1lW201 \le l_W \le 20) is the length of the trail that worm W left on the surface, measured in unit lengths.

Output

Print YES if the two worms stopped on the same face of the tetrahedron. Otherwise print NO.