In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].

If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.

The GasBit Concern intends to dominate the gas market in Byteotia. The experts have already marked the optimal gas extraction points and distribution stations on the Byteotian map. Now it is time to assign the distribution stations to the extraction points. Each distribution station is to be connected to exactly one extraction point and vice versa, each extraction point to exactly one station.

GasBit specializes in building gas pipelines leading from the extraction points to the distribution stations in the south or east direction - each gas pipeline built is to be (in a bird's eye view) in the shape of a broken line whose each consecutive segment leads south or east and is perpendicular to the previous one. The concern's board of directors wonders how to assign the distribution stations to the extraction points in such a way that the total length of the necessary pipelines be minimal. Crossing of pipelines is not a problem, as the colliding pipes can be put at different depths underground.

Write a programme that:

- reads from the standard input the planned locations of the gas extraction points and distribution stations,
- determines such an assignment of the distribution stations to the extraction points that allows minimal total length of necessary pipelines that have to be built to connect them,
- writes out the outcome to the standard output.

The first line of the standard input contains one integer (), denoting the number of extraction points (equal to the number of distribution stations). The following lines contain two integers and ( for ) each, separated by a single space and denoting the coordinates of the extraction points. One moves east as rises and moves north as rises. The next lines contain two integers and ( for ) each, separated by a single space and denoting the coordinates of the distribution stations.

Both the extraction points and the distribution stations are numbered with integers from to in the order they appear in the input. No pair of coordinates appears twice in one input data set. Furthermore for each input data set there is an assignment of the distribution stations to the extraction points that can be realized by pipelines leading south or east only.

The first line of the standard output should contain one integer denoting the minimal total length of all the gas pipelines that need to be built. Then an exemplary description of an extraction points - distribution stations assignment that attains this minimum should follow. Each of the following lines should contain two integers separated by a single space and denoting the number of the extraction point and the number of the distribution station respectively that should be connected by a pipeline. The order of the pairs may be arbitrary. If many optimal solutions exist, your programme may write out one arbitrarily chosen.

For the input data:

3 3 5 1 2 4 3 6 3 5 2 2 1

the correct result is:

9 2 3 1 2 3 1

*Task author: Jakub Radoszewski.*