Self-Adjusting Segment Tree
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

The fastest segment tree is the self-adjusting one according to the queries.
The root of self-adjusting segment tree is , and for each vertex , we can choose arbitrary and then split it into two vertices and . The following figure is an illustration of such a segment tree.
c.jpg
When we locate an interval in the segment tree, we need to visit some vertices. For example, the vertices in purple rectangles are visited when locating the interval .
Formally, when we locate the interval , let be the set with minimum number of vertices such that their intervals are disjoint and the union of there intervals is , all the vertices in S or the ancestors of some vertex in will be visited.
Now you are given queries  and you need to find a segment tree that minimizes the sum of visited vertices for all queries.

输入描述:

The first line contains two integers .
Each of the following lines contains two integers .

输出描述:

Output one integer - the minimum sum of visited vertices for all queries.
示例1

输入

复制
5 5
4 5
2 4
2 5
1 5
1 2

输出

复制
16