Skiing

Find the shortest polygonal path from S down to F that crosses n horizontal gates in top-to-bottom order, and output its breakpoints.

Hard9GeometryGreedyDivide and conquerImplementationNo attempts yetTime limit1sMemory limit1024 MB

Problem

Kleofáš failed to qualify for the IOI, so he decided to become a slalom skier instead. Tomorrow is the day of his first skiing contest.

In the contest Kleofáš has to get from a start point to a finish point while passing through nn gates. To be as fast as possible, he wants the shortest possible trajectory.

A skiing course consists of a start point SS, a finish point FF and nn gates. Every gate is a line segment parallel to the xx axis, so every gate is horizontal. No two gates share the same yy coordinate (altitude). The start point is above every gate, so its yy coordinate is larger than the yy coordinate of any gate. The finish point is below every gate and below the start point.

Find the shortest polygonal chain that starts at SS, finishes at FF and intersects all gates in order from top to bottom. A polygonal chain intersects a line segment when the two have at least one common point, and that point may be an endpoint of the segment.

Input

The first line contains one integer nn (0n1060 \le n \le 10^6), the number of gates. The second line contains four integers xS,yS,xF,yFx_S, y_S, x_F, y_F, the coordinates of S=(xS,yS)S = (x_S, y_S) and F=(xF,yF)F = (x_F, y_F).

Each of the next nn lines contains three integers x1i,x2i,yix_{1i}, x_{2i}, y_i, meaning that the ii-th gate is the segment from (x1i,yi)(x_{1i}, y_i) to (x2i,yi)(x_{2i}, y_i). For every ii, x1i<x2ix_{1i} < x_{2i} holds.

All coordinates are between 109-10^9 and 10910^9, inclusive. The gates are given from top to bottom, so yS>y1>y2>>yn>yFy_S > y_1 > y_2 > \dots > y_n > y_F.

Output

The shortest polygonal chain always exists, it is unique, and all of its vertices have integer coordinates. Print this chain without any redundant vertices, that is, print only the vertices where the chain changes its direction.

On the first line print one integer kk, the number of vertices of the optimal chain. Then print kk more lines, the ii-th of them containing two space separated integers xi,yix_i, y_i, the coordinates of the ii-th vertex of the chain. Print the vertices from the start of the chain to its end, so x1=xSx_1 = x_S, y1=ySy_1 = y_S, xk=xFx_k = x_F, yk=yFy_k = y_F and y1>y2>>yky_1 > y_2 > \dots > y_k must hold.

Hint

The picture below shows the course of the first example together with its optimal chain.