View as PDF

Points: 12 (partial)
Time limit: 1.0s
Java 8 2.0s
PyPy 2 7.0s
Memory limit: 512M

Author:
Problem type

Long live the Queen!

After a heavy bombardment by the forces of Queen Alice, the Art Academy has started to collapse. Since hewmatt10, the faithful leader of the Art Academy, was so fanatical in his obsession for art, pieces have been hung everywhere; including on the ceiling. hewmatt10 has already managed to remove all his pieces on the walls, so he turns his salvation efforts to the sky, and prepares to catch the pieces as they fall. Fortunately for him, he is not alone. His loyal aide has come back to help him, and together, they will save the art!

The Academy will be represented on a 2D plane where artworks will fall sequentially, given as a point (, ). Either one of them must collect a given piece of art when it falls. Determine the minimum sum of the Manhattan distances that hewmatt10 and his aide will have to travel.

For example, if there was a single painting that falls, hewmatt10 started at , his aide started at , and the painting falls at , the optimal Manhattan distance would be for his aide to move a distance of .

#### Input Specification

The first line will contain the integer , representing the number of artworks that will fall.

The next line will have the integers , , , and , representing the coordinates of where hewmatt10 and his aide will start.

The following lines will have two integers and , representing the coordinate of where the artwork will drop down to.

#### Constraints

$$1 \le N \le 2\,000, \space$$-

No further constraints.

#### Output Specification

Save the Academy! Save the artwork! Output the minimum total distance hewmatt10 and his aide will have to travel in order to catch all the artwork.

#### Sample Input 1

3
1 1 4 4
3 3
1 0
5 2

#### Sample Output 1

6