Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r). 

He has a set that consists of n integer segments: [l_1; r_1], [l_2; r_2], ..., [l_{n}; r_{n}]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].

The value of a set of segments that consists of n segments [l_1; r_1], [l_2; r_2], ..., [l_{n}; r_{n}] is the number of integers x, such that there is integer j, for which the following inequality holds, l_{j} ≤ x ≤ r_{j}.

Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.


-----Input-----

The first line contains two integers n and k (1 ≤ n, k ≤ 10^5). Each of the following n lines contain a segment as a pair of integers l_{i} and r_{i} ( - 10^5 ≤ l_{i} ≤ r_{i} ≤ 10^5), separated by a space.

It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds, min(r_{i}, r_{j}) < max(l_{i}, l_{j}).


-----Output-----

In a single line print a single integer — the answer to the problem.


-----Examples-----
Input
2 3
1 2
3 4

Output
2

Input
3 7
1 2
3 3
4 7

Output
0

Source: codeforces | Difficulty: EASY
Tags: Implementation

Complete search


Solutions:
(n, k) = input().split()
L = {''}
for i in range(int(n)):
	(m, f) = input().split()
	for i in range(int(m), int(f) + 1):
		L |= {i}
L.remove('')
x = len(L)
print((int(k) - x % int(k)) % int(k))


import sys

def covered_segments(nsegments):
	covered_points = 0
	for i in range(nsegments):
		(x, y) = map(int, sys.stdin.readline().split())
		covered_points += y - x + 1
	return covered_points

def result(covered_points, k):
	if covered_points % k == 0:
		return 0
	elif k < covered_points:
		return k - covered_points % k
	else:
		return k - covered_points
(n, k) = map(int, sys.stdin.readline().split())
covered_points = covered_segments(n)
print(result(covered_points, k))


from math import factorial
(n, k) = [int(x) for x in input().split(' ')]
(answer, s) = (0, 0)
(min_l, prev_l) = (9999999999999, -10)
s_left = 0
for i in range(n):
	(l, r) = [int(x) for x in input().split(' ')]
	if l < min_l:
		min_l = l
	s += r - l + 1
	if prev_l != -10:
		s_left += l - prev_l - 1
	prev_l = r
s_left += min_l - 1
if s % k == 0:
	print(0)
else:
	(s1, s2, c1) = (s, s, 0)
	while s1 % k != 0 and s2 % k != 0:
		s1 += 1
		if s_left > 0:
			s2 -= 1
			s_left -= 1
		c1 += 1
	print(c1)


(n, k) = map(int, input().split())
p = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	p += b - a + 1
if p % k == 0:
	print(0)
else:
	l = p
	while p % k != 0:
		p += 1
	print(p - l)


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


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


nk = input().strip().split(' ')
n = int(nk[0])
k = int(nk[1])
value = 0
for i in range(n):
	lr = input().strip().split(' ')
	value += int(lr[1]) - int(lr[0]) + 1
if value % k != 0:
	print(k - value % k)
else:
	print(0)


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


import sys

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

def iinput():
	return int(input())

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

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
(n, k) = rinput()
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
p = 0
for i in range(n):
	p += a[i][1] - a[i][0] + 1
if p % k == 0:
	print(0)
else:
	print(k - p % k)


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


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


def solve(res, n, k):
	tot = 0
	for i in res:
		tot += i[1] - i[0] + 1
	count = 0
	while tot % k != 0:
		tot += 1
		count += 1
	return count

def main():
	(n, k) = list(map(int, input().split(' ')))
	res = []
	for _ in range(n):
		arr = list(map(int, input().split(' ')))
		res.append(arr)
	print(solve(res, n, k))
main()


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


(n, k) = map(int, input().split())
counter = 0
for i in range(n):
	(x1, x2) = map(int, input().split())
	counter += x2 - x1 + 1
if counter % k == 0:
	print(0)
else:
	m = counter % k
	print(k - m)


def main():
	(n, k) = map(int, input().split())
	count = 0
	for _ in range(n):
		(a, b) = map(int, input().split())
		count += b - a + 1
	moves = (k - count % k) % k
	print(moves)
main()


(n, k) = map(int, input().split())
L = {''}
for i in range(n):
	(m, f) = input().split()
	for i in range(int(m), int(f) + 1):
		L |= {i}
L.remove('')
x = len(L)
if x % k == 0:
	print('0')
else:
	print(k - x % k)


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


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


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


Sum = 0
X = list(map(int, input().split()))
for i in range(X[0]):
	Temp = list(map(int, input().split()))
	Sum += Temp[1] - Temp[0] + 1
