Problem: 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

The first line contains a single integer n (1≤ n≤ 2000) — the number of curses.

The next line contains a single integer x (1≤ x≤ 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≤ tl_i≤ tr_i≤ 10^6, 1≤ l_i≤ r_i≤ 10^6).

Output

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).

Examples

Input


2
1
1 2 1 2
2 3 2 3


Output


2

Input


3
4
1 4 1 2
1 4 4 15
6 7 1 4


Output


8

Input


4
3
1 5 1 1
4 10 1 4
1 2 3 13
1 10 7 19


Output


14

Input


7
5
78 96 76 91
6 16 18 37
53 63 40 56
83 88 21 38
72 75 17 24
63 63 53 60
34 46 60 60


Output


20

Solution (in CPP):

Solution:

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

struct LineReader
{

    std::vector<uint32_t> const &readLine()
    {
        std::getline(std::cin, line);
        values.clear();
        uint32_t value = 0;
        bool gotValue = false;
        for (std::size_t i = 0; i < line.length(); ++i)
        {
            const char c = line[i];
            if (c >= '0' && c <= '9')
            {
                value = (value * 10) + (c - '0');
                gotValue = true;
            }
            else if (gotValue)
            {
                values.emplace_back(value);
                value = 0;
                gotValue = false;
            }
        }
        if (gotValue)
        {
            values.emplace_back(value);
        }
        return values;
    }

    std::string line;
    std::vector<uint32_t> values;
};

struct Box
{

    Box() = default;

    Box(uint32_t const &p) : tl(1), tr(1), l(p), r(p)
    {
    }

    Box(std::vector<uint32_t> const &data) : tl(data[0]), tr(data[1]), l(data[2]), r(data[3])
    {
    }

    bool operator<(Box const &other) const
    {
        return (tl > other.tl) || ((tl == other.tl) && (l > other.l));
    }

    void add(Box const &other)
    {
        tl = std::min(tl, other.tl);
        tr = std::max(tr, other.tr);
        l = std::min(l, other.l);
        r = std::max(r, other.r);
    }

    void enlarge()
    {
        --tl;
        ++tr;
        --l;
        r += 2;
    }

    uint32_t tl{0};
    uint32_t tr{0};
    uint32_t l{0};
    uint32_t r{0};
};

struct CompressedRow
{
    static constexpr uint32_t infinity{static_cast<uint32_t>(-1)};

    struct Range
    {

        uint32_t operator[](uint32_t const x) const
        {
            if (x < c)
            {
                return d + (c - x);
            }
            else
            {
                return d + (x - c);
            }
        }

        bool is_blocked() const
        {
            return d == infinity;
        }

        bool is_valid_after(uint32_t const t) const
        {
            return (!is_blocked()) || (c >= t);
        }

        void set_l(uint32_t const nl)
        {
            l = nl;
            if (is_blocked())
            {
                return;
            }
            if (c < l)
            {
                d += (l - c);
                c = l;
            }
        }

        void set_r(uint32_t const nr)
        {
            r = nr;
            if (is_blocked())
            {
                return;
            }
            if (c > r)
            {
                d += (c - r);
                c = r;
            }
        }

        void advance(uint32_t const dt)
        {
            if (is_blocked())
            {
                return;
            }
            c += dt;
            if (c > r)
            {
                d += (c - r);
                c = r;
            }
        }

        void block_until(uint32_t const t)
        {
            if (is_blocked())
            {
                c = std::max(c, t);
            }
            else
            {
                c = t;
                d = infinity;
            }
        }

        void block_l()
        {
            if ((is_blocked()) || (c > l))
            {
                return;
            }
            ++c;
            ++d;
            if (c > r)
            {
                d += (c - r);
                c = r;
            }
        }

        uint32_t l;
        uint32_t c;
        uint32_t r;
        uint32_t d;
    };

    CompressedRow(uint32_t const l, uint32_t const r, uint32_t const c) : time(0), l(l), r(r)
    {
        distance.emplace_back(Range{0, c, infinity, 0});
    }

    uint32_t min()
    {
        uint32_t result = infinity;
        for (Range &dr : distance)
        {
            result = std::min(result, dr.d);
        }
        return result;
    }

    void advance(uint32_t new_time)
    {
        const uint32_t dt = new_time - time;
        uint32_t offset = 0;
        for (uint32_t i = 0; i < distance.size(); ++i)
        {
            if ((!distance[i].is_blocked()) && (i + 1 < distance.size()) && (!distance[i + 1].is_blocked()))
            {
                uint32_t j = i + 2;
                while ((j < distance.size()) && (!distance[j].is_blocked()))
                {
                    ++j;
                }
                --j;
                distance[i].r = std::min(distance[i].r + dt, distance[j].r);
                distance[i].advance(dt);
                if (offset > 0)
                {
                    distance[i - offset] = distance[i];
                }
                while (i < j)
                {
                    ++i;
                    if (i < j)
                    {
                        distance[i].r = std::min(distance[i].r + dt, distance[j].r);
                    }
                    distance[i].advance(dt);
                    distance[i].l += dt;
                    if (distance[i].l > distance[i].r)
                    {
                        ++offset;
                    }
                    else if (offset > 0)
                    {
                        distance[i - offset] = distance[i];
                    }
                }
            }
            else
            {
                distance[i].advance(dt);
                if (offset > 0)
                {
                    distance[i - offset] = distance[i];
                }
            }
        }
        if (offset > 0)
        {
            distance.resize(distance.size() - offset);
        }
        time = new_time;
    }

