{"trustable":false,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript type\u003d\"text/javascript\" async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS_HTML-full\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"MD","content":"### Description\n\nThere are $N$ teleports (numbered from $1$ to $N$) and $M$ chests (numbered from $1$ to $M$) on a number axis. Teleport $i$ is located in point $A_i$, and chest $i$ is located in point $B_i$.\n\nBeing in the same point with a teleport, one can instantly relocate to another point with the teleport. Being in the same point with a chest, one can instantly obtain this chest.\n\nYou are in point $A_1$ and must obtain all chests, then return back to point $A_1$. You can move along with the number axis in any direction at the speed $1$. Determine the minimum time to obtain all chests.\n\n### Constraints\n\n- $1 \\leq N, M \\leq 200 \\, 000$\n- $-10^9 \\leq A_i, B_i \\leq 10^9$ for $1 \\leq i \\leq N$.\n- $A_i \\leq A_{i+1}$ for $1 \\leq i \u003c N$.\n- $B_i \\leq B_{i+1}$ for $1 \\leq i \u003c N$.\n- All values in input are integers.\n\n### Input\n\n\u003cpre\u003e\nN M\nA\u003csub\u003e1\u003c/sub\u003e A\u003csub\u003e2\u003c/sub\u003e … A\u003csub\u003eN\u003c/sub\u003e\nB\u003csub\u003e1\u003c/sub\u003e B\u003csub\u003e2\u003c/sub\u003e … B\u003csub\u003eM\u003c/sub\u003e\n\u003c/pre\u003e\n\n### Output\n\nOutput the minimum time to obtain all chests.\n\n### Sample Input 1\n\n\u003cpre\u003e\n2 4\n0 10\n-1 1 9 11\n\u003c/pre\u003e\n\n### Sample Output 1\n\n\u003cpre\u003e\n8\n\u003c/pre\u003e\n\n### Sample Input 2\n\n\u003cpre\u003e\n2 2\n0 10\n4 6\n\u003c/pre\u003e\n\n### Sample Output 2\n\n\u003cpre\u003e\n10\n\u003c/pre\u003e\n\n### Sample Input 3\n\n\u003cpre\u003e\n1 1\n1000000000\n-1000000000\n\u003c/pre\u003e\n\n### Sample Output 3\n\n\u003cpre\u003e\n4000000000\n\u003c/pre\u003e"}}]}