众所周知,ZJM 住在 B 站。
这一天 Q 老师来找 ZJM 玩,ZJM 决定向 Q 老师展现一下自己快速看番的本领。
ZJM 让 Q 老师任意挑选了一部番,Q老师一眼看中了《辉夜大小姐想让我告白》,已知这部番一共有 N 个精彩片段,每个精彩片段用 [L[i], R[i]] 来表示。
ZJM 从第一分钟开始看,每次可以按下快进键,快进 X 分钟,问 ZJM 最快需要多久可以看完这部番。
Input
第一行给出两个整数 N、X(1 ≤ N ≤ 50, 1 ≤ X ≤ 1e5)
接下来 N 行,每行给出两个整数,L[i]、R[i](1 ≤ L[i] ≤ R[i] ≤ 1e5)
数据保证,R[i-1] < L[i]
Output
输出一个整数,表示 ZJM 最快看完这部番的时间。
Examples
Input
2 3
5 6
10 12
Output
6
Input
1 1
1 100000
Output
100000
Note
在第一个样例中,ZJM 从第 1 分钟开始看,由于 [1, 3] 区间内没有精彩片段,因此 ZJM 快进到了第 4 分钟。
观看完 [4, 6] 区间的内容后,时间来到了第 7 分钟,由于 [7, 9] 区间内没有精彩片段,因此 ZJM 快进到了第 10 分钟,观看完 [10, 12] 区间的内容后,看番结束。
一共耗时 3 + 3 = 6 分钟。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main() {
int n,x, a,b;
scanf("%d%d", &n,&x);
int L[100005]={0};
int themax=0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a,&b);
for(int j=a;j<=b;j++){
L[j]=1;
}
if(b>themax) themax=b;
}
int current=1;
int count=0;
while(current<=themax){
if(L[current]==0){
int j=current;
bool ok=1;
for(;j<current+x && j<=themax;j++){
if(L[j]){
ok=0;
break;
}
}
if(!ok){
count+=j-current;
while(L[j] && j<current+x){
j++;
count++;
}
current=j;
}
else{
current+=x;
}
}
else{
int j=current;
while(L[j] && j<=themax){
j++;
count++;
}
current=j;
}
}
printf("%d\n",count);
return 0;
}