    void change_boxes(std::vector<Box> const &boxes)
    {
        uint32_t i = 0;
        for (Box const &box : boxes)
        {
            while (distance[i].r < box.l)
            {
                if (distance[i].is_valid_after(time))
                {
                    new_distance.emplace_back(distance[i]);
                    fix_new_distance();
                }
                ++i;
            }
            if (distance[i].l < box.l)
            {
                if (distance[i].is_valid_after(time))
                {
                    new_distance.emplace_back(distance[i]);
                }
                distance[i].set_l(box.l);
                new_distance.back().set_r(box.l - 1);
                fix_new_distance();
            }
            while (distance[i].r <= box.r)
            {
                new_distance.emplace_back(distance[i]);
                new_distance.back().block_until(box.tr);
                fix_new_distance();
                ++i;
            }
            if (distance[i].l <= box.r)
            {
                new_distance.emplace_back(distance[i]);
                new_distance.back().block_until(box.tr);
                new_distance.back().set_r(box.r);
                fix_new_distance();
                distance[i].set_l(box.r + 1);
            }
            distance[i].block_l();
        }
        while (i < distance.size())
        {
            if (distance[i].is_valid_after(time))
            {
                new_distance.emplace_back(distance[i]);
                fix_new_distance();
            }
            ++i;
        }
        distance.swap(new_distance);
        new_distance.clear();
    }

    void fix_new_distance()
    {
        while (new_distance.size() >= 2)
        {
            Range &dl = new_distance[new_distance.size() - 2];
            Range &dr = new_distance.back();
            if (dl.r + 1 < dr.l)
            {
                if ((dl.is_blocked()) && (dr.is_blocked()))
                {
                    if (dl.c < dr.c)
                    {
                        dl.r = dr.l - 1;
                    }
                    else
                    {
                        dr.l = dl.r + 1;
                    }
                }
                else if (dl.is_blocked())
                {
                    dr.l = dl.r + 1;
                }
                else
                {
                    dl.r = dr.l - 1;
                }
            }
            if ((dl.is_blocked()) && (dr.is_blocked()) && (dl.c == dr.c))
            {
                dl.r = dr.r;
                new_distance.pop_back();
            }
            if ((!dl.is_blocked()) && (!dr.is_blocked()))
            {
                if (dl[dl.c] >= dr[dl.c])
                {
                    dl.r = dr.r;
                    dl.c = dr.c;
                    dl.d = dr.d;
                    new_distance.pop_back();
                    continue;
                }
                else if (dl[dr.c] <= dr[dr.c])
                {
                    dl.r = dr.r;
                    new_distance.pop_back();
                }
                else
                {
                    const uint32_t c = (dl.c + dr.c - dl.d + dr.d) / 2;
                    dl.r = c;
                    dr.l = c + 1;
                }
            }
            break;
        }
    }

    std::vector<Range> distance;
    uint32_t time;
    uint32_t l;
    uint32_t r;

    std::vector<Range> new_distance;
};

struct NewBoxesExtractor
{

    std::vector<Box> &operator()(std::vector<Box> &boxes, uint32_t time)
    {
        new_boxes.clear();
        temp_boxes.clear();
        while ((!boxes.empty()) && (boxes.back().tl == time))
        {
            if ((temp_boxes.empty()) || (boxes.back().l < temp_boxes.back().l))
            {
                add_box(boxes.back());
                boxes.pop_back();
            }
            else
            {
                add_box(temp_boxes.back());
                temp_boxes.pop_back();
            }
        }
        while (!temp_boxes.empty())
        {
            add_box(temp_boxes.back());
            temp_boxes.pop_back();
        }
        return new_boxes;
    }

    void add_box(Box const &box)
    {
        if ((new_boxes.empty()) || (new_boxes.back().r < box.l))
        {
            new_boxes.emplace_back(box);
        }
        else if (box.tr > new_boxes.back().tr)
        {
            if (box.r < new_boxes.back().r)
            {
                temp_boxes.emplace_back(new_boxes.back());
                temp_boxes.back().l = box.r + 1;
            }
            new_boxes.back().r = box.l - 1;
            if (new_boxes.back().r < new_boxes.back().l)
            {
                new_boxes.pop_back();
            }
            new_boxes.emplace_back(box);
        }
        else if (box.r > new_boxes.back().r)
        {
            const uint32_t l = new_boxes.back().r + 1;
            new_boxes.emplace_back(box);
            new_boxes.back().l = l;
        }
    }

    std::vector<Box> new_boxes;
    std::vector<Box> temp_boxes;
};

int main()
{
    LineReader input;
    const uint32_t number_of_boxes = input.readLine()[0];

    uint32_t start_position = input.readLine()[0];

    if (number_of_boxes == 0)
    {
        std::cout << '0' << std::endl;
        return EXIT_SUCCESS;
    }

    Box bb{start_position};

    std::vector<Box> boxes;
    boxes.reserve(number_of_boxes);
    std::vector<uint32_t> critical_times;
    critical_times.reserve(2 * number_of_boxes);
    for (std::size_t i = 0; i < number_of_boxes; ++i)
    {
        boxes.emplace_back(input.readLine());
        critical_times.emplace_back(boxes.back().tl);
        critical_times.emplace_back(boxes.back().tr + 1);
        bb.add(boxes.back());
    }
    std::sort(boxes.begin(), boxes.end());
    sort(critical_times.begin(), critical_times.end());
    critical_times.erase(std::unique(critical_times.begin(), critical_times.end()), critical_times.end());

    bb.enlarge();

    CompressedRow row{bb.l, bb.r, start_position};

    NewBoxesExtractor new_boxes_extractor;
    for (uint32_t const time : critical_times)
    {
        row.advance(time);
        row.change_boxes(new_boxes_extractor(boxes, time));
    }

    std::cout << row.min() << std::endl;

    return EXIT_SUCCESS;
}