Universal Cup Judging System

Universal Cup

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

喺 $xy$ 平面上面畀定一個簡單 $N$ 邊形,佢嘅所有邊都平行於 $x$ 軸或者 $y$ 軸(即係一個冇自交而且冇窿嘅多邊形)。

喺呢個多邊形嘅邊界上面有 $M$ 個演講地點。

當只允許沿住呢個多邊形嘅邊界移動嗰陣,請搵出恰好造訪所有演講地點一次所需嘅最細總移動距離。

移動嘅起點同終點可以喺多邊形嘅邊界上面任意揀選。

Input

第一行包含多邊形嘅頂點數量 $N$ 同埋演講地點嘅數量 $M$。($4 \le N \le 10^5, 1 \le M \le 10^5$)

第 $(i + 1)$ 行包含簡單 $N$ 邊形第 $i$ 個頂點嘅座標 $(x_i, y_i)$。($1 \le i \le N, |x_i|, |y_i| \le 10^5$)多邊形嘅邊連接住第 $i$ 個頂點同第 $(i + 1)$ 個頂點。換句話講,$x_i = x_{i+1}$ 或者 $y_i = y_{i+1}$ 之中恰好有一個成立。(第 $(N + 1)$ 個頂點代表第 $1$ 個頂點。)冇任何頂點位於邊上面。(即係話,唔存在角度為 $180$ 度嘅頂點。)

第 $(j + 1 + N)$ 行包含第 $j$ 個演講地點嘅座標 $(p_j, q_j)$。($1 \le j \le M, |p_j|, |q_j| \le 10^5$)呢啲點全部互不相同,而且都位於畀定多邊形嘅邊界上面。(包括頂點。)

所有輸入數值都係整數。

Output

印出答案。

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

喺第一個範例入面,有三個點位於長方形嘅頂點上面,按順序造訪第 $1$ 個 $(0, 0) \to 2$ 個 $(3, 0) \to 3$ 個 $(3, 2)$ 可以得到最短總移動距離,即係 $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.