Asphalt Paving

Given segments on a triangular grid, choose the largest subset so that no two share an endpoint at an acute angle.

Medium6GraphDynamic programmingGeometryBit manipulationNo attempts yetTime limit1sMemory limit512 MB

Problem

In one tilted city the paths and the streets do not meet at right angles. They always meet at an angle that is a multiple of 60 degrees. More precisely, the city sits on a triangular grid, a coordinate system in which the xx axis and the yy axis enclose an angle of 60 degrees. Two points AA and BB with integer coordinates are neighbors when their Euclidean distance equals 1. Every point has exactly 6 neighbors. The neighbors of the point (x,y)(x, y) are (x+1,y)(x+1, y), (x1,y)(x-1, y), (x,y+1)(x, y+1), (x,y1)(x, y-1), (x+1,y1)(x+1, y-1), and (x1,y+1)(x-1, y+1).

The city has nn paths. Each path connects a point to one of its neighbors. The city has secured the money to cover paths with asphalt and turn them into modern roads. Sharp turns on those new roads are not allowed. Precisely, two paved paths must not touch at a point and enclose an acute angle there.

Determine the largest number of paths that can be paved so that no sharp turn appears.

Input

The first line contains the number of paths nn (1n10001 \le n \le 1000).

Each of the next nn lines contains four positive integers xAx_A, yAy_A, xBx_B, yBy_B (1xA,yA,xB,yB1001 \le x_A, y_A, x_B, y_B \le 100), in that order the coordinates of the points AA and BB that the path connects. AA and BB are always neighbors, and no path appears more than once in the input.

Output

Print the largest number of paths that can be paved.

Figure

The figure shows the two coordinate axes meeting at 60 degrees and the triangular grid. The orange segments are the 17 paths of the first example, and the 10 segments outlined in gray are the paved ones.