Teodor is not a liar!
Contest: Codeforces Round 468 (Div. 1, based on Technocup 2018 Final Round)
Difficulty rating: 1900
Tags: data structures, dp

Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge [1;m] segment. One day Teodor noticed that picture he just drawn has one interesting feature: there doesn't exist an integer point, that belongs each of segments in the picture. Having discovered this fact, Teodor decided to share it with Sasha.

Sasha knows that Teodor likes to show off so he never trusts him. Teodor wants to prove that he can be trusted sometimes, so he decided to convince Sasha that there is no such integer point in his picture, which belongs to each segment. However Teodor is lazy person and neither wills to tell Sasha all coordinates of segments' ends nor wills to tell him their amount, so he suggested Sasha to ask him series of questions 'Given the integer point xi, how many segments in Fedya's picture contain that point?', promising to tell correct answers for this questions.

Both boys are very busy studying and don't have much time, so they ask you to find out how many questions can Sasha ask Teodor, that having only answers on his questions, Sasha can't be sure that Teodor isn't lying to him. Note that Sasha doesn't know amount of segments in Teodor's picture. Sure, Sasha is smart person and never asks about same point twice.

Input format:
First line of input contains two integer numbers: n and m (1 ≤ n, m ≤ 100 000) — amount of segments of Teodor's picture and maximal coordinate of point that Sasha can ask about.

ith of next n lines contains two integer numbers li and ri (1 ≤ li ≤ ri ≤ m) — left and right ends of ith segment in the picture. Note that that left and right ends of segment can be the same point.

It is guaranteed that there is no integer point, that belongs to all segments.

Output format:
Single line of output should contain one integer number k – size of largest set (xi, cnt(xi)) where all xi are different, 1 ≤ xi ≤ m, and cnt(xi) is amount of segments, containing point with coordinate xi, such that one can't be sure that there doesn't exist point, belonging to all of segments in initial picture, if he knows only this set(and doesn't know n).

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
First example shows situation where Sasha can never be sure that Teodor isn't lying to him, because even if one knows cnt(xi) for each point in segment [1;4], he can't distinguish this case from situation Teodor has drawn whole [1;4] segment.

In second example Sasha can ask about 5 points e.g. 1, 2, 3, 5, 6, still not being sure if Teodor haven't lied to him. But once he knows information about all points in [1;6] segment, Sasha can be sure that Teodor haven't lied to him.

Example input:
2 4
1 2
3 4

Example output:
4

Example input:
4 6
1 3
2 3
4 6
5 6

Example output:
5

Editorial:
Idea. The main idea is that set of point xi is bad(meaning that Sasha can't be sure, that Teodor hasn't lied, relying only on this information) $$\longleftrightarrow cnt(x_i)$$ satisfies the following property: $$$$.
Solution. Firstly let's calculate cnt(xi) for each integer point in [1;m]. One way to do this is scanning line, which asymptotics is O(m + n). Other approach uses segment tree supporting segment addition queries. In this case asymptotics is O(n·log(m)) .
Now we only need to find longest sequence satisfying this property. Let's consider all possible xi in previous inequation(element that has peak cnt(xi)). Now the answer is length of longest nondecreasing sequence ending in xi +  length of longest nonincreasing sequence, starting in xi - 1. Both lengths can be found in O(1) if one precalculates this lengths for each 1 ≤ i ≤ m, using dynamic programming. Note that you should use O(m·log(m)) algorithm for calculating this dp, not O(m2), otherwise you will end up with TL verdict.
Total asymptotics of this solution is O(m·log(m)) for solution using scanning line or O((n + m)·log(m)) for solution using segment tree.