You have decided to watch the best moments of some movie. There are two buttons on your player:   Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie.  Skip exactly x minutes of the movie (x is some fixed positive integer). If the player is now at the t-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (t + x). 

Initially the movie is turned on in the player on the first minute, and you want to watch exactly n best moments of the movie, the i-th best moment starts at the l_{i}-th minute and ends at the r_{i}-th minute (more formally, the i-th best moment consists of minutes: l_{i}, l_{i} + 1, ..., r_{i}). 

Determine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments?


-----Input-----

The first line contains two space-separated integers n, x (1 ≤ n ≤ 50, 1 ≤ x ≤ 10^5) — the number of the best moments of the movie and the value of x for the second button.

The following n lines contain the descriptions of the best moments of the movie, the i-th line of the description contains two integers separated by a space l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ 10^5).

It is guaranteed that for all integers i from 2 to n the following condition holds: r_{i} - 1 < l_{i}.


-----Output-----

Output a single number — the answer to the problem.


-----Examples-----
Input
2 3
5 6
10 12

Output
6

Input
1 1
1 100000

Output
100000



-----Note-----

In the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie.

In the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie.

Source: codeforces | Difficulty: EASY
Tags: Implementation

Greedy algorithms


Solutions:
import sys

def main():
	(n, x) = [int(i) for i in input().split()]
	ans = 0
	s = 1
	for k in range(n):
		(l, r) = [int(i) for i in input().split()]
		ans += (l - s) % x + r - l + 1
		s = r + 1
	print(ans)
main()


import sys
import math as m

def main():
	pass

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def lts(l):
	s = ''.join(map(str, l))
	return s

def stl(s):
	l = list(s)
	return l

def sq(a, target, arr=[]):
	s = sum(arr)
	if s == target:
		return arr
	if s >= target:
		return
	for i in range(len(a)):
		n = a[i]
		remaining = a[i + 1:]
		ans = sq(remaining, target, arr + [n])
		if ans:
			return ans
mod = int(1000000000.0) + 7

def sinp():
	return sys.stdin.readline().strip()

def iinp():
	return int(input())

def ninp():
	return map(int, sys.stdin.readline().strip().split())

def llinp():
	return list(map(int, sys.stdin.readline().strip().split()))

def p(xyz):
	print(xyz)

def p2(a, b):
	print(a, b)
(n, x) = ninp()
s = 0
ans = 0
for i in range(n):
	(l, r) = ninp()
	while s + x < l:
		s = s + x
	ans = ans + r - s
	s = r
p(ans)


a = [int(i) for i in str(input()).split()]
n = a[0]
x = a[1]
(li, ls) = ([], [])
for i in range(n):
	h = [int(i) for i in str(input()).split()]
	li.append(h[0])
	ls.append(h[1])
