AquaMoon and Time Stop (hard version)
Contest: Codeforces Round 732 (Div. 1)
Difficulty rating: 3500
Tags: data structures, dp

Note that the differences between easy and hard versions are the constraints on $$$n$$$ and the time limit. You can make hacks only if both versions are solved.

AquaMoon knew through foresight that some ghosts wanted to curse tourists on a pedestrian street. But unfortunately, this time, these ghosts were hiding in a barrier, and she couldn't enter this barrier in a short time and destroy them. Therefore, all that can be done is to save any unfortunate person on the street from the ghosts.

The pedestrian street can be represented as a one-dimensional coordinate system. There is one person hanging out on the pedestrian street. At the time $$$0$$$ he is at coordinate $$$x$$$, moving with a speed of $$$1$$$ unit per second. In particular, at time $$$i$$$ the person will be at coordinate $$$x+i$$$.

The ghosts are going to cast $$$n$$$ curses on the street. The $$$i$$$-th curse will last from time $$$tl_i-1+10^{-18}$$$ to time $$$tr_i+1-10^{-18}$$$ (exclusively) and will kill people with coordinates from $$$l_i-1+10^{-18}$$$ to $$$r_i+1-10^{-18}$$$ (exclusively). Formally that means, that the person, whose coordinate is between $$$(l_i-1+10^{-18},r_i+1-10^{-18})$$$ in the time range $$$(tl_i-1+10^{-18},tr_i+1-10^{-18})$$$ will die.

To save the person on the street, AquaMoon can stop time at any moment $$$t$$$, and then move the person from his current coordinate $$$x$$$ to any coordinate $$$y$$$ ($$$t$$$, $$$x$$$ and $$$y$$$ are not necessarily integers). The movement costs AquaMoon $$$|x-y|$$$ energy. The movement is continuous, so if there exists some cursed area between points $$$x$$$ and $$$y$$$ at time $$$t$$$, the person will die too.

AquaMoon wants to know what is the minimum amount of energy she needs to spend in order to save the person on the street from all $$$n$$$ curses. But she is not good at programming. As her friend, can you help her?

Input format:
The first line contains a single integer $$$n$$$ ($$$1\le n\le 2 \cdot 10^5$$$) — the number of curses.

The next line contains a single integer $$$x$$$ ($$$1\le x\le 10^6$$$) — the initial coordinate of the person.

The following $$$n$$$ lines contain four integers $$$tl_i$$$, $$$tr_i$$$, $$$l_i$$$, $$$r_i$$$ each ($$$1\le tl_i\le tr_i\le 10^6$$$, $$$1\le l_i\le r_i\le 10^6$$$).

Output format:
Print a single integer — the minimum energy which AquaMoon needs to spent, rounded up to the nearest integer (in case there are two nearest integers you should round the answer to the highest of them).

Constraints:
Time limit: 7.0 s
Memory limit: 1024.0 MB

Example input:
2
1
1 2 1 2
2 3 2 3

Example output:
2

Example input:
3
4
1 4 1 2
1 4 4 15
6 7 1 4

Example output:
8

Editorial:
We scan through the time, each time. We need to get minimum answer for all positions of this certain time.
We can use some data structure to maintain it.
In detail, we use balance tree to maintain every segments which are not covered for some time $$$t$$$.
We can see that after some time, the answer of every position of the segment is shifted. A new arithmetic sequence is added to the left, and the rightmost part is erased. We use lazy tags to maintain them(We update the segment only when we need to use the answer of it).
When a segment is banned, then we just erase it.
When a segment is added, we need to consider the answer of at most two neighboring segment.
There are two conditions:
1. We use two of the neighboring segment to get the answer of the new-added segment. We need to merge the two segments and insert two arithmetic sequences between them.
2. We use only one of the neighboring segment to get the answer of the new-added segment, and this one will even influence the other. We just use brute force to erase some arithmetic sequences of the influenced segment at the end.
For each segment, we use a balance tree to maintain the arithmetic sequences in it.
Since each time we will only add segments of a constant number, so the time complexity is $$$O(nlogn)$$$.
(The easy version is for solution without too many data structures)