Dragon Curve

Generate the segments of N dragon curves on a 101 by 101 grid, mark the grid points they pass through, and count unit squares whose four corners are all marked.

Medium6ImplementationRecursionSimulationMathNo attempts yetTime limit1sMemory limit512 MB

Problem

A dragon curve is fixed by three values and it is defined on the two dimensional coordinate plane. On that plane the x axis runs in the → direction and the y axis runs in the ↓ direction.

  1. Starting point
  2. Starting direction
  3. Generation

A generation 0 dragon curve is a single segment of length 1. The figure below is the generation 0 dragon curve that starts at (0,0)(0, 0) with the starting direction to the right.

The generation 1 dragon curve is the generation 0 curve turned 90 degrees clockwise about its end point and then attached to the end point of the generation 0 curve. The end point is the point that lies farthest away when you travel from the starting point along the segments.

The generation 2 dragon curve is built from generation 1 by the same method. The blue segments are the ones just attached.

The generation 3 dragon curve is built from the generation 2 curve. The figure below is the generation 3 dragon curve.

In other words, the generation KK dragon curve (K>1K > 1) is the generation K1K-1 curve turned 90 degrees clockwise about its end point and attached to that end point.

A grid of size 100×100100 \times 100 holds NN dragon curves. Write a program that counts the squares of size 1×11 \times 1 whose four corners all belong to a dragon curve. A grid coordinate is written (x,y)(x, y), and only the coordinates with 0x1000 \le x \le 100 and 0y1000 \le y \le 100 are valid.

Input

The first line has the number of dragon curves NN (1N201 \le N \le 20). Each of the next NN lines describes one dragon curve with four integers xx, yy, dd, gg. Here xx and yy are the starting point, dd is the starting direction, and gg is the generation. (0x,y1000 \le x, y \le 100, 0d30 \le d \le 3, 0g100 \le g \le 10)

No dragon curve given in the input leaves the grid. Dragon curves can overlap each other.

The direction dd is one of 0, 1, 2, 3 and means the following.

  • 0: the direction in which the x coordinate increases (→)
  • 1: the direction in which the y coordinate decreases (↑)
  • 2: the direction in which the x coordinate decreases (←)
  • 3: the direction in which the y coordinate increases (↓)

Output

Print on the first line the number of squares of size 1×11 \times 1 whose four corners all belong to a dragon curve.

Hint

Example with 3 dragon curvesExample with 4 dragon curves