minutei = 1
watchedMin = 0
best_i = 0
while minutei <= ls[-1] and best_i < n:
	if (li[best_i] - minutei) // x > 0:
		minutei += x * ((li[best_i] - minutei) // x)
	else:
		watchedMin += ls[best_i] - minutei + 1
		minutei = ls[best_i] + 1
		best_i += 1
print(watchedMin)


(n, x) = map(int, input().split())
(minutes, t) = (0, 1)
for _ in range(n):
	(l, r) = map(int, input().split())
	(minutes, t) = (minutes + (l - t) % x + r - l + 1, r + 1)
print(minutes)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scan_int = lambda : int(input())
scan_string = lambda : input().rstrip()
read_list = lambda : list(read())
read = lambda : map(int, input().split())
read_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(beauty, s, n, count):
	pass
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'.upper()
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect

def solve():
	(n, x) = read()
	s = 0
	ptr = 1
	for i in range(n):
		(l, r) = read()
		s += (l - ptr) % x
		s += r - l + 1
		ptr = r + 1
	print(s)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(1):
	solve()


(n, x) = map(int, input().split())
cur = 1
total = 0
for y in range(n):
	(l, r) = map(int, input().split())
	while cur + x <= l:
		cur += x
	total += r - cur + 1
	cur = r + 1
print(total)


(n, x) = map(int, input().split())
r = lambda : list(map(int, input().split()))
arr = [r() for _ in range(n)]
t = 1
watch = 0
i = 0
while 1:
	while t + x <= arr[i][0]:
		t += x
	watch += arr[i][1] - t + 1
	t = arr[i][1] + 1
	i += 1
	if i == n:
		break
print(watch)


(n, x) = map(int, input().strip().split())
total = 0
arr = [1]
for i in range(n):
	(a, b) = map(int, input().strip().split())
	total += b - a + 1
	arr.append(a)
	arr.append(b + 1)
for i in range(1, 2 * n + 1, 2):
	total += (arr[i] - arr[i - 1]) % x
print(total)


(x, y) = map(int, input().split())
(i, ans, prev) = (0, 0, 1)
while i < int(x):
	(a, b) = map(int, input().split())
	ans += (a - prev) % y + (b - a + 1)
	prev = b + 1
	i += 1
print(ans)


(n, x) = (int(i) for i in input().split())
(cur, result) = (0, 0)
for i in range(n):
	(left, right) = (int(i) for i in input().split())
	result += right - left + 1 + (left - cur - 1) % x
	cur = right
print(result)


(n, x) = list(map(int, input().split()))
s = 1
total_min = 0
for i in range(n):
	(l, r) = list(map(int, input().split()))
	total_min += (l - s) % x + (r - l + 1)
	s = r + 1
print(total_min)


(n, k) = map(int, input().split())
x = []
i = 1
c = 0
for z in range(n):
	(a, b) = map(int, input().split())
	while i + k <= a:
		i += k
	while i <= b:
		i += 1
		c += 1
print(c)


(n, x) = [int(i) for i in input().split()]
(prevBeg, prevEnd) = (0, 0)
totalTime = 0
for i in range(n):
	(curBeg, curEnd) = [int(i) for i in input().split()]
	totalTime += curEnd - curBeg + 1 + (curBeg - prevEnd - 1) % x
	prevBeg = curBeg
	prevEnd = curEnd
print(totalTime)


(n, m) = map(int, input().split())
x = 0
j = 0
for i in range(n):
	(a, b) = map(int, input().split())
	x += b - a + 1
	x += (a - j - 1) % m
	j = b
print(x)


(n, k) = map(int, input().split())
count = 0
st = 1
for i in range(n):
	(l, r) = map(int, input().split())
	skips = (l - st) // k
	count += r - (skips * k + st) + 1
	st = r + 1
print(count)


(n, x) = map(int, input().split())
m = []
for _ in range(n):
	a = tuple(map(int, input().split()))
	m.append(a)
t = 1
c = 0
for i in range(n):
	if m[i][0] - t >= x:
		t += x * ((m[i][0] - t) // x)
		c += m[i][1] - t + 1
		t = m[i][1] + 1
	else:
		c += m[i][1] - t + 1
		t = m[i][1] + 1
print(c)


(n, x) = map(int, input().split())
w = 0
m = 1
for i in range(n):
	(l, r) = map(int, input().split())
	k = r
	(l, r) = (l - m, r - m)
	w += l % x + r - l + 1
	m = k + 1
print(w)


arr = input().split(' ')
n = int(arr[0])
button2 = int(arr[1])
start = 1
total = 0
for i in range(n):
	minutes = input().split(' ')
	firstMin = int(minutes[0])
	lastMin = int(minutes[1])
	totalBestMinutes = lastMin - firstMin + 1
	remainingMinutes = (firstMin - start) % button2
	total += totalBestMinutes + remainingMinutes
	start = lastMin + 1
print(total)


def get():
	s = input().split()
	return [int(s[0]), int(s[1])]
a = get()
n = a[0]
x = a[1]
b = [get() for i in range(n)]
b.sort()
ans = 0
l = r = 1
for a in b:
	if a[0] + 1 > r:
		ans += r - l
		l = r + (a[0] - r) // x * x
		r = a[1] + 1
	elif a[1] + 1 > r:
		r = a[1] + 1
ans += r - l
print(ans)


(n, x) = map(int, input().split())
ans = 0
arr = []
for i in range(n):
	l = list(map(int, input().split()))
	arr.append(l)
i = 1
j = 0
while j < n:
	if i + x <= arr[j][0]:
		i += x
	else:
		ans += arr[j][1] - i + 1
		i = arr[j][1] + 1
		j += 1
print(ans)


class CodeforcesTask499ASolution:

	def __init__(self):
		self.result = ''
		self.n_x = []
		self.best_moments = []

	def read_input(self):
		self.n_x = [int(x) for x in input().split(' ')]
		for x in range(self.n_x[0]):
			self.best_moments.append([int(j) for j in input().split(' ')])

	def process_task(self):
		watch_time = 0
		watch_time += (self.best_moments[0][0] - 1) % self.n_x[1]
		for moment in self.best_moments:
			watch_time += moment[1] - moment[0] + 1
		for x in range(len(self.best_moments) - 1):
			watch_time += (self.best_moments[x + 1][0] - 1 - self.best_moments[x][1]) % self.n_x[1]
		self.result = str(watch_time)

	def get_result(self):
		return self.result
Solution = CodeforcesTask499ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


(n, k) = map(int, input().split())
(cur, res) = (0, 0)
for i in range(n):
	(l, r) = map(int, input().split())
	res += r - l + 1 + (l - cur - 1) % k
	cur = r
print(res)


from sys import stdin
max_val = int(10000000000000.0)
min_val = int(-10000000000000.0)

def read_int():
	return int(stdin.readline())

def read_ints():
	return [int(x) for x in stdin.readline().split()]

def read_str():
	return input()

def read_strs():
	return [x for x in stdin.readline().split()]
(nb_moments, skip) = read_ints()
total = 0
now = 1
for _ in range(nb_moments):
	(begin, end) = read_ints()
	total += (begin - now) % skip + end - begin + 1
	now = end + 1
print(total)


inp = input().split(' ')
num = int(inp[0])
skip = int(inp[1])
out = 0
curr = 1
for x in range(num):
	time = input().split(' ')
	start = int(time[0])
	end = int(time[1])
	while curr + skip <= start:
		curr += skip
	out += end - curr
	out += 1
	curr = end + 1
print(out)


(n, x) = map(int, input().split())
m = []
for i in range(n):
	m.append([int(x) for x in input().split()])
r = 1
t = 0
for i in range(n):
	t += (m[i][0] - r) % x + m[i][1] - m[i][0] + 1
	r = (m[i][1] + 1) % x
print(t)


import sys
bf = sys.stdin
conf = bf.readline().split(' ')
(num, x) = (int(conf[0]), int(conf[1]))
start = 1
length = 0
for a in range(num):
	conf = bf.readline().split(' ')
	(a, b) = (int(conf[0]), int(conf[1]))
	while start + x <= a:
		start += x
	length += b - start + 1
	start = b + 1
print(length)


(n, x) = map(int, input().split())
(m, prev) = (0, 1)
for _ in range(n):
	(l, r) = map(int, input().split())
	m += r - l + 1
	m += (l - prev) % x
	prev = r + 1
print(m)


first_line = list(map(int, input().split(' ')))
number_moments = first_line[0]
x_val = first_line[1]
moments = []
for i in range(number_moments):
	moments.append(list(map(int, input().split(' '))))
current_time = 1
watched = 0
for m in moments:
	remaining = m[0] - current_time
	skippable = remaining // x_val
	current_time += x_val * skippable
	watched += m[1] - current_time + 1
	current_time = m[1] + 1
print(watched)


(n, x) = map(int, input().split())
ans = 0
crt = 1
for i in range(n):
	(l, r) = map(int, input().split())
	ans = ans + (l - crt) % x + r - l + 1
	crt = r + 1
print(ans)


(n, x) = map(int, input().split())
(p, w) = (0, 0)
for _ in range(n):
	(l, r) = map(int, input().split())
	while p < l - x:
		p += x
	w += r - p
	p = r
print(w)


(n, x) = map(int, input().split())
p = []
for i in range(n):
	(a, b) = map(int, input().split())
	p.append([a, b])
p.sort()
w = 1
col = 0
while len(p) != 0:
	while x + w <= p[0][0]:
		w += x
	col += p[0][1] - w + 1
	w = p[0][1] + 1
	p.pop(0)
print(col)


(n, x) = map(int, input().split())
p = 1
s = 0
for _ in range(n):
	(l, r) = map(int, input().split())
	s += (l - p) % x + (r - l + 1)
	p = r + 1
print(s)


(n, x) = map(int, input().split())
B = [list(map(int, input().split())) for _ in range(n)]
i = 1
r = 0
c = 0
b = 0
while b or c < n:
	if b == 0:
		if B[c][0] - i >= x:
			i += x
		elif i == B[c][0]:
			c += 1
			b = 1
		else:
			i += 1
			r += 1
	else:
		if i == B[c - 1][1]:
			b = 0
		i += 1
		r += 1
print(r)


(n, x) = list(map(int, input().split()))
arr = []
for i in range(n):
	(l, r) = list(map(int, input().split()))
	arr.append([l, r])
cnt = 1
ans = 0
for i in range(n):
	while cnt + x <= arr[i][0]:
		cnt += x
	ans += arr[i][1] - cnt + 1
	cnt = arr[i][1] + 1
print(ans)


s = input().split()
n = int(s[0])
x = int(s[1])
l = []
r = []
sum = 0
for i in range(n):
	s = input().split()
	l.append(int(s[0]))
	r.append(int(s[1]))
	sum += int(s[1]) - int(s[0]) + 1
sum += (l[0] - 1) % x
for i in range(n - 1):
	sum += (l[i + 1] - r[i] - 1) % x
print(sum)


(n, x) = map(int, input().split())
m = [[int(x) for x in input().split()] for i in range(n)]
res = 0
prev = 1
for i in range(n):
	(l, r) = m[i]
	left = prev + (l - prev) // x * x
	res += r - left + 1
	prev = r + 1
print(res)


(n, x) = [int(x) for x in input().split()]
c = 1
s = 0
for i in range(n):
	(l, r) = [int(x) for x in input().split()]
	for j in range(2):
		if c + x > l:
			s += r - c + 1
			c = r + 1
			break
		else:
			c += int((l - c) / x) * x
print(s)


(n, x) = map(int, input().split())
t = 1
sumy = 0
for i in range(n):
	(a, b) = map(int, input().split())
	sumy += (a - t) % x + b - a + 1
	t = b + 1
print(sumy)


(n, dt) = map(int, input().split())
time = 1
score = 0
for i in range(n):
	(a, b) = map(int, input().split())
	while time + dt <= a:
		time += dt
	score += b - time + 1
	time = b + 1
print(score)


L1 = input().split()
n = int(L1[0])
x = int(L1[1])
L = []
for i in range(n):
	a = input().split()
	L.append(a)
for i in range(n):
	L[i][0] = int(L[i][0])
	L[i][1] = int(L[i][1])
ans = (L[0][0] - 1) % x + L[0][1] - L[0][0] + 1
for i in range(1, n):
	b = L[i][0] - L[i - 1][1] - 1
	if b < x:
		ans += b
	else:
		ans += b % x
	ans += L[i][1] - L[i][0] + 1
print(ans)


import sys, math
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
ara = [0] * 1000000
(n, m) = invr()
mx = -1
for i in range(n):
	(l, r) = invr()
	mx = max(mx, r, l)
	for j in range(l, r + 1):
		ara[j] = 1
now = 1
watch = 0
curr = 0
while now <= mx:
	if ara[now] == 0:
		watch += 1
		now += 1
		curr += 1
		if curr == m:
			curr = 0
			watch -= m
	else:
		now += 1
		watch += 1
		curr = 0
print(watch)


(x, y) = list(map(int, input().split()))
c = 0
start = 1
for i in range(x):
	(one, two) = list(map(int, input().split()))
	c += (one - start) % y + (two - one) + 1
	start = two + 1
print(c)


(n, x) = map(int, input().split())
p = 1
c = 0
for i in range(n):
	(l, r) = map(int, input().split())
	while l - p >= x:
		p += x
	c += r - p + 1
	p = r + 1
print(c)


(n, x) = map(int, input().split())
l = []
for i in range(n):
	t = list(map(int, input().split()))
	l.append(t)
ans = 0
prev = 1
for i in l:
	if prev == 0:
		ans += i[1] - i[0] + 1
		prev = i[1] + 1
	else:
		ans += (i[0] - prev) % x
		ans += i[1] - i[0] + 1
		prev = i[1] + 1
print(ans)


(nof, nn) = map(int, input().split())
(w, t) = (0, 1)
for i in range(nof):
	(a, b) = map(int, input().split())
	w += (a - t) % nn + (b - a) + 1
	t = b + 1
print(w)


(n, x) = list(map(int, input().rstrip().split()))
l = []
r = []
for _ in range(n):
	a = list(map(int, input().rstrip().split()))
	l += [a[0]]
	r += [a[1]]
j = 1
watch = 0
i = 0
while i < n:
	if j + x < l[i]:
		j = j + x
	elif j + x == l[i]:
		watch += r[i] - l[i] + 1
		j = r[i] + 1
		i += 1
	else:
		watch += r[i] - j + 1
		j = r[i] + 1
		i += 1
print(watch)


(n, x) = map(int, input().split())
index = 1
minutes = 0
for i in range(n):
	(l, r) = map(int, input().split())
	while index + x < l + 1:
		index += x
	minutes += r - index + 1
	index = r + 1
print(minutes)


(n, k) = map(int, input().split())
c = 1
for i in range(n):
	(x, y) = map(int, input().split())
	d = x - c
	c = c + d % k
	c += y - x + 1
print(c - 1)


(n, x) = map(int, input().split())
que = []
for i in range(n):
	que.append(list(map(int, input().split())))
count = 0
i = 0
for k in que:
	while True:
		if i == k[1]:
			break
		if i + x < k[0]:
			i += x
		else:
			i += 1
			count += 1
print(count)


(n_val, x_val) = list(map(int, input().split()))
best = list()
for index in range(n_val):
	best.append(tuple(map(int, input().split())))
start = 1
total = 0
index = 0
while index < n_val:
	while start + x_val <= best[index][0]:
		start += x_val
	total += best[index][1] - start + 1
	start = best[index][1] + 1
	index += 1
print(total)


(n, x) = [int(i) for i in input().split()]
l = n * [0]
r = n * [0]
t = 0
p = [1]
for i in range(n):
	(l[i], r[i]) = [int(i) for i in input().split()]
	t += (l[i] - p[-1]) % x
	p.append(r[i] + 1)
	t += r[i] - l[i] + 1
print(t)


from sys import stdin
(n, x) = map(int, stdin.readline().split())
ans = 0
i = 1
for _ in range(n):
	(l, r) = map(int, stdin.readline().split())
	k = r
	(l, r) = (l - i, r - i)
	ans += l % x + r - l + 1
	i = k + 1
print(ans)


(n, x) = map(int, input().split())
arr = []
for z in range(n):
	(a, b) = map(int, input().split())
	arr.append([a, b])
ans = 0
m = 0
for i in range(n):
	while m + x < arr[i][0]:
		m = m + x
	ans = ans + arr[i][1] - m
	m = arr[i][1]
print(ans)


line = list(map(int, input().split()))
n = line[0]
x = line[1]
tow = []
for i in range(0, n):
	line = list(map(int, input().split()))
	l = line[0]
	r = line[1]
	tow.append(l)
	tow.append(r)
i = 1
t = 0
while len(tow) > 0:
	l = tow.pop(0)
	r = tow.pop(0)
	while i + x <= l:
		i = i + x
	while i <= r:
		i = i + 1
		t = t + 1
print(t)


n_x = [int(c) for c in input().split()]
x = n_x[1]
best = []
for i in range(n_x[0]):
	t_r = [int(c) for c in input().split()]
	best.append(t_r)
output = 0
n = len(best)
r = 1
for i in range(n):
	t = best[i][0]
	a = (t - r) % x
	r = best[i][1] + 1
	output += a + r - t
print(output)


a = list(map(int, input().split()))
n = a[0]
m = a[1]
b = []
while a[0] > 0:
	b += list(map(int, input().split()))
	a[0] -= 1
c = 0
i = 0
c = 0
i = 1
while i < len(b):
	c += b[i] - b[i - 1] + 1
	i += 2
i = 2
c += (b[0] - 1) % m
while i < len(b):
	c += (b[i] - 1 - b[i - 1]) % m
	i += 2
print(c)


(n, k) = map(int, input().split())
start = 1
m = []
ans = 0
for i in range(n):
	(st, se) = map(int, input().split())
	start = start + (st - start) // k * k
	ans += se - start + 1
	start = se + 1
print(ans)


(a, b) = map(int, input().split())
e = 0
w = 1
f = 0
for i in range(a):
	(c, d) = map(int, input().split())
	e = e + d - c + 1
	f = f + (c - w) % b
	w = d + 1
print(e + f)


(numberOfMoments, valueOfSkip) = map(int, input().split())
current = 1
totalCounter = 0
for i in range(0, numberOfMoments):
	(start, end) = map(int, input().split())
	buffer = start - current
	totalCounter += buffer % valueOfSkip
	totalCounter += end - start + 1
	current = end + 1
print(totalCounter)


(n, x) = [int(x) for x in input().split()]
p = 1
ans = 0
for i in range(n):
	(l, r) = [int(x) for x in input().split()]
	p += (l - p) // x * x
	ans += r - p + 1
	p = r + 1
print(ans)


I = lambda : map(int, input().split())
(n, x) = I()
(p, w, i) = (1, 0, 0)
while i < n:
	(l, r) = I()
	p = l - (l - p) % x
	w += r - p + 1
	p = r + 1
	i += 1
print(w)


(n, x) = map(int, input().split())
result = 0
cur = 1
for i in range(n):
	(l, r) = map(int, input().split())
	result += r - l + 1
	result += (l - cur) % x
	cur = r + 1
print(result)


arr = []
(n, x) = map(int, input().split())
for i in range(n):
	(a, b) = map(int, input().split())
	arr.append([a, b])
minutes = 0
for i in arr:
	minutes += i[1] - i[0] + 1
minutes += (arr[0][0] - 1) % x
for i in range(1, n):
	minutes += (arr[i][0] - arr[i - 1][1] - 1) % x
print(minutes)


import sys, itertools, math

def ia():
	return [int(i) for i in sys.stdin.readline().strip().split(' ')]

def ii():
	return int(sys.stdin.readline().strip())

def istr():
	return sys.stdin.readline().strip()
(n, x) = ia()
t = 1
ans = 0
for _ in range(n):
	(l, r) = ia()
	s = (l - t) // x
	t += s * x
	ans += r - t + 1
	t = r + 1
print(ans)


inp = input()
a = inp.split()
for i in range(0, len(a)):
	a[i] = int(a[i])
time = 0
temp = 0
for i in range(0, a[0]):
	inp2 = input()
	b = inp2.split()
	b[0] = int(b[0])
	b[1] = int(b[1])
	time += b[1] - b[0] + 1 + (b[0] - temp - 1) % a[1]
	temp = b[1]
print(time)


def solve(arr, n, k):
	arr.sort()
	current = 1
	skipped = 0
	for i in range(n):
		skipped += (arr[i][0] - current) // k
		current = arr[i][1] + 1
	return arr[n - 1][1] - skipped * k
(n, k) = list(map(int, input().strip().split()))
arr = []
for _ in range(n):
	arr.append(list(map(int, input().strip().split())))
result = solve(arr, n, k)
print(result)


parse = lambda st: map(int, st.split())

def solve(x, moments):
	start = 0
	watch = 0
	for (l, r) in moments:
		watch += (l - start - 1) % x
		watch += r - l + 1
		start = r
	return watch
(n, x) = parse(input())
res = solve(x, [parse(input()) for _ in range(n)])
print(res)


l1 = [int(x) for x in input().split()]
(n, x) = map(int, l1)
l2 = []
for _ in range(n):
	l2.append([int(x) for x in input().split()])
ans = 0
old = 1
for i in range(n):
	current = l2[i][0]
	distance = current - old
	not_skipped = (current - old) % x
	ans += not_skipped
	old = l2[i][1] + 1
	ans += l2[i][1] - l2[i][0] + 1
print(ans)


(n, x) = [int(i) for i in input().split()]
(totalMinutes, idx) = (0, 1)
for i in range(0, n):
	(l, r) = [int(i) for i in input().split()]
	totalMinutes += r - l + 1 + (l - idx) % x
	idx = r + 1
print(totalMinutes)


(a, b) = map(int, input().split())
l = [list(map(int, input().split())) for i in range(a)]
sum = 0
time = 1
for i in range(a):
	x = int((l[i][0] - time) / b)
	time = time + x * b
	sum = sum + (l[i][1] - time + 1)
	time = l[i][1] + 1
print(sum)


def tv_remote(a, t):
	current = 1
	watch = 0
	for (x, y) in a:
		diff = x - current
		watch += x - (current + diff // t * t)
		watch += y - x + 1
		current = y + 1
	return watch
(n, t) = list(map(int, input().split()))
a = []
for q in range(n):
	a.append(list(map(int, input().split())))
print(tv_remote(a, t))


(n, x) = map(int, input().split())
a = []
a.append([0, 1])
for i in range(n):
	a.append(list(map(int, input().split())))
count = 0
for i in range(0, len(a) - 1):
	b = a[i + 1][0] - a[i][1]
	while b >= x:
		a[i][1] += x
		b = a[i + 1][0] - a[i][1]
	count += a[i + 1][1] - a[i][1] + 1
	a[i + 1][1] += 1
print(count)


import sys
nx = list(map(int, sys.stdin.readline().split()))
n = nx[0]
x = nx[1]
lr = [[0, 0]]
for i in range(0, n):
	g = list(map(int, sys.stdin.readline().split()))
	lr = lr + [[g[0], g[1]]]
r = 0
gg = 0
for j in lr:
	while gg + x < j[0]:
		gg = gg + x
	r = r + j[1] - gg
	gg = j[1]
print(r)


vals = [int(n) for n in input().split()]
n = vals[0]
x = vals[1]
start = 1
result = 0
for _ in range(n):
	moment = [int(n) for n in input().split()]
	result += (moment[0] - start) % x + moment[1] - moment[0] + 1
	start = moment[1] + 1
print(result)


(n, x) = map(int, input().split())
totalMinutes = 0
currentMinute = 1
for i in range(0, n):
	(li, ri) = map(int, input().split())
	currentMinute += (li - currentMinute) // x * x
	totalMinutes += ri - currentMinute + 1
	currentMinute = ri + 1
print(totalMinutes)


def main():
	(n, x) = map(int, input().split())
	current = 1
	acum = 0
	for i in range(n):
		(l, r) = map(int, input().split())
		dif = l - current
		div = int(dif / x)
		minu = current + div * x
		acum += r - minu + 1
		current = r + 1
	print(acum)
main()


(n, x) = map(int, input().split())
p = [1]
d = []
e = []
for i in range(n):
	(l, r) = map(int, input().split())
	e.append((l - p[-1]) % x)
	d.append(r - l + 1)
	p.append(r + 1)
print(sum(d) + sum(e))


(n, x) = map(int, input().split())
nach = 1
summa = 0
for i in range(n):
	(l, r) = map(int, input().split())
	while nach + x <= l:
		nach += x
	while nach + 1 <= r + 1:
		nach += 1
		summa += 1
print(summa)


(n, x) = map(int, input().split())
t = 1
final = 0
for i in range(n):
	(l, r) = map(int, input().split())
	final += (l - t) % x + (r - l) + 1
	t = r + 1
print(final)


moments = [[0, 0]]
gaps = []
skipped_minutes = 0
(num_moments, x) = input().split(' ')
for i in range(int(num_moments)):
	moments.append(input().split(' '))
for i in range(len(moments) - 1):
	gaps.append(int(moments[i + 1][0]) - int(moments[i][1]) - 1)
for i in gaps:
	skipped_minutes += i // int(x)
print(int(moments[-1][1]) - skipped_minutes * int(x))


z = input()
z = z.split()
n = int(z[0])
x = int(z[1])
lst = []
fm = 1
for i in range(n):
	y = input()
	y = y.split()
	temp = [int(y[0]), int(y[1])]
	lst.append(temp)
ans = 0
count = 0
for i in lst:
	while fm + x <= i[0]:
		fm = fm + x
	if fm <= i[0]:
		while fm <= i[1]:
			count += 1
			fm += 1
print(count)


(n, x) = map(int, input().split())
(k, h) = (0, 1)
for i in range(n):
	(l, r) = map(int, input().split())
	while h <= r:
		if h + x <= l:
			h += x
		else:
			k += r - h + 1
			h = r + 1
print(k)


import sys
import math
from sys import stdin, stdout

def sieve(N):
	primeNumbers = [True] * (N + 1)
	primeNumbers[0] = False
	primeNumbers[1] = False
	i = 2
	while i * i <= N:
		j = i
		if primeNumbers[j]:
			while j * i <= N:
				primeNumbers[j * i] = False
				j += 1
		i += 1
	return primeNumbers

def getPrime(N):
	primes = sieve(N)
	result = []
	for i in range(len(primes)):
		if primes[i]:
			result.append(i)
	return result

def factor(N):
	factors = []
	i = 1
	while i * i <= N:
		if N % i == 0:
			factors.append(i)
			if i != N // i:
				factors.append(N // i)
		i += 1
	return sorted(factors)

def reduceB(b):
	mod = 0
	for i in range(0, len(b)):
		mod = (mod * 10 + ord(b[i])) % 1000000007
	return mod

def gcd(a, b):
	if a < b:
		return gcd(b, a)
	if b == 0:
		return a
	return gcd(b, a % b)

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(input())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def main():
	(n, x) = get_ints_in_variables()
	start = 1
	minutes = 0
	for i in range(0, n):
		(l, r) = get_ints_in_variables()
		while l >= start:
			if l == start:
				minutes += r - l + 1
				start = r + 1
				break
			elif start + x <= l:
				start += x
			elif start + 1 != l:
				start += 1
				minutes += 1
			else:
				start += 1
				minutes += 1
	print(minutes)
main()


n = input()
n = n.split()
for i in range(len(n)):
	n[i] = int(n[i])
x = n[1]
n = n[0]
mylist = []
for i in range(n):
	s = input()
	s = s.split()
	for j in range(len(s)):
		s[j] = int(s[j])
	mylist.append(s)
t = 1
time = 0
for char in mylist:
	while t + x <= char[0]:
		t = t + x
	time = time + char[1] + 1 - t
	t = char[1] + 1
print(time)


(n, x) = [int(i) for i in input().split()]
t = 0
k = 1
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	t += (a - k) % x + b - a + 1
	k = b + 1
print(t)


def movie():
	(n, x) = [int(i) for i in input().split()]
	ans = 0
	last = 0
	for i in range(n):
		(start, end) = [int(p) for p in input().split()]
		ans += (start - last - 1) % x
		ans += end - start + 1
		last = end
	print(ans)
	return
movie()


(n, x) = map(int, input().split())
li = []
min_c = 0
t = 1
while n > 0:
	(l, r) = map(int, input().split())
	while t + x <= l:
		t = t + x
	min_c += r + 1 - t
	t = r + 1
	n -= 1
print(min_c)


(n, x) = [int(_) for _ in input().split()]
t = 1
c = 0
for m in range(n):
	(l, r) = [int(_) for _ in input().split()]
	while t <= l:
		t += x
	t -= x
	c += r - t + 1
	t = r + 1
print(c)


(n, x) = map(int, input().split())
p = 1
ans = 0
for _ in range(n):
	(l, r) = map(int, input().split())
	while p + x <= l:
		p += x
	ans += r - p + 1
	p = r + 1
print(ans)


(n, x) = [int(i) for i in input().split(' ')]
current = 1
sum = 0
for i in range(n):
	(l, r) = [int(i) for i in input().split(' ')]
	while l >= current + x:
		current += x
	sum += r - current + 1
	current = r + 1
print(sum)


(n, x) = map(int, input().split())
data = []
result = 0
for _ in range(n):
	data.append(list(map(int, input().split())))
for i in range(n):
	if i == 0:
		result += (data[i][0] - 1) % x
	else:
		result += (data[i][0] - data[i - 1][1] - 1) % x
	result += data[i][1] - data[i][0] + 1
print(result)


a1 = input().split()
a = list(a1)
k = 0
c = 1
for i in range(int(a[0])):
	b1 = input().split()
	b = list(b1)
	k += (int(b[0]) - c) % int(a[1])
	k += int(b[1]) - int(b[0]) + 1
	c = int(b[1]) + 1
print(k)


(n, x) = map(int, input().split())
pos = 1
total_time = 0
for i in range(n):
	y = list(map(int, input().split()))
	total_time += y[1] - y[0] + 1
	total_time += (y[0] - pos) % x
	pos = y[1] + 1
print(total_time)


import sys
(n, x) = (int(x) for x in input().split(' '))
cur = 1
count = 0
for i in range(0, n):
	(l, r) = (int(x) for x in input().split(' '))
	while cur + x <= l:
		cur += x
	count += r - cur + 1
	cur = r + 1
print(count)


(n, x) = map(int, input().split())
data = []
for _ in range(n):
	data.append(list(map(int, input().split())))
result = 0
prev = 1
for d in data:
	result += (d[0] - prev) % x
	result += d[1] - d[0] + 1
	prev = d[1] + 1
print(result)


(n, x) = list(map(int, input().split()))
left = 1
total = 0
for _ in range(n):
	(l, r) = list(map(int, input().split()))
	total += (l - left) % x + (r - l + 1)
	left = r + 1
print(total)


(n, x) = list(map(int, input().split()))
curr = 1
t = 0
for _ in range(n):
	(start, end) = list(map(int, input().split()))
	if curr == start:
		t += end - start + 1
		curr = end + 1
	else:
		while curr + x <= start:
			curr += x
		t += end - curr + 1
		curr = end + 1
print(t)


import math
(n, x) = [int(x) for x in input().split()]
times = []
cont = 0
lastr = 1
for t in range(0, n):
	(l, r) = [int(x) for x in input().split()]
	times.append(l)
	skips = int((l - lastr) % x)
	cont += r - l + 1 + skips
	lastr = r + 1
print(cont)


(n, x) = map(int, input().split())
previous = 1
total_minutes = 0
diff = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	diff += b - a + 1
	total_minutes += divmod(a - previous, x)[1]
	previous = b + 1
print(total_minutes + diff)


def main():
	(n, x) = map(int, input().split())
	watched = 0
	current = 1
	for _ in range(n):
		(l, r) = map(int, input().split())
		watched += r - (l - (l - current) % x) + 1
		current = r + 1
	print(watched)
main()


(n, k) = map(int, input().split())
(moments, minutes) = (0, 0)
for i in range(n):
	(li, ri) = map(int, input().split())
	while moments < li - k:
		moments += k
	minutes += ri - moments
	moments = ri
print(minutes)


import sys
(mod, MOD) = (1000000007, 998244353)
from math import ceil, floor, log, sqrt, factorial, pi, gcd

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
(n, x) = get_ints()
Arr = []
for i in range(n):
	(a, b) = get_ints()
	Arr.append((a, b))
ans = 0
for i in range(n - 1, -1, -1):
	ans += Arr[i][1] - Arr[i][0] + 1
	if i != 0:
		diff = Arr[i][0] - Arr[i - 1][1]
	else:
		diff = Arr[i][0]
	if diff % x == 0:
		ans += x - 1
	else:
		ans += diff % x - 1
print(ans)


(n, x) = map(int, input().split())
start = 1
watched = 0
for i in range(n):
	(l, r) = map(int, input().split())
	watched += r + 1 - l + l - (start + x * ((l - start) // x))
	start = r + 1
print(watched)


(n, x) = map(int, input().split())
p = 1
t = 0
i = 0
while i < n:
	(l, r) = map(int, input().split())
	z = (l - p) // x * x
	t = t + (r - (p + z) + 1)
	i = i + 1
	p = r + 1
print(t)


(n, x) = map(int, input().split())
watch = 0
now = 1
for i in range(n):
	(s, e) = map(int, input().split())
	watch += (s - now) % x + (e - s) + 1
	now = e + 1
print(watch)


(n, k) = map(int, input().strip().split(' '))
l = []
for i in range(n):
	m = list(map(int, input().strip().split(' ')))
	l.append(m)
i = 1
j = 0
count = 0
while j < n:
	if i + k <= l[j][0]:
		i += k
	else:
		count = count + l[j][1] - i + 1
		i = l[j][1] + 1
		j += 1
print(count)


from itertools import *

def main(n, x, l, r):
	s = 0
	curr = 1
	for i in range(n):
		skip = (l[i] - curr) // x
		s += r[i] - curr - skip * x + 1
		curr = r[i] + 1
	print(s)

def main_input(info=0):
	(n, x) = map(int, input().split())
	(l, r) = (list(range(n)), list(range(n)))
	for i in range(n):
		(l[i], r[i]) = map(int, input().split())
	main(n, x, l, r)
main_input()


def calc(n, x, bms):
	curr = 1
	bms.sort()
	ans = 0
	for i in range(n):
		if bms[i][0] > curr:
			x1 = 0
			if bms[i][0] - curr >= x:
				x1 = (bms[i][0] - curr) % x
			else:
				x1 = bms[i][0] - curr
			ans += x1
		ans += bms[i][1] - bms[i][0] + 1
		curr = bms[i][1] + 1
	print(ans)
bms = []
(n, x) = list(map(int, input().split()))
for i in range(n):
	(l, r) = list(map(int, input().split()))
	bms.append([l, r])
calc(n, x, bms)


def main():
	(n, x) = list(map(int, input().split()))
	time = 0
	for _ in range(n):
		(l, r) = list(map(int, input().split()))
		if _ == 0:
			curr = 1
		time += (l - curr) % x + (r - l + 1)
		curr = r + 1
	print(time)

def __starting_point():
	main()
__starting_point()


(n, x) = map(int, input().split())
intervals = []
for i in range(n):
	intervals.append(list(map(int, input().split())))
elapsed = 0
result = 0
for interval in intervals:
	while elapsed + x < interval[0]:
		elapsed += x
	result += interval[1] - elapsed
	elapsed = interval[1]
print(result)


[n, x] = map(int, input().split(' '))
r = 0
t = 1
for i in range(n):
	[a, b] = map(int, input().split(' '))
	while t + x <= a:
		t += x
	r += b - t + 1
	t = b + 1
print(r)


(N, X) = map(int, input().split())
now = 1
ans = 0
for i in range(N):
	(l, r) = map(int, input().split())
	ans += (l - now) % X
	ans += r - l + 1
	now = r + 1
print(ans)


(n, x) = map(int, input().split())
l1 = []
t = 1
minute = 0
for i in range(n):
	(l, r) = map(int, input().split())
	while t <= r:
		if t + x <= l:
			t += x
		else:
			minute += 1
			t += 1
print(minute)


(n, x) = map(int, input().split())
ans = 0
l = [0] * (n + 2)
l[0] = 0
r = l.copy()
for i in range(1, n + 1):
	(l[i], r[i]) = map(int, input().split())
	ans += r[i] - l[i] + 1
	r[i - 1] += 1
	while r[i - 1] + x <= l[i]:
		r[i - 1] += x
	ans += l[i] - r[i - 1]
print(ans)


(n, x) = input().split()
n = int(n)
x = int(x)
start = []
end = []
for i in range(n):
	(s, e) = input().split()
	start.append(int(s))
	end.append(int(e))
minutes = 0
temp = 1
for it in range(n):
	temp += abs(start[it] - temp) // x * x - 1
	minutes += end[it] - temp
	temp = end[it] + 1
print(minutes)


import sys
nums = sys.stdin.readline().split(' ')
n = int(nums[0])
x = int(nums[1])
current = 1
total = 0
for i in range(n):
	next = sys.stdin.readline().split(' ')
	next_l = int(next[0])
	next_r = int(next[1])
	total += next_r - next_l + 1 + (next_l - current) % x
	current = (next_r + 1) % x
print(total)


(n, x) = map(int, input().split())
k = 0
v = 1
for i in range(n):
	(a, b) = map(int, input().split())
	while v + x <= a:
		v = v + x
	k = k + abs(v - b) + 1
	v = b + 1
print(k)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	(n, x) = LI()
	a = sorted([LI() for _ in range(n)])
	t = 1
	r = 0
	for (b, c) in a:
		if b >= t + x:
			t += (b - t) // x * x
		r += c - t + 1
		t = c + 1
	return r
print(main())


(n, x) = map(int, input().split())
l = []
for _ in range(n):
	(s, t) = map(int, input().split())
	l.append((s, t))
l.sort()
ans = 0
time = 1
for (s, t) in l:
	if s > time:
		time += (s - time) // x * x
	ans += t + 1 - time
	time = t + 1
print(ans)


(n, x) = map(int, input().split())
current = 1
ans = 0
for i in range(n):
	(left, right) = map(int, input().split())
	current += (left - current) // x * x
	ans += right - current + 1
	current = right + 1
print(ans)


(n, x) = map(int, input().split())
array = []
a = 0
b = 0
c = []
for i in range(n):
	array.append(list(map(int, input().split())))
for i in range(n):
	for j in range(1, 10 ** 5):
		if array[i][0] - b > x * j:
			a += 1
		else:
			break
	b = array[i][1]
print(array[n - 1][1] - a * x)


def main_function():
	(n, x) = [int(i) for i in input().split(' ')]
	current_minute = 1
	watched_minutes = 0
	best_moments = [[int(i) for i in input().split(' ')] for i in range(n)]
	for i in range(len(best_moments)):
		watched_minutes += best_moments[i][1] + 1 - best_moments[i][0]
		watched_minutes += (best_moments[i][0] - current_minute) % x
		current_minute = best_moments[i][1] + 1
	print(watched_minutes)
main_function()


from math import *
import sys
from sys import *

def gI():
	return map(int, sys.stdin.readline().strip().split())

def gL():
	return list(map(int, sys.stdin.readline().strip().split()))
t = stdin.readline
p = stdout.write
(n, x) = gI()
(f, cnt) = (1, 0)
for _ in range(n):
	(l, r) = gI()
	while f <= r:
		if f + x <= l:
			f += x
		else:
			cnt += 1
			f += 1
	f = r + 1
p(str(cnt))


import sys
(n, x) = map(int, sys.stdin.readline().split())
ans = 0
cur = 1
for i in range(n):
	(l, r) = map(int, sys.stdin.readline().split())
	ans += (l - cur) % x + (r - l + 1)
	cur = r + 1
print(ans)


p = 1
i = 0
ans = 0
a = input()
stuff = a.split()
n = int(stuff[0])
x = int(stuff[1])
while i < n:
	pr = input()
	lr = pr.split()
	l = int(lr[0])
	r = int(lr[1])
	while True:
		p = p + x
		if p > l:
			p = p - x
			ans = ans + (r - p) + 1
			p = r + 1
			break
	i = i + 1
print(ans)


([n, x], cur, ans) = (list(map(int, input().split())), 1, 0)
for i in range(n):
	(l, r) = list(map(int, input().split()))
	cur += (l - cur) // x * x
	ans += r - cur + 1
	cur = r + 1
print(ans)


import sys
from math import *
input = sys.stdin.readline
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : input().strip().split()
ls = lambda : list(input().strip())
from collections import defaultdict as df
import heapq
infinite = float('inf')
(n, x) = RI()
s = 1
ans = 0
for i in range(n):
	(l, r) = RI()
	while s + x <= l:
		s += x
	if s != l:
		ans += l - s
	ans += r - l + 1
	s = r + 1
print(ans)


(n, x) = map(int, input().split())
y = 1
total = 0
for i in range(n):
	(a, b) = map(int, input().split())
	start = (a - y) % x
	total = total + (start + (b - a) + 1)
	y = b + 1
print(total)


def main():
	(n, x) = map(int, input().split())
	t = 1
	ans = 0
	for i in range(n):
		(l, r) = map(int, input().split())
		while t + x <= l:
			t += x
		ans += r - t + 1
		t = r + 1
	print(ans)
main()


(X, Watched, Last) = (list(map(int, input().split())), 0, 1)
for i in range(X[0]):
	Moment = list(map(int, input().split()))
	Press = (Moment[0] - Last) // X[1]
	Last = Last + Press * X[1]
	Watched += Moment[1] - Last + 1
	Last = Moment[1] + 1
print(Watched)


from sys import stdin, stdout
nx = list(map(int, stdin.readline().strip().split()))
n = nx[0]
x = nx[1]
movie_lst = []
t_mins = 0
for i in range(n):
	movie = list(map(int, stdin.readline().strip().split()))
	if i == 0:
		t_mins += (movie[0] - 1) % x
	t_mins += movie[1] - movie[0] + 1
	movie_lst.append(movie)
for i in range(n - 1):
	mins = (movie_lst[i + 1][0] - movie_lst[i][1] - 1) % x
	t_mins += mins
stdout.write(str(t_mins))


(n, x) = map(int, input().split())
foo = 1
ans = 0
for i in range(n):
	(l, r) = map(int, input().split())
	y = l - foo
	ans += y % x + r - l + 1
	foo = r + 1
print(ans)


(x, y) = map(int, input().strip().split())
bm = []
for i in range(x):
	bm.append(list(map(int, input().strip().split())))
bm.sort()
sum = 0
if y == 1:
	for i in bm:
		sum += i[1] - i[0] + 1
	print(sum)
else:
	sum = 0
	j = 1
	for i in bm:
		if j != y:
			while j + y <= i[0]:
				j = j + y
		sum += i[1] - j + 1
		j = i[1] + 1
	print(sum)


(n, x) = map(int, input().split())
cur = 1
ans = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	while cur + x <= a:
		cur += x
	while cur <= b:
		cur += 1
		ans += 1
print(ans)


(n, k) = map(int, input().split())
(now, reslt) = (1, 0)
for i in range(n):
	(x, y) = map(int, input().split())
	reslt += (x - now) % k + y - x + 1
	now = y + 1
print(reslt)


import sys

def main():
	(n, x) = tuple([int(y) for y in input().split()])
	c = 1
	out = 0
	for i in range(n):
		(l, r) = tuple([int(y) for y in input().split()])
		s = int((l - c) / x)
		if s > 0:
			c += x * s
		out += r - c + 1
		c = r + 1
	print(out)
main()


(n, x) = input().split(' ')
n = int(n)
x = int(x)
cur = 1
wasted = 0
for i in range(n):
	(l, r) = input().split(' ')
	l = int(l)
	r = int(r)
	while cur + x <= l:
		cur = cur + x
	if cur > l:
		cur = cur - x
	wasted = wasted + (r - cur + 1)
	cur = r + 1
print(wasted)


(n, t) = map(int, input().split())
minutes = [list(map(int, input().split())) for i in range(n)]
k = 0
winena = 1
result = 0
while k < len(minutes):
	if minutes[k][0] - winena >= t:
		winena += t
	else:
		length = minutes[k][1] - winena + 1
		winena += length
		result += length
		k += 1
print(result)


(n, x) = map(int, input().split())
nw = 0
curr = 1
for i in range(n):
	(l, r) = map(int, input().split())
	nw += (l - curr) % x + r - l + 1
	curr = r + 1
print(nw)


(n, x) = map(int, input().split())
ans = 0
t = 1
for _ in range(n):
	(i, j) = map(int, input().split())
	ans += j - i + 1 + (i - t) % x
	t = j + 1
print(ans)


from sys import stdin
file = stdin

def read_line():
	return file.readline().strip()

def read_int():
	return int(read_line())

def string_list_to_int_list(x):
	return [int(e) for e in x]
[n, x] = string_list_to_int_list(read_line().split())
time_list = []
for e in range(n):
	time_list.append(string_list_to_int_list(read_line().split()))
time_list.sort(key=lambda y: y[0])
minutes = 0
current = 1
for e in range(n):
	[start, end] = time_list[e]
	nearest = current + (start - current) // x * x
	minutes += end - nearest + 1
	current = end + 1
print(minutes)


import sys
(n, x) = [int(x) for x in sys.stdin.readline().split()]
res = 0
k = 1
for i in range(n):
	(l, r) = [int(x) for x in sys.stdin.readline().split()]
	res += r - (l - (l - k) % x) + 1
	k = r + 1
print(res)


(n, x) = input().split()
(n, x) = (int(n), int(x))
lis = []
for i in range(n):
	lis.append(input().split())
for i in range(n):
	for j in range(2):
		lis[i][j] = int(lis[i][j])
s = 0
for i in range(n):
	s += lis[i][1] - lis[i][0] + 1
s += (lis[0][0] - 1) % x
if n >= 2:
	for i in range(n - 1):
		s += (lis[i + 1][0] - lis[i][1] - 1) % x
print(s)


(n, x) = map(int, input().split())
sp = 1
c = 0
for k in range(n):
	(l, r) = map(int, input().split())
	np = sp + x
	while sp <= r:
		if l < np:
			ans = r - sp + 1
			c = c + ans
			sp = r + 1
			np = sp + x
		elif l - np < x:
			sp = np
			ans = r - sp + 1
			c = c + ans
			sp = r + 1
			np = sp + x
		else:
			sp = np
			np = sp + x
print(c)


(n, x) = map(int, input().split())
bestmoments = []
mintime = 0
time = 1
for i in range(n):
	bestmoments.append(list(map(int, input().split())))
for i in range(n):
	mintime += (bestmoments[i][0] - time) % x
	mintime += bestmoments[i][1] - bestmoments[i][0] + 1
	time = bestmoments[i][1] + 1
print(mintime)


from sys import stdin, stdout
import bisect
import math

def st():
	return list(stdin.readline())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def soe(limit):
	l = [1] * (limit + 1)
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
			n = n - 1
		else:
			a = a * a
			n = n >> 1
	return r

def solve():
	(n, x) = mp()
	c = 1
	p = 0
	for i in range(n):
		(a, b) = mp()
		while c + x <= a:
			c += x
		p += b - c + 1
		c = b + 1
	pr(p)
solve()


string = input()
numbers = string.split()
a = int(numbers[0])
b = int(numbers[1])
moments = {}
for x in range(a):
	string = input()
	numbers = string.split()
	moments[int(numbers[0])] = int(numbers[1])
left = sorted(moments)
minutes = 0
minute = 1
for x in range(a):
	p = left[x]
	q = moments[p]
	minute += (p - minute) // b * b
	minutes += q - minute + 1
	minute = q + 1
print(minutes)


(n, x) = map(int, input().split())
(cur, ans) = (1, 0)
for _ in range(n):
	(l, r) = map(int, input().split())
	skip = (l - cur) // x
	cur += skip * x
	ans += r - cur + 1
	cur = r + 1
print(ans)


(n, skip) = map(int, input().split())
c = 0
s = [[1, 1]]
for i in range(n):
	(a, b) = map(int, input().split())
	b += 1
	s.append([a, b])
for i in range(n):
	if s[i + 1][0] - s[i][1] >= skip:
		c += (s[i + 1][0] - s[i][1]) % skip
	else:
		c += s[i + 1][0] - s[i][1]
for i in range(1, n + 1):
	c += s[i][1] - s[i][0]
print(c)


(n, x) = map(int, input().split())
k = []
for i in range(n):
	y = []
	(l, r) = map(int, input().split())
	y = [l, r]
	k.append(y)
k.sort()
pos = 1
t = 0
for j in range(n):
	y = (k[j][0] - pos) // x
	pos = pos + y * x
	t = t + k[j][1] - pos + 1
	pos = k[j][1] + 1
print(t)


(n, x) = map(int, input().split())
m = 0
c = 1
for i in range(n):
	(l, r) = map(int, input().split())
	if l - c >= x:
		c = c + (l - c) // x * x
		m = m + (r - c) + 1
		c = r + 1
	else:
		m = m + (r - c) + 1
		c = c + (r - c) + 1
print(m)


(n, x) = list(map(int, input().split()))
minutes = 0
pos = 1
for i in range(n):
	(l, r) = list(map(int, input().split()))
	pos += (l - pos) // x * x
	minutes += r - pos + 1
	pos = r + 1
print(minutes)


(n, m) = map(int, input().split())
s = 0
a = []
for i in range(n):
	(b, f) = map(int, input().split())
	a.append([b, f])
t = 1
for i in range(len(a)):
	if a[i][0] - t >= m:
		s = s + (a[i][0] - t) % m
	else:
		s = s + (a[i][0] - t)
	s = s + (a[i][1] - a[i][0] + 1)
	t = a[i][1] + 1
print(s)


class Player:

	def __init__(self, x):
		self.skip_val = x
		self.cur_pos = 1
		self.watched = 0

	def play_minute(self):
		self.cur_pos += 1
		self.watched += 1

	def skip(self):
		self.cur_pos += self.skip_val

def main():
	(n, x) = map(int, input().split())
	p = Player(x)
	scenes = []
	for i in range(n):
		tup = tuple(map(int, input().split()))
		scenes.append(tup)
	for scene in scenes:
		while scene[0] - p.cur_pos >= p.skip_val:
			p.skip()
		while p.cur_pos <= scene[1]:
			p.play_minute()
	print(p.watched)
main()


(n, x) = map(int, input().split())
segs = []
for i in range(n):
	(l, r) = map(int, input().split())
	segs += [[l, r]]
segs.sort()
curS = 0
curM = 1
ans = 0
while curS < len(segs):
	if curM < segs[curS][0]:
		if (segs[curS][0] - curM) % x != 0:
			ans += segs[curS][0] - (curM + x * ((segs[curS][0] - curM) // x))
		curM = segs[curS][0]
		if curM > segs[curS][1]:
			curS += 1
	else:
		ans += segs[curS][1] - curM + 1
		curM = segs[curS][1] + 1
		curS += 1
print(ans)


def watch_movie():
	buttons = list(map(int, input().split()))
	channels = []
	output = 0
	temp = buttons[1]
	for i in range(buttons[0]):
		channels.append(list(map(int, input().split())))
		output += channels[i][1] + 1 - channels[i][0]
		channels[i][0] = channels[i][0] - (temp + 1)
		channels[i][0] = channels[i][0] % buttons[1]
		output += channels[i][0]
		temp = channels[i][1]
	return output
A = watch_movie()
print(A)


(n, x) = map(int, input().split())
(total, start) = (0, 0)
for i in range(n):
	(a, b) = map(int, input().split())
	while start + x < a:
		start += x
	total += b - start
	start = b
print(total)


(n, x) = map(int, input().split())
cur = 1
s = 0
for i in range(n):
	(l, r) = map(int, input().split())
	d = (l - cur) % x
	s = s + d + r - l + 1
	cur = r + 1
print(s)


(n, k) = map(int, input().split())
t = 0
i = 1
for _ in range(n):
	(l, r) = map(int, input().split())
	(d, m) = divmod(l - i, k)
	t += m + (r - l) + 1
	i = r + 1
print(t)


(n, x) = map(int, input().split())
pos = 1
ans = 0
for _ in range(n):
	(l, r) = map(int, input().split())
	ans += (l - pos) % x
	ans += r - l + 1
	pos = r + 1
print(ans)


(n, x) = map(int, input().split())
minutes = 1
for i in range(n):
	(l, r) = map(int, input().split())
	diff = l - minutes
	minutes = minutes + diff % x
	minutes = minutes + (r - l + 1)
print(minutes - 1)


(part, x) = input().split()
part = int(part)
x = int(x)
i = 0
m = 0
temp1 = 0
temp2 = 0
while i < part:
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	for j in range(0, a, x):
		continue
	if temp2 == 0:
		for k in range(j, b, 1):
			temp1 += 1
			temp2 = b
	else:
		for k in range(temp2, a, x):
			continue
		for l in range(k, b, 1):
			temp1 += 1
			temp2 = b
	i += 1
	m += 1
	if m == part:
		print(temp1)


answer = 0
minute = 1
s = input()
(n, x) = s.split(' ')
n = int(n)
x = int(x)
for i in range(n):
	s = input()
	(l, r) = s.split(' ')
	l = int(l)
	r = int(r)
	answer = answer + r - l + 1 + (l - minute) % x
	minute = r + 1
print(answer)


(n, x) = list(map(int, input().split()))
l = []
(k, time) = (0, 0)
for i in range(n):
	l.append(list(map(int, input().split())))
i = 0
while i < l[n - 1][1]:
	(lo, hi) = l[k]
	if i + x < lo:
		i += x
		continue
	else:
		time += lo - i + (hi - lo)
		i = hi
		k += 1
		continue
print(time)


a = list(map(int, input().split()))
(n, x) = (a[0], a[1])
b = []
for i in range(n):
	c = list(map(int, input().split()))
	b.append(c)
time = abs(b[0][0] - 1) % x
for i in range(n):
	time += b[i][1] - b[i][0] + 1
	if i > 0:
		time += (b[i][0] - 1 - b[i - 1][1]) % x
print(time)


(n, x) = map(int, input().split())
i = 1
cnt = 0
for _ in range(n):
	(left, right) = map(int, input().split())
	i += (left - i) // x * x
	cnt += right - i + 1
	i = right + 1
print(cnt)


import sys
(number, skip) = map(int, sys.stdin.readline().split())
listofduration = []
for index in range(number):
	listofduration.append(list(map(int, sys.stdin.readline().split())))
listofduration.sort(key=lambda x: x[0])
pos = 1
for index in range(1, number):
	if listofduration[pos][0] <= listofduration[pos - 1][1]:
		listofduration[pos - 1][1] = listofduration[pos][0]
		listofduration.remove(listofduration[pos])
	else:
		pos += 1
position = 1
timeseen = 0
for index in range(pos):
	position = position + (listofduration[index][0] - position) // skip * skip
	timeseen = timeseen + listofduration[index][1] - position + 1
	position = listofduration[index][1] + 1
print(timeseen)


def main():
	rdl = list(map(int, input().split()))
	(n, x) = rdl
	minutes = 0
	cur = 1
	bests = []
	for i in range(n):
		bests.append(list(map(int, input().split())))
	for i in bests:
		j = 1
		while cur + x * j <= i[0]:
			j += 1
		cur += x * (j - 1)
		minutes += i[1] - cur + 1
		cur += i[1] - cur + 1
	print(minutes)
main()


def main():
	(n, x) = map(int, input().split())
	(tot, t) = (0, 1)
	for _ in range(n):
		(l, r) = map(int, input().split())
		t1 = r
		r -= t - 1
		l -= t
		tot += r - l + l % x
		t = t1 + 1
	print(tot)
main()


(n, d) = map(int, input().split())
ans = 0
cnt = 1
for i in range(n):
	(x, y) = map(int, input().split())
	ans += y - x + 1
	while cnt + d <= x:
		cnt += d
	ans += x - cnt
	cnt = y + 1
print(ans)


(n, p) = map(int, input().split())
now = 1
time = 0
for q in range(n):
	(t, i) = map(int, input().split())
	time += (t - now) % p + (i - t + 1)
	now = i + 1
print(time)


(n, x) = map(int, input().split())
last = 1
res = 0
for i in range(n):
	(a, b) = map(int, input().split())
	while last + x <= a:
		last += x
	res += b - last + 1
	last = b + 1
print(res)


(n, m) = map(int, input().split())
t = 1
s = 0
for i in range(n):
	(x, y) = map(int, input().split())
	s += (x - t) % m + (y - x + 1)
	t = y + 1
print(s)


(n, x) = input().split(' ')
n = int(n)
x = int(x)
moments = []
current = 1
watched = 0
for i in range(n):
	(i, j) = input().split(' ')
	i = int(i)
	j = int(j)
	moments.append(i)
	moments.append(j)
for i in range(0, len(moments), 2):
	t = (moments[i] - current) // x
	current += x * t
	watched += moments[i + 1] - current + 1
	current = moments[i + 1] + 1
print(watched)


(n, x) = [int(y) for y in input().split()]
L = list()
R = list()
for i in range(n):
	(l, r) = [int(y) for y in input().split()]
	L.append(l)
	R.append(r)
cur_min = 1
min_watch = 0
for i in range(n):
	min_start = (L[i] - cur_min) // x * x
	min_start += cur_min
	cur_min = R[i] + 1
	min_watch += R[i] - min_start + 1
print(min_watch)


j = str()
(n, x) = [int(j) for j in input('').split(' ')]
minut = 1
l = r = int()
tot = 0
for i in range(n):
	(l, r) = [int(j) for j in input('').split(' ')]
	tot += (l - minut) % x + (r - l + 1)
	minut = r + 1
print(tot)


(n, x) = [int(x) for x in input('').split()]
sum = 0
prev = 1
for i in range(n):
	(a, b) = [int(x) for x in input('').split()]
	sum = sum + (a - prev) % x + (b + 1 - a)
	prev = b + 1
print(sum)


(n, x) = map(int, input().split())
res = 0
ri = 1
for i in range(n):
	(l, r) = map(int, input().split())
	l = l - ri
	r = r - ri
	ri = ri + r + 1
	res = res + l % x + (r - l + 1)
print(res)


def main():
	(n, x) = [int(c) for c in input().split()]
	time = res = 0
	for i in range(n):
		(l, r) = [int(c) - 1 for c in input().split()]
		time += x * ((l - time) // x)
		tmp = r - time + 1
		time += tmp
		res += tmp
	print(res)
main()


def main():
	(n, x) = map(int, input().split())
	now = 1
	count = 0
	for i in range(n):
		(l, r) = map(int, input().split())
		count += (l - now) % x + (r - l) + 1
		now = r + 1
	print(count)
main()


(n, x) = map(int, input().split())
arr = []
start = 1
ans = 0
for i in range(n):
	(a, b) = map(int, input().split())
	if (a - start) // x > 0:
		start += (a - start) // x * x
	ans += b - start + 1
	start = b + 1
print(ans)


def main():
	(n, x) = map(int, input().split(' '))
	l = []
	r = []
	for i in range(n):
		(li, ri) = map(int, input().split(' '))
		l.append(li)
		r.append(ri)
	cur = 1
	mins = 0
	i = 0
	while i < n:
		li = l[i]
		ri = r[i]
		if li == cur:
			cur += ri - li + 1
			mins += ri - li + 1
			i += 1
		elif li >= cur + x:
			cur += x
		else:
			mins += 1
			cur += 1
	return mins
print(main())


(n, x) = map(int, input().split())
minutes = 0
current = 1
for _ in range(n):
	(l, r) = map(int, input().split())
	skip = (l - current) // x * x
	current += skip
	minutes += r - current + 1
	current = r + 1
print(minutes)


(n, x) = map(int, input().split())
l = [[0, 1]]
ans = 0
p = n
while n:
	(a, b) = map(int, input().split())
	if n != 1:
		l.append([a, b + 1])
		ans = ans + b - a + 1
	else:
		l.append([a, b])
		ans = ans + b - a + 1
	n = n - 1
for i in range(p):
	ans = ans + (l[i + 1][0] - l[i][1]) % x
print(ans)


import datetime
(n, k) = map(int, input().split())
current = 0
watching = 0
for i in range(n):
	(x, y) = map(int, input().split())
	watching += y - x + 1
	watching += (x - current - 1) % k
	current = y
print(watching)


from math import *
(n, x) = map(int, input().split())
(c, t) = (1, 0)
for _ in range(n):
	(s, e) = map(int, input().split())
	c += floor((s - c) / x) * x
	t += floor((s - c) / x) * x
	t += e - c + 1
	c = e + 1
print(t)


data = input().split()
(N, X) = (int(data[0]), int(data[1]))
best = [None] * N
for i in range(N):
	best[i] = list(map(int, input().split()))
minuts = 1
counter = 0
for i in range(N):
	while minuts + X <= best[i][0]:
		minuts += X
	if minuts <= best[i][0]:
		while minuts <= best[i][1]:
			counter += 1
			minuts += 1
print(counter)


(n, x) = (int(i) for i in input().split())
moments = []
for i in range(n):
	moments.append([int(i) for i in input().split()])
now = 1
i = 0
ans = 0
while i < n:
	if now == moments[i][0]:
		ans += moments[i][1] - moments[i][0] + 1
		now = moments[i][1] + 1
		i += 1
		continue
	if moments[i][0] - now >= x:
		now += x
		continue
	ans += moments[i][0] - now
	now = moments[i][0]
print(ans)


(n, x) = map(int, input().split())
mincout = 0
currmin = 1
for i in range(n):
	(l, r) = map(int, input().split())
	while True:
		if currmin + x <= l:
			currmin += x
		else:
			mincout += r - currmin + 1
			currmin = r + 1
			break
print(mincout)


(n, x) = map(int, input().split())
count = 0
f = 1
for i in range(n):
	(l, r) = map(int, input().split())
	count += (l - f) % x + r - l + 1
	f = r + 1
print(count)


(n, x) = list(map(int, input().split(' ')))
c = 1
w = 0
for i in range(n):
	(s, e) = list(map(int, input().split(' ')))
	d = (s - c) % x
	w += d + 1 + e - s
	c = e + 1
print(w)


r = lambda : map(int, input().split())
start = 1
seen = 0
(n, skip) = r()
for x in range(n):
	(s, e) = r()
	if s < start + skip:
		seen += e - start + 1
	else:
		start += skip * ((s - start) // skip)
		seen += e - start + 1
	start = e + 1
print(seen)


(n, x) = map(int, input().split())
ans = 0
a = []
t = 0
for i in range(n):
	(l, r) = map(int, input().split())
	a += [[l, r]]
a.sort()
for elem in a:
	ans += (elem[0] - 1 - t) % x + (elem[1] - elem[0] + 1)
	t = elem[1]
print(ans)


(n, x) = [int(item) for item in input().split()]
minutes = 0
init = 1
for i in range(n):
	(l, r) = [int(item) for item in input().split()]
	if init < l:
		while l - init >= x:
			init += x
		minutes += r - init + 1
		init = r + 1
	else:
		minutes += r - init + 1
		init = r + 1
print(minutes)


l = input().split()
n = int(l[0])
x = int(l[1])
curr = 1
tot = 0
for i in range(n):
	p = input().split()
	l = int(p[0])
	r = int(p[1])
	while curr + x <= l:
		curr += x
	tot += r - curr + 1
	curr = r + 1
print(tot)


a = []
a = input().split()
n = int(a[0])
x = int(a[1])
t = []
for i in range(0, n):
	t.append([int(j) for j in input().split()])
m = 0
for i in range(0, n):
	if i == 0:
		m = m + (t[i][0] - 1) % x + (t[i][1] - t[i][0]) + 1
	else:
		m = m + (t[i][0] - t[i - 1][1] - 1) % x + (t[i][1] - t[i][0]) + 1
print(m)


(n, x) = map(int, input().split())
tot = 0
prev = 1
for i in range(n):
	(l, r) = map(int, input().split())
	if (l - prev) % x == 0:
		tot += r - l + 1
	else:
		tot += r - l + 1
		temp = (l - prev) % x
		tot += temp
	prev = r + 1
print(tot)


(n, d) = input().split()
n = int(n)
d = int(d)
f = [0] * n
t = [0] * n
for i in range(n):
	(a, b) = input().split()
	f[i] = int(a)
	t[i] = int(b)
i = 1
j = 0
w = 0
while i <= t[n - 1] and j < n:
	if i + d <= f[j]:
		i += d
	else:
		w += -i + t[j] + 1
		i = t[j] + 1
		j += 1
print(w)


def solution(skip_amount, current_time):
	(left, right) = input().strip().split()
	left = int(left)
	right = int(right)
	while current_time + skip_amount <= left:
		current_time += skip_amount
	time_watched = right - current_time + 1
	return (time_watched, current_time + time_watched)
(n, x) = input().strip().split()
n = int(n)
x = int(x)
current_time = 1
net = 0
for i in range(n):
	(time_watched, new_current_time) = solution(x, current_time)
	net += time_watched
	current_time = new_current_time
print(net)


(n, x) = list(map(int, input().split()))
t = 1
m = 0
for i in range(n):
	(l, r) = list(map(int, input().split()))
	if t <= l:
		m += r - l + 1 + (l - t) % x
	t = r + 1
print(m)


import math
(n, k) = map(int, input().strip().split())
pr = 1
ans = 0
for i in range(n):
	(st, ed) = map(int, input().strip().split())
	tmp = st - pr
	tmp = pr + tmp // k * k
	ans += ed - tmp + 1
	pr = ed + 1
print(ans)


def fun(m, b, z):
	while m <= z:
		m = m + b
	return m - b
(x, y) = input().split()
(a, b) = (int(x), int(y))
(list, list1, list2) = ([], [], [])
for i in range(a):
	(x, y) = input().split()
	(c, d) = (int(x), int(y))
	list.append((c, d))
	list1.append(d - c)
for i in range(a):
	if i == 0:
		n = fun(1, b, list[i][0])
	else:
		n = fun(list[i - 1][1] + 1, b, list[i][0])
	try:
		list2.append(list[i][0] - n)
	except:
		g = 1
o = sum(list1) + sum(list2) + a
print(o)


(n, fwd) = map(int, input().split())
start = 1
duration = 0
for _ in range(n):
	(frm, to) = map(int, input().split())
	if start + fwd <= frm:
		while start + fwd <= frm:
			start += fwd
		while start != to + 1:
			start += 1
			duration += 1
	else:
		while start != to + 1:
			start += 1
			duration += 1
print(duration)


(n, k) = list(map(int, input().split()))
st = 1
tim = 0
for z in range(n):
	(a, b) = list(map(int, input().split()))
	tim += (a - st) % k + (b - a) + 1
	st = b + 1
print(tim)


import os
import sys
from io import BytesIO, IOBase
import math as M
import itertools as ITR
from collections import defaultdict as D
from collections import Counter as C
from collections import deque as Q
import threading
from functools import lru_cache, reduce
from functools import cmp_to_key as CMP
from bisect import bisect_left as BL
from bisect import bisect_right as BR
import random as R
import string
import cmath, time
enum = enumerate
start_time = time.time()
MOD = 1000000007
MA = float('inf')
MI = float('-inf')
di8 = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))
di4 = ((1, 0), (0, 1), (-1, 0), (0, -1))

def increase_stack():
	sys.setrecursionlimit(2 ** 32 // 2 - 1)
	threading.stack_size(1 << 27)

def binary(n):
	return bin(n)[2:]

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def maxfactor(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
	if q:
		return q[-1]

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(M.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	l.sort()
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def seive(n):
	a = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p = p + 1
	for p in range(2, n + 1):
		if prime[p]:
			a.append(p)
	prime[0] = prime[1] = False
	return (a, prime)

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(M.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countchar(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c

def str_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def lis(arr):
	n = len(arr)
	lis = [1] * n
	maximum = 0
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] > arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
				maximum = max(maximum, lis[i])
	return maximum

def lcm(arr):
	a = arr[0]
	val = arr[0]
	for i in range(1, len(arr)):
		gcd = gcd(a, arr[i])
		a = arr[i]
		val *= arr[i]
	return val // gcd

def ncr(n, r):
	return M.factorial(n) // (M.factorial(n - r) * M.factorial(r))

def npr(n, r):
	return M.factorial(n) // M.factorial(n - r)

def IF(c, t, f):
	return t if c else f

def YES(c):
	print(IF(c, 'YES', 'NO'))

def Yes(c):
	print(IF(c, 'Yes', 'No'))

def yes(c):
	print(IF(c, 'yes', 'no'))

def JA(a, sep=' '):
	print(sep.join(map(str, a)))

def JAA(a, s='\n', t=' '):
	return s.join((t.join(map(str, b)) for b in a))

def PS(a, s=' '):
	print(str(a), end=s)

def I():
	return int(inp())

def F():
	return float(inp())

def LI():
	return list(map(int, inp().split()))

def LF():
	return list(map(float, inp().split()))

def MATI(n):
	return [LI() for i in range(n)]

def MATS(n):
	return [list(inp()) for i in range(n)]

def IV():
	return map(int, inp().split())

def FV():
	return map(float, inp().split())

def LS():
	return list(inp())

def S():
	return inp()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
inp = lambda : sys.stdin.readline().rstrip('\r\n')
TestCases = 0

def solve():
	(n, m) = IV()
	arr = []
	for i in range(n):
		arr.append(LI())
	arr.sort(key=lambda x: x[0])
	res = 0
	p = 1
	for (x, y) in arr:
		q = (x - p) % m
		res += q
		res += y - x + 1
		p = y + 1
	print(res)

def main():
	flag = 1
	try:
		sys.stdin = open('c:/Users/Manoj Chowdary/Documents/python/CodeForces/contest-div-2/input.txt', 'r')
		sys.stdout = open('c:/Users/Manoj Chowdary/Documents/python/CodeForces/contest-div-2/output.txt', 'w')
	except:
		flag = 0
	t = 1
	if TestCases:
		t = I()
	for _ in range(1, t + 1):
		solve()
	if flag:
		print('Time: %.4f sec' % (time.time() - start_time))
		localtime = time.asctime(time.localtime(time.time()))
		print(localtime)
		sys.stdout.close()
main()


import sys
input = lambda : sys.stdin.readline().strip()
(n, x) = map(int, input().split())
cur = 1
t = 0
for i in range(n):
	(s, e) = map(int, input().split())
	while cur + x <= s:
		cur += x
	t += e - cur + 1
	cur = e + 1
print(t)


z = lambda : map(int, input().split())
(n, x) = z()
a = list()
c = 1
ans = 0
for i in range(n):
	(l, r) = z()
	ans += (l - c) % x
	ans += r - l + 1
	c = r + 1
print(ans)


from sys import stdin, stdout
input = stdin.readline
(n, x) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
ans = 0
r = 1
for i in a:
	ans += (i[0] - r) % x + i[1] - i[0] + 1
	r = i[1] + 1
stdout.write(str(ans))


(n, x) = map(int, input().split())
l = 1
s = 0
for i in range(n):
	(a, b) = map(int, input().split())
	d = a - l
	if d >= x:
		c = 0
		c += d // x
		l = l + c * x
	s += b - l + 1
	l = b + 1
print(s)


(n, s) = map(int, input().split())
left = 1
ans = 0
for i in range(n):
	(l, r) = map(int, input().split())
	ans += (l - left) % s + (r - l + 1)
	left = r + 1
print(ans)


def answer(n, x, l):
	if n == 0:
		return 0
	count = 0
	ptr = 1
	for i in range(n):
		forward = ptr + (l[i][0] - ptr) // x * x
		count += l[i][1] - forward + 1
		ptr = l[i][1] + 1
	return count
(n, x) = map(int, input().split())
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append([a, b])
print(answer(n, x, l))


(n, x) = map(int, input().split())
arr = []
for _ in range(n):
	(l, r) = map(int, input().split())
	arr.append((l, r))
res = 0
res += (arr[0][0] - 1) % x
for i in range(n - 1):
	res += arr[i][1] - arr[i][0] + 1
	res += (arr[i + 1][0] - (arr[i][1] + 1)) % x
res += arr[n - 1][1] - arr[n - 1][0] + 1
print(res)


s = list(map(int, input().split()))
t = 0
last = 1
for _ in range(s[0]):
	l = list(map(int, input().split()))
	a = (l[0] - last) // s[1] * s[1]
	t += l[1] - a - (last - 1)
	last = l[1] + 1
print(t)


(n, x) = map(int, input().split())
k = 0
ind = 1
for i in range(n):
	(l, r) = map(int, input().split())
	ind += (l - ind) // x * x
	k += r - ind + 1
	ind = r + 1
print(k)


R = lambda : map(int, input().split())
(n, x) = R()
res = 0
p = 1
for i in range(n):
	(l, r) = R()
	res += (l - p) % x + r - l + 1
	p = r + 1
print(res)


(n, x) = [int(i) for i in input().split()]
t = 1
k = 0
for i in range(n):
	(l, r) = [int(y) for y in input().split()]
	while t + x <= l:
		t += x
	k += r - t + 1
	t = r + 1
print(k)


(n, x) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
m = 0
s = 1
for i in range(len(a)):
	if (a[i][0] - s) % x == 0:
		m += a[i][1] + 1 - a[i][0]
		s = a[i][1] + 1
	else:
		m += (a[i][0] - s) % x + (a[i][1] + 1 - a[i][0])
		s = a[i][1] + 1
print(m)


r = lambda : map(int, input().split())
(a, b) = r()
t = 0
z = 0
for i in a * [0]:
	(c, d) = r()
	t += (c - z - 1) % b + (d - c + 1)
	z = d
print(t)


(n, x) = map(int, input().split())
watchtime = 0
end = 0
for i in range(n):
	(l, r) = map(int, input().split())
	watchtime += (l - end - 1) % x + (r - l + 1)
	end = r
print(watchtime)


(n, x) = map(int, input().split())
a = []
for i in range(n):
	(l, r) = map(int, input().split())
	a += [(l, r)]
a.sort()
i = 0
k = 0
while i < n:
	m = 0
	if i > 0:
		m = a[i - 1][1]
	if i == 0 or m < 0:
		m = 0
	k += (a[i][0] - 1 - m) % x + 1 + a[i][1] - a[i][0]
	i += 1
print(k)


par = input().split()
n = int(par[0])
x = int(par[1])
result = 0
current = 1
for i in range(n):
	moments = input().split()
	l = int(moments[0])
	while l - current >= x:
		current += x
	r = int(moments[1])
	while current <= r:
		result += 1
		current += 1
print(result)


(n, x) = list(map(int, input().split()))
(c, a) = (0, 0)
for i in range(n):
	(l, r) = list(map(int, input().split()))
	while c + x < l:
		c += x
	(a, c) = (a + r - c, r)
print(a)


(n, x) = map(int, input().split(' '))
p = 1
res = 0
for i in range(n):
	(a, b) = map(int, input().split(' '))
	res += (a - p) % x + b - a + 1
	p = b + 1
print(res)


(n, x) = list(map(int, input().split()))
(c, c1) = (0, 0)
for i in range(n):
	(s, l) = list(map(int, input().split()))
	while c < s - x:
		c += x
	c1 += l - c
	c = l
print(c1)


(n, c) = map(int, input().split())
time = [1]
sub = []
for i in range(n):
	(a, b) = map(int, input().split())
	b = b + 1
	x = abs(a - b)
	time.append(a)
	time.append(b)
	sub.append(x)
time = time[:-1]
res = []
for i in range(len(time) - 1):
	x = time[i + 1] - time[i]
	res.append(x)
	i = i + 1
minu = []
for i in range(len(res)):
	if i % 2 == 0:
		minu.append(res[i])
res2 = []
for i in range(len(minu)):
	y = minu[i] % c
	res2.append(y)
m = sum(res2) + sum(sub)
print(m)


(n, m) = map(int, input().split())
(res, c) = (0, 1)
for i in range(n):
	(l, r) = map(int, input().split())
	res += r - l + 1 + (l - c) % m
	c = r + 1
print(res)


n = [int(x) for x in input().split()]
t = 1
inte = n[0]
sk = n[1]
counter = 0
for i in range(inte):
	x = [int(x) for x in input().split()]
	l = x[0]
	r = x[1]
	if l - t >= sk:
		for e in range(l - t):
			t += sk
			if l - t < sk:
				counter += r + 1 - t
				t = r + 1
				break
	elif l - t < sk:
		counter += r + 1 - t
		t = r + 1
print(counter)


import sys, os
from math import sqrt, gcd, ceil, log, floor, sin, pi
from math import factorial as fact
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
from itertools import permutations
input = sys.stdin.readline
read = lambda : list(map(int, input().strip().split()))
MOD = 10 ** 9 + 7

def main():
	(n, x) = read()
	ans = 0
	(a, b) = read()
	ans += (a - 1) % x + (b - a + 1)
	for i in range(n - 1):
		(m, n) = read()
		ans += (m - b - 1) % x + (n - m + 1)
		(a, b) = (m, n)
	print(ans)
main()


(n, x) = map(int, input().split())
(prev, val) = (1, 0)
for i in range(n):
	(l, r) = map(int, input().split())
	val += (l - prev) % x + (r - l + 1)
	prev = r + 1
print(val)


(n, x) = [int(x) for x in input().split()]
currentMin = 1
res = 0
for i in range(n):
	(l, r) = [int(x) for x in input().split()]
	res += (l - currentMin) % x + (r - l + 1)
	currentMin = r + 1
print(res)


(n, m) = [int(x) for x in input().split()]
x = [[int(x) for x in input().split()] for _ in range(n)]
a = 0
end = 0
for i in range(n):
	a += max((x[i][0] - end - 1) // m, 0) * m
	end = x[i][1]
print(x[-1][1] - a)


r = list(map(int, input().split(' ')))
n = r[0]
x = r[1]
m = []
for I in range(n):
	l = list(map(int, input().split(' ')))
	m.append(l)
t = 0
a = 1
for i in range(n):
	b = (m[i][0] - a) % x
	t = t + (m[i][1] - m[i][0]) + 1 + b
	a = 1 + m[i][1]
print(t)


(n, x) = map(int, input().split())
l = []
r = []
count = 0
for i in range(0, n):
	(lm, rm) = map(int, input().split())
	l.append(lm)
	r.append(rm)
m = 1
for i in range(0, n):
	while m + x <= l[i]:
		m = m + x
	count = count + r[i] - m + 1
	m = r[i] + 1
print(count)


I = lambda : map(int, input().split())
(n, x) = I()
i = 1
a = 0
for _ in range(n):
	(l, r) = I()
	(a, i) = (a + (l - i) % x + r + 1 - l, r + 1)
print(a)


(n, x) = (int(_) for _ in input().split(' '))
current = 1
minutes_watched = 0
for i in range(n):
	(li, ri) = (int(_) for _ in input().split(' '))
	while current + x <= li:
		current += x
	minutes_watched += ri - current + 1
	current = ri + 1
print(minutes_watched)


(n, x) = [int(i) for i in input().split()]
ranges = []
for i in range(n):
	(l, r) = [int(i) for i in input().split()]
	ranges.append([l, r])
ranges = sorted(ranges)
(time, listened) = (0, 0)
for (l, r) in ranges:
	skips = max(0, (l - time - 1) // x)
	time += skips * x
	if r > time:
		listened += r - time
		time = r
print(listened)


params = input().split(' ')
store = []
for i in range(int(params[0])):
	store.append(input().split(' '))
start = 1
watch = 0
for x in store:
	difference = int(x[0]) - start
	if difference == 0:
		watch += int(x[1]) - int(x[0]) + 1
	elif difference >= int(params[1]):
		requiredVal = difference // int(params[1])
		val = requiredVal * int(params[1])
		start += val
		watch += int(x[1]) - start + 1
	else:
		watch += int(x[1]) - start + 1
	start = int(x[1]) + 1
print(watch)


listx = []
(x, y) = map(int, input().split(' '))
for i in range(x):
	(a, b) = map(int, input().split(' '))
	listx.append([a, b])
time = 1
curr = 0
mins = 0
while curr < x:
	while time + y <= listx[curr][0]:
		time += y
	mins += listx[curr][1] - time + 1
	time = listx[curr][1]
	curr += 1
	time += 1
print(mins)


(n, x) = list(map(int, input().split()))
r = 0
l = 0
for i in range(n):
	(a, b) = list(map(int, input().split()))
	r = r + (a - l - 1) % x + b - a + 1
	l = b
print(r)