print(X[1] - Sum % X[1] if Sum % X[1] != 0 else Sum % X[1])


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


(n, k) = list(map(int, input().split()))
res = 0
for i in range(n):
	(start, end) = list(map(int, input().split()))
	res += end - start + 1
print(0 if not res % k else k - res % k)


(n, k) = map(int, input().split())
ans = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	ans += b - a + 1
if ans % k == 0:
	print(0)
else:
	print(k - ans % k)


def main():
	(n, k) = map(int, input().split())
	lr = []
	for _ in range(n):
		lr.append([int(i) for i in input().split()])
	cnt = 0
	for i in range(n):
		cnt += lr[i][1] - lr[i][0] + 1
	if cnt % k == 0:
		print(0)
		return
	t = 0
	while cnt % k != 0:
		t += 1
		cnt += 1
	print(t)
main()


l = list(map(int, input().split()))
(n, k) = (l[0], l[1])
i = 0
sum = 0
while i < n:
	l = list(map(int, input().split()))
	l = list(range(l[0], l[1] + 1))
	sum += len(l)
	i += 1
if sum % k == 0:
	print(0)
else:
	print((sum // k + 1) * k - sum)


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


(n, m) = list(map(int, input().split()))
tot = sum([x[1] - x[0] + 1 for x in [[int(x) for x in input().split()] for i in range(n)]])
print((m - tot % m) % m)


import sys
input = sys.stdin.readline
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 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(l, ps, n, t):
	for i in range(n - l + 1):
		if ps[i + l] - ps[i] <= t:
			return True
	return False
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
import bisect as bis
import random
import sys

def solve():
	(n, k) = read()
	diff = 0
	for i in range(n):
		(l, r) = read()
		diff += r - l + 1
	if diff % k == 0:
		print(0)
	else:
		print(k - diff % k)
for _ in range(1):
	solve()


(n, k) = map(int, input().split())
a = []
for i in range(n):
	(x, y) = map(int, input().split())
	coc = y - x + 1
	a.append(coc)
if sum(a) % k == 0:
	print('0')
else:
	print(k - sum(a) % k)


(n, k) = map(int, input().split())
l = []
for i in range(n):
	put = input().split()
	l.append(put)
s = 0
for i in l:
	s += int(i[1]) - int(i[0]) + 1
if s % k == 0:
	print(0)
else:
	print(k - s % k)


(n, k) = map(int, input().split())
t = 0
for i in range(n):
	(li, ri) = map(int, input().split())
	t = t + (ri - li + 1)
if t % k == 0:
	print(0)
	exit()
print(k - t % k)


(n, k) = [int(x) for x in input().split()]
ans = 0
for i in range(n):
	temp = [int(x) for x in input().split()]
	ans += temp[1] - temp[0] + 1
if ans % k == 0:
	print(0)
else:
	print(k - ans % k)


(n, k) = map(int, input().split())
sum = 0
while n > 0:
	(a, b) = map(int, input().split())
	n -= 1
	sum += b - a + 1
print((k - sum % k) % k)


import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

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

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

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

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

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def revn(n):
	m = 0
	while n > 0:
		m = m * 10 + n % 10
		n = n // 10
	return m
(n, k) = mi()
ans = 0
for i in range(n):
	(l, r) = mi()
	for i in range(l, r + 1):
		ans += 1
if ans % k == 0:
	print(0)
else:
	print(k - ans % k)


(n, k) = map(int, input().split())
r = 0
for i in range(n):
	num = list(map(int, input().split()))
	r += abs(num[0] - num[1]) + 1
if r > k or r < k:
	if r % k == 0:
		print(0)
	else:
		print(-r % k)
else:
	print(0)


(n, k) = map(int, input().split())
s = 0
for i in range(int(n)):
	(a, b) = map(int, input().split())
	s += b - a + 1
if s % k == 0:
	print(0)
else:
	c = 0
	while s % k != 0:
		s += 1
		c += 1
	print(c)


(n, k) = map(int, input().split())
a = []
for i in range(n):
	(l, r) = map(int, input().split())
	d = r - l + 1
	a.append(d)
if sum(a) % k == 0:
	print(0)
	exit()
else:
	print(k - sum(a) % k)
	exit()


import sys
input = sys.stdin.readline
(n, k) = [int(x) for x in input().split()]
summ = 0
for _ in range(n):
	(l, r) = list(map(int, input().split()))
	summ += r - l + 1
if summ % k == 0:
	print(0)
else:
	print((summ // k + 1) * k - summ)


def solve():
	(n, k) = map(int, input().split())
	x = 0
	for _ in range(n):
		(l, r) = map(int, input().split())
		x += r - l + 1
	movs = 0
	while x % k != 0:
		movs += 1
		x += 1
	print(movs)
solve()


(n, k) = map(int, input().split())
diff = 0
for _ in range(n):
	(start, end) = map(int, input().split())
	diff += end - start + 1
print((k - diff % k) % k)


(n, k) = [int(x) for x in input().split()]
s = 0
for i in range(n):
	(l, r) = [int(x) for x in input().split()]
	s += r - l + 1
c = 0
if s % k != 0:
	c = k - s % k
else:
	c = 0
print(c)


from sys import *
(N, K) = map(int, input().split())
cur = 0
for i in range(N):
	(l, r) = map(int, stdin.readline().split())
	cur += r - l + 1
if cur % K == 0:
	print(0)
else:
	print(K - cur % K)


(n, k) = list(map(int, input().split(' ')))
s = [None] * n
cnt = 0
for i in range(n):
	s[i] = list(map(int, input().split(' ')))
	cnt += s[i][1] - s[i][0] + 1
i = 0
while True:
	if cnt % k == 0:
		break
	cnt += 1
	i += 1
print(i)


(n, k) = input().split(' ')
(c, z) = (0, 0)
for i in range(int(n)):
	(e, s) = input().split(' ')
	c += int(s) - int(e) + 1
if c % int(k) == 0:
	print(0)
elif c < int(k):
	print(int(k) - c)
else:
	while True:
		c += 1
		z += 1
		if c % int(k) == 0:
			print(z)
			break


from sys import stdin, stdout
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))
for i in range(1):
	(n, c) = lst()
	ans = 0
	for i in range(n):
		(a, b) = lst()
		ans += b - a + 1
	if ans % c == 0:
		print(0)
		continue
	next = ans // c * c + c
	print(next - ans)


class CodeforcesTask289ASolution:

	def __init__(self):
		self.result = ''
		self.n_k = []
		self.segments = []

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

	def process_task(self):
		curr = 0
		for seg in self.segments:
			curr += seg[1] - seg[0] + 1
		moves = self.n_k[1] - curr % self.n_k[1]
		if moves == self.n_k[1]:
			moves = 0
		self.result = str(moves)

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


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


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


(n, k) = map(int, input().split())
li = []
res = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	res += b - a + 1
r = 0
while res % k != 0:
	res += 1
	r += 1
print(r)


q = lambda : map(int, input().split())
(n, k) = q()
a = []
for _ in range(n):
	a += [q()]
print((k - sum((r - l + 1 for (l, r) in a)) % k) % k)


(n, k) = [int(x) for x in input().split()]
arr = []
for i in range(n):
	l_r = [int(x) for x in input().split()]
	arr.append(l_r)
s = 0
for i in range(n):
	s += arr[i][1] - arr[i][0] + 1
c = 0
if s % k != 0:
	c = k - s % k
else:
	c = 0
print(c)


def main():
	MAX = float('infinity')
	(n, k) = map(int, input().split())
	segments = []
	_sum = 0
	for _ in range(n):
		(li, ri) = map(int, input().split())
		segments.append((li, ri))
		_sum += ri - li + 1
	if _sum % k == 0:
		print(0)
		return
	segments.sort()
	segments = [(-MAX, -MAX)] + segments
	segments.append((MAX, MAX))
	ans = 0
	for i in range(1, len(segments) - 1):
		(prv, cur, nxt) = (segments[i - 1], segments[i], segments[i + 1])
		while cur[0] != prv[1]:
			_sum += 1
			ans += 1
			segments[i] = (segments[i][0] - 1, segments[i][1])
			cur = segments[i]
			if _sum % k == 0:
				print(ans)
				return
		while cur[1] != nxt[0]:
			_sum += 1
			segments[i] = (segments[i][0], segments[i][1] + 1)
			cur = segments[i]
			ans += 1
			if _sum % k == 0:
				print(ans)
				return
	else:
		print('ops')
main()


(a, b) = map(int, input().split())
c = 0
f = []
for i in range(a):
	(x, y) = map(int, input().split())
	c += y - x + 1
	f.append(x)
	f.append(y)
if c % b == 0:
	print(0)
elif c < b:
	print(b - c)
elif c > b:
	w = (c // b + 1) * b - c
	v = c - c // b * b
	t = v
	if f[0] != 1:
		pass
	else:
		for i in range(2, len(f), 2):
			s = f[i] - f[i - 1] - 1
			if s == 0:
				continue
			else:
				v -= s
			if v <= 0:
				break
	if v <= 0:
		print(min([w, t]))
	else:
		print(w)


(n, k) = list(map(int, input().split()))
sum = 0
for i in range(0, n):
	(l, r) = list(map(int, input().split()))
	sum += r - l + 1
if sum % k == 0:
	res = sum
else:
	res = (sum // k + 1) * k
print(res - sum)


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


(n, k) = input().split(' ')
(c, z, n, k) = (0, 0, int(n), int(k))
for i in range(n):
	(e, s) = input().split(' ')
	c += int(s) - int(e) + 1
while c % k != 0:
	c += 1
	z += 1
print(z)


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


(n, k) = list(map(int, input().split(' ')))
ans = 0
val = 0
for i in range(n):
	a = list(map(int, input().split(' ')))
	val += a[1] - a[0] + 1
while val % k != 0:
	val += 1
	ans += 1
print(ans)


s = [int(s) for s in input().split()]
d = []
total = 0
total_init = 0
for i in range(s[0]):
	d.append([int(d) for d in input().split()])
for i in d:
	total += i[1] - i[0] + 1
total_init = total
if total % s[1] == 0:
	print(0)
else:
	while total % s[1] != 0:
		total += 1
	print(total - total_init)


(n, k) = map(int, input().split())
c = 0
s = list()
for i in range(n):
	(x, y) = map(int, input().split())
	s.append(y - x + 1)
z = sum(s)
while 1:
	if z % k == 0:
		print(c)
		exit()
	else:
		z += 1
		c += 1


I = lambda : map(int, input().split())
(n, k) = I()
s = 0
for _ in range(n):
	(L, R) = I()
	s += R - L + 1
print(-s % k % k)


(n, k) = map(int, input().split())
arr = []
cnt = 0
for i in range(n):
	(a, b) = map(int, input().split())
	arr.append(a)
	arr.append(b)
	cnt += b - a + 1
m = cnt
if cnt % k == 0:
	print(0)
	quit()
print(k - m % k)


def solve():
	(n, k) = map(int, input().split())
	z = 0
	for i in range(n):
		(x, y) = map(int, input().split())
		z += y - x + 1
	if z % k == 0:
		print(0)
	else:
		print(k - z % k)
try:
	solve()
except:
	pass


a = input().split(' ')
lista = []
contador = 0
contador2 = 0
for i in range(0, int(a[0])):
	b = input().split(' ')
	lista.append(b)
for j in range(0, len(lista)):
	contador += int(lista[j][0])
	contador2 += int(lista[j][1])
formula = (contador - int(a[0]) - contador2) % int(a[-1])
print(formula)


(a, b) = input().strip().split(' ')
(a, b) = [int(a), int(b)]
p = 0
for i in range(a):
	c = list(map(int, input().strip().split(' ')))
	p += c[1] - (c[0] - 1)
if p % b == 0:
	print(0)
else:
	print(b - p % b % b)


(n, k) = map(int, input().strip().split())
l = list()
r = list()
s = 0
res = 0
for i in range(n):
	(x, y) = map(int, input().strip().split())
	if x == y:
		s += 1
	elif abs(x - y) == 1:
		s += 2
	else:
		s += abs(x - y) + 1
while s % k != 0:
	s += 1
	res += 1
print(res)


(n, k) = map(int, input().split())
print((sum((int(l) - int(r) for (l, r) in [input().split() for i in range(n)])) - n) % k)


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations, permutations
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
(n, k) = r()
arr = [rr() for i in range(n)]
x = sum((j - i + 1 for (i, j) in arr))
x = x % k
print((k - x) % k)


(n, k) = map(int, input().strip().split())
res = 0
for i in range(0, n):
	(l, r) = map(int, input().strip().split())
	res += r - l + 1
count1 = 0
while True:
	if res % k == 0:
		print(count1)
		break
	res += 1
	count1 += 1


import math

def findmin(num1, num2):
	if num1 < num2:
		return num1
	return num2
(n, k) = input().split()
n = int(n)
k = int(k)
sum = 0
while n:
	n -= 1
	(t1, t2) = input().split()
	t1 = int(t1)
	t2 = int(t2)
	sum += abs(t1 - t2) + 1
mod = sum % k
if mod == 0:
	print(0)
else:
	mul = (int(sum / k) + 1) * k - sum
	print(mul)


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


(a, b) = map(int, input().split())
z = 0
sum1 = 0
while z < a:
	(c, d) = map(int, input().split())
	if d != c:
		sum1 += 1
	sum1 += d - c
	if d == c:
		sum1 += 1
	z += 1
if sum1 % b == 0:
	print(0)
else:
	print(b - sum1 % b)


def main():
	(n, k) = list(map(int, input().split()))
	res = 0
	for _ in range(n):
		(a, b) = list(map(int, input().split()))
		res -= b - a + 1
	print(res % k)

def __starting_point():
	main()
__starting_point()


(N, k) = input().split()
N = int(N)
k = int(k)
x = 0
for i in range(N):
	(A, B) = input().split()
	A = int(A)
	B = int(B)
	x += max(A, B) - min(A, B) + 1
answer = x % k
if answer != 0:
	answer = k - answer
print(answer)


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


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


myMap = map(int, input().split())
myMap = list(myMap)
(n, k, sum) = (myMap[0], myMap[1], 0)
for i in range(n):
	temp = map(int, input().split())
	temp = list(temp)
	sum += temp[1] - temp[0] + 1
sum = sum % k
if sum != 0:
	sum = k - sum
print(sum)


(n, k) = map(int, input().split())
value = 0
for i in range(n):
	(l, r) = map(int, input().split())
	diff = r - l + 1
	value += diff
if value % k == 0:
	print('0')
else:
	print((value // k + 1) * k - value)


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


q = lambda : map(int, input().split())
(n, k) = q()
a = []
while n:
	a += [q()]
	n -= 1
print((k - sum((r - l + 1 for (l, r) in a)) % k) % k)


def solve():
	(n, k) = map(int, input().split())
	x = 0
	for _ in range(n):
		(l, r) = map(int, input().split())
		x += r - l + 1
	if x % k == 0:
		print(0)
	else:
		print(k - x % k)
solve()


from math import ceil
(n, k) = map(int, input().split())
suma = 0
for i in range(n):
	(l, r) = map(int, input().split())
	suma += r - l + 1
factor = ceil(suma / k)
ans = factor * k - suma
print(ans)


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


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


def main():
	(n, k) = map(int, input().split())
	add = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		add += b - a + 1
	if add % k == 0:
		print(0)
	else:
		print((k - add % k) % k)
main()


x = input()
x = x.split()
n = int(x[0])
k = int(x[1])
o = 0
c = 0
for i in range(n):
	seg = input()
	seg = seg.split()
	o += int(seg[1]) - int(seg[0]) + 1
while True:
	if o % k == 0:
		break
	o += 1
	c += 1
print(c)


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


(n, k) = [int(i) for i in input().split()]
total = 0
for x in range(n):
	(l, r) = [int(i) for i in input().split()]
	total = total + (abs(l - r) + 1)
count = 0
while total % k != 0:
	total += 1
	count += 1
print(count)


x = input()
l = x.split(' ')
n = int(l[0])
k = int(l[1])
sum = 0
for i in range(n):
	x = input()
	l = x.split()
	sum += int(l[1]) - int(l[0]) + 1
if sum % k == 0:
	print(0)
else:
	print(k - sum % k)


(n, k) = map(int, input().split())
o = 0
for i in range(n):
	(s, s1) = map(int, input().split())
	o += s1 - s
	o += 1
if o % k == 0:
	print(0)
elif o % k == o:
	print(k - o)
elif o % k > 0:
	j = o % k
	print(k - j)


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


def penguin_segments(k, seg):
	num_integers = 0
	for (x, y) in seg:
		num_integers += y - x + 1
	if num_integers % k == 0:
		return 0
	else:
		return k - num_integers % k
(n, k) = list(map(int, input().split()))
seg = []
for x in range(n):
	inp = list(map(int, input().split()))
	seg.append(inp)
print(penguin_segments(k, seg))


(n, k) = (int(i) for i in input().split())
segs = 0
for i in range(n):
	(s1, s2) = (int(s) for s in input().split())
	segs += s2 - s1 + 1
result = segs % k
if result == 0:
	print(result)
else:
	result = k - result
	print(result)


a = input().split()
n = int(a[0])
k = int(a[1])
p = 0
for i in range(n):
	b = input().split()
	p = int(b[1]) - int(b[0]) + 1 + p
if p % k == 0:
	print(0)
else:
	s = (k - p % k) % k
	print(s)


(n, k) = map(int, input().split())
l2 = []
for i in range(n):
	(l, r) = map(int, input().split())
	l2.append(r - l + 1)
sm = sum(l2)
r = 0
while sm % k != 0:
	sm += 1
	r += 1
print(r)


(n, k) = map(int, input().split())
coverd = 0
for i in range(0, n):
	(start, end) = map(int, input().split())
	coverd = coverd + (end - start + 1)
coverd = coverd % k
if coverd != 0:
	coverd = k - coverd
print(coverd)