Hiking in the Hills

No attempts yetTime limit2sMemory limit256 MB

Problem

Helen is hiking with her friends in a highland. The plan is to walk from their camp A to a lookout B.

Helen has started to feel dizzy from altitude sickness. Help the group find a route whose highest point is as low as possible.

The landscape is given as a set of triangles. A route is a polyline from A to B in which every segment lies entirely inside one of those triangles, so the route never leaves the surface. The topmost height of a route is the largest zz coordinate of any point on it. Find the smallest topmost height over all routes from A to B.

Input

The landscape covers a square region of size 106×10610^6 \times 10^6.

The first line contains one integer nn, the number of triangles in the landscape (2n20002 \le n \le 2000).

Each of the next nn lines contains nine integers xi1x_{i1}, yi1y_{i1}, zi1z_{i1}, xi2x_{i2}, yi2y_{i2}, zi2z_{i2}, xi3x_{i3}, yi3y_{i3}, zi3z_{i3}, the coordinates of one triangle. Every coordinate belongs to the closed interval [0,106][0, 10^6].

The last two lines contain three integers each: xAx_A, yAy_A, zAz_A and xBx_B, yBy_B, zBz_B, the coordinates of camp A and lookout B.

The triangles describe one consistent continuous landscape. Their projections onto the XY plane are non-degenerate and fill the square without overlapping. A vertex of one triangle never lies inside an edge of another triangle. A and B lie on the landscape surface and are different points.

Output

Print one integer, the smallest topmost height that a route from A to B can have.