Painting Patterns

No attempts yetTime limit2sMemory limit64 MB

Problem

Lukas has a very large grid. Initially every cell is white. He has three patterns, numbered 1 to 3 (from left to right):

XXXX    X.X.    X.X.
....    X.X.    .X.X
XXXX    X.X.    X.X.
....    X.X.    .X.X

Each pattern is periodic and can be extended to fill a rectangle of any size. Lukas paints the grid NN times. Each time he chooses an axis-aligned rectangle and one pattern, then paints the cells inside the rectangle black according to that pattern (an X in the pattern paints the cell black, a . leaves it unchanged). The pattern is anchored so that its top-left corner coincides with the top-left corner of the rectangle: the grid point with the smallest xx coordinate and the largest yy coordinate.

When painted areas overlap, the OR rule applies: a cell is black if it was painted black by at least one operation. For example, applying pattern 1 and then pattern 3 to the same 4×44 \times 4 rectangle yields:

XXXX
.X.X
XXXX
.X.X

After all NN operations, compute the number of black cells.

Input

The first line contains an integer NN (0N1000000 \le N \le 100000). Each of the next NN lines contains five integers x1x_1, y1y_1, x2x_2, y2y_2, pp, where (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) are two opposite corners of the rectangle (given as grid points), and pp (1p31 \le p \le 3) is the pattern number. All coordinates have absolute value at most 10910^9, and every rectangle is at least one unit wide and one unit tall.

Output

Print a single integer: the number of black cells after all operations.