Package Pickup

시간 제한4초메모리 제한2048 MB

문제

Farmer John has distributed cows and packages in a weird pattern across the number line using the following process:

  • Farmer John chooses a number $M$ ($1 \le M \le 10^{18}$).
  • Farmer John chooses $N$ ($1 \le N \le 2 \cdot 10^4)$ intervals $[L_i, R_i]$ to distribute cows in ($1 \le L_i \le R_i \le 10^{18}$). He then places cows at locations $L_i, L_i + M, L_i + 2M, \ldots, R_i$. It is guaranteed that $R_i - L_i$ is a multiple of $M$.
  • Farmer John chooses $P$ ($1 \le P \le 2 \cdot 10^4)$ intervals $[A_i, B_i]$ to distribute packages in ($1 \le A_i \le B_i \le 10^{18}$). He then places packages at locations $A_i, A_i + M, A_i + 2M, \ldots, B_i$. It is guaranteed that $B_i - A_i$ is a multiple of $M$.

Once the cows and packages are distributed, Farmer John wants to see how long it takes the cows to pick up the packages. Every second, Farmer John can issue a command to a single cow to move one unit left or right of their current position with his handy walkie talkie. If a cow travels to the position where a package is located, they are able to pick it up. Farmer John wants to know the minimum time in seconds that it would take the cows to pick up every package.

입력

The first line contains $M$, $N$, and $P$.

The next $N$ lines each contain two integers $L_i$ and $R_i$.

The next $P$ lines each contain two integers $A_i$ and $B_i$.

출력

Output a single integer, representing the minimum amount of time it can take the cows to pick up all the packages, given that every second, he can issue a single left/right command to a single cow.