Universal Cup Judging System

Universal Cup

Time Limit: 2.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓
Statistics

You are given a simple $N$-gon on the $xy$-plane all of whose edges are parallel to either the $x$-axis or the $y$-axis (that is, a polygon with no self-intersections and no holes).

There are $M$ speech locations on the boundary of this polygon.

When movement is allowed only along the boundary of this polygon, find the minimum total travel distance required to visit all speech locations exactly once.

The starting point and ending point of the movement may be chosen arbitrarily on the boundary of the polygon.

Input

The first line contains the number of vertices $N$ of the polygon and the number of speech locations $M$. ($4 \le N \le 10^5, 1 \le M \le 10^5$)

The $(i + 1)$-th line contains the coordinates $(x_i, y_i)$ of the $i$-th vertex of the simple $N$-gon. ($1 \le i \le N, |x_i|, |y_i| \le 10^5$) The edges of the polygon connect the $i$-th vertex and the $(i + 1)$-th vertex. In other words, exactly one of $x_i = x_{i+1}$ or $y_i = y_{i+1}$ holds. (The $(N + 1)$-th vertex denotes the $1$-st vertex.) No vertex lies on an edge. (That is, there is no vertex with an angle of $180$ degrees.)

The $(j + 1 + N)$-th line contains the coordinates $(p_j, q_j)$ of the $j$-th speech location. ($1 \le j \le M, |p_j|, |q_j| \le 10^5$) These points are all distinct and lie on the boundary of the given polygon. (This includes vertices.)

All input values are integers.

Output

Print the answer.

Examples

Input 1

4 3
0 0
3 0
3 2
0 2
0 0
3 0
3 2

Output 1

5

Input 2

6 4
0 0
3 0
3 1
2 1
2 2
0 2
3 0
0 2
2 1
1 2

Output 2

5

Input 3

10 10
-12 -11
-12 9
3 9
3 1
-3 1
-3 -1
8 -1
8 -10
-5 -10
-5 -11
3 1
3 7
7 -1
-3 0
-4 -10
-12 8
-10 -11
-3 9
-12 -10
8 -7

Output 3

74

Note

In the first example, there are points on three vertices of a rectangle, and visiting them in the order $1$-st $(0, 0) \to 2$-nd $(3, 0) \to 3$-rd $(3, 2)$ gives the shortest total travel distance, which is $3 + 2 = 5$.

problem_17716_25.png

Figure for Sample Input 1

problem_17716_26.png

Figure for Sample Input 2

problem_17716_24.png

Figure for Sample Input 3

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#1525EditorialOpen题解jianglyView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.