AtCoder Beginner Contest 127 - Prison

We have N ID cards, and there are M gates.
We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.
How many of the ID cards allow us to pass all the gates alone?

-----Constraints-----
 - All values in input are integers.
 - 1 \leq N \leq 10^5
 - 1 \leq M \leq 10^5
 - 1 \leq L_i \leq R_i \leq N

-----Input-----
Input is given from Standard Input in the following format:
N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

-----Output-----
Print the number of ID cards that allow us to pass all the gates alone.

-----Sample Input-----
4 2
1 3
2 4

-----Sample Output-----
2

Two ID cards allow us to pass all the gates alone, as follows:
 - The first ID card does not allow us to pass the second gate.
 - The second ID card allows us to pass all the gates.
 - The third ID card allows us to pass all the gates.
 - The fourth ID card does not allow us to pass the first gate.

Source: atcoder | Difficulty: EASY


Solutions:
(N, M) = map(int, input().split())
L = []
for i in range(M):
	LR = list(map(int, input().split()))
	L.append(LR)
res = [L[0][0], L[0][1]]
for i in range(M):
	res = [max(res[0], L[i][0]), min(res[1], L[i][1])]
if res[1] < res[0]:
	print(0)
else:
	print(res[1] - res[0] + 1)


import sys
input = sys.stdin.readline

def main():
	gates = []
	(n, m) = map(int, input().split())
	gates = [list(map(int, input().split())) for _ in range(m)]
	min_data = 0
	max_data = 10 ** 5 + 1
	for gate in gates:
		if min_data < gate[0]:
			min_data = min(gate)
		if max_data > gate[1]:
			max_data = max(gate)
	print(len(list(range(min_data, max_data + 1))))

def __starting_point():
	main()
__starting_point()


(N, M) = [int(n) for n in input().split()]
L = 0
R = N
for m in range(M):
	(l, r) = [int(n) for n in input().split()]
	L = max(L, l)
	R = min(R, r)
print(R - L + 1 if L <= R else 0)


import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline

def RD():
	return input().rstrip()

def F():
	return float(input().rstrip())

def I():
	return int(input().rstrip())

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

def MF():
	return map(float, input().split())

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

def TI():
	return tuple(map(int, input().split()))

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

def Init(H, W, num):
	return [[num for i in range(W)] for j in range(H)]

def main():
	(N, M) = MI()
	D = [0] * (N + 2)
	for i in range(M):
		(L, R) = MI()
		D[L] += 1
		D[R + 1] -= 1
	ans = 0
	temp = 0
	for i in D[1:]:
		temp += i
		if temp == M:
			ans += 1
	print(ans)

def __starting_point():
	main()
__starting_point()


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


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


(N, M) = input().split()
N = int(N)
M = int(M)
L = [0] * M
R = [0] * M
for i in range(M):
	(L_i, R_i) = input().split()
	L[i] = int(L_i)
	R[i] = int(R_i)
print(max(min(R) - max(L) + 1, 0))


(N, M) = map(int, input().split())
Lm = -1
Rm = float('inf')
for _ in range(M):
	(L, R) = map(int, input().split())
	Lm = max(L, Lm)
	Rm = min(R, Rm)
print(max(Rm - Lm + 1, 0))


(N, M) = map(int, input().split())
a = list()
b = list()
for i in range(M):
	(tmp_a, tmp_b) = map(int, input().split())
	a.append(tmp_a)
	b.append(tmp_b)
print(min(b) - max(a) + 1 if min(b) - max(a) >= 0 else 0)


(n, m) = map(int, input().split())
(mn, mx) = (-1, 10 ** 7)
for _ in range(m):
	(l, r) = map(int, input().split())
	mn = max(mn, l)
	mx = min(mx, r)
ans = max(mx - mn + 1, 0)
print(ans)


(N, M) = map(int, input().split())
lsL = []
lsR = []
for i in range(M):
	(L, R) = map(int, input().split())
	lsL.append(L)
	lsR.append(R)
print(max(0, min(lsR) - max(lsL) + 1))


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


(n, m) = map(int, input().split())
mi = -1
ma = n
for i in range(m):
	(l, r) = map(int, input().split())
	mi = max(mi, l)
	ma = min(ma, r)
print(max(0, ma - mi + 1))


g = lambda : map(int, input().split())
(_, m) = g()
(ml, mr) = (0, 10 ** 5)
for _ in range(m):
	(l, r) = g()
	ml = max(ml, l)
	mr = min(mr, r)
print([0, mr - ml + 1][mr >= ml])


(n, m) = map(int, input().split())
l = 0
r = n + 1
for _ in range(m):
	(li, ri) = map(int, input().split())
	l = max(l, li)
	r = min(r, ri)
print(max(r - l + 1, 0))


(N, M) = map(int, input().split())
l = 1
r = N
for i in range(M):
	(L, R) = map(int, input().split())
	l = max(l, L)
	r = min(r, R)
print(max(r - l + 1, 0))


(n, m) = map(int, input().split())
a = 1
z = n
for _ in range(m):
	(l, m) = map(int, input().split())
	a = max(a, l)
	z = min(z, m)
print(max(0, z - a + 1))


(n, m) = map(int, input().split())
L = []
R = []
for _ in range(m):
	(l, r) = map(int, input().split())
	L.append(l)
	R.append(r)
ans = min(R) - max(L) + 1
print(ans) if ans > 0 else print(0)


(N, M) = map(int, input().split())
L = []
R = []
for i in range(M):
	(l, r) = map(int, input().split())
	L.append(l)
	R.append(r)
a = 1
b = N
for i in range(M):
	if L[i] >= a and R[i] >= b:
		a = L[i]
	if L[i] <= a and R[i] <= b:
		b = R[i]
	if L[i] >= a and R[i] <= b:
		a = L[i]
		b = R[i]
	if R[i] < a or L[i] > b:
		print(0)
		return
print(b - a + 1)


(n, m) = map(int, input().split())
s = 0
t = 10 ** 10
for i in range(m):
	(l, r) = map(int, input().split())
	s = max(s, l)
	t = min(t, r)
if s > t:
	print(0)
else:
	print(t - s + 1)


(n, m) = map(int, input().split())
(l, r) = (0, n)
for _ in range(m):
	(l0, r0) = map(int, input().split())
	(l, r) = (max(l, l0), min(r, r0))
print(max(0, r + 1 - l))


o = lambda : map(int, input().split())
(n, m) = o()
p = [list(o()) for _ in range(m)]
l = max(map(lambda x: x[0], p))
r = min(map(lambda x: x[1], p))
print(len(range(l, r + 1)))


(n, m, *lr) = list(map(int, open(0).read().split()))
print(max(0, min(lr[1::2]) - max(lr[::2]) + 1))


(N, M) = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
max_l = 0
min_r = 10 ** 9
for i in range(M):
	if max_l <= LR[i][0]:
		max_l = LR[i][0]
	if min_r >= LR[i][1]:
		min_r = LR[i][1]
print(max(0, min_r - max_l + 1))


in_gate = []
out_gate = []
(N, M) = map(int, input().split())
for _ in range(M):
	(temp_l, temp_r) = map(int, input().split())
	in_gate.append(temp_l)
	out_gate.append(temp_r)
ans_bef = min(out_gate) - max(in_gate)
if ans_bef < 0:
	print(0)
else:
	print(ans_bef + 1)


(n, m_) = map(int, input().split())
m = 0
M = 9 ** 9
for i in range(m_):
	(l, r) = map(int, input().split())
	m = max(m, l)
	M = min(M, r)
print(0 if M < m else M - m + 1)


(n, m) = map(int, input().split())
le = 1
ri = n
for i in range(m):
	(l, r) = map(int, input().split())
	le = max(le, l)
	ri = min(ri, r)
print(max(0, ri - le + 1))


(N, M) = map(int, input().split())
L = [0] * M
R = [0] * M
for m in range(M):
	(L[m], R[m]) = map(int, input().split())
print(max(0, min(R) - max(L) + 1))


(n, m) = input().split()
LR = [list(map(int, input().split())) for i in range(int(m))]
s = max([i[0] for i in LR])
e = min([i[1] for i in LR])
print(max(e - s + 1, 0))


(N, M) = map(int, input().split())
(L, R) = (0, N)
for i in range(M):
	(_L, _R) = map(int, input().split())
	L = max(_L, L)
	R = min(_R, R)
print(max(R - L + 1, 0))


(k, g) = map(int, input().split())
id = [map(int, input().split()) for i in range(g)]
(l, r) = list(zip(*id))
print(max(min(r) - max(l) + 1, 0))


(N, M) = map(int, input().split())
L0 = 1
R0 = N
for i in range(M):
	(L, R) = map(int, input().split())
	L0 = max(L0, L)
	R0 = min(R0, R)
print(max(R0 - L0 + 1, 0))


(N, M) = list(map(int, input().split()))
min_i = 0
max_i = N + 1
for _ in range(M):
	(a, b) = list(map(int, input().split()))
	min_i = max(min_i, a)
	max_i = min(max_i, b + 1)
c = max(max_i - min_i, 0)
print(c)


(N, M) = map(int, input().split())
LR = list((map(int, input().split()) for _ in range(M)))
(L, R) = list(map(list, zip(*LR)))
print(max(0, min(R) - max(L) + 1))


(N, M) = map(int, input().split())
(a, b) = (1, N)
for i in range(M):
	(L, R) = map(int, input().split())
	(a, b) = (max(a, L), min(b, R))
print(max(b - a + 1, 0))


(N, M) = map(int, input().split())
(L, R) = ([], [])
for i in range(M):
	(x, y) = map(int, input().split())
	L.append(x)
	R.append(y)
(max_l, min_r) = (max(L), min(R))
print(max(0, min_r - max_l + 1))


(n, m) = map(int, input().split())
min_i = 1
max_i = n
for i in range(m):
	(l, r) = map(int, input().split())
	min_i = max(min_i, l)
	max_i = min(max_i, r)
if max_i - min_i >= 0:
	print(max_i - min_i + 1)
else:
	print(0)


(n, m) = map(int, input().split())
l = 1
r = 10 ** 5
for _ in range(m):
	(l2, r2) = map(int, input().split())
	l = max(l, l2)
	r = min(r, r2)
print(max(r - l + 1, 0))


(n, m, *a) = map(int, open(0).read().split())
b = min(a[1::2]) - max(a[::2]) + 1
print(b if b > 0 else 0)


f = lambda : map(int, input().split())
(n, m) = f()
(L, R) = (0, n)
for i in range(m):
	(l, r) = f()
	L = max(L, l)
	R = min(R, r)
print(max(0, R - L + 1))


(N, M) = map(int, input().split())
L = list()
R = list()
for i in range(M):
	(l, r) = map(int, input().split())
	L.append(l)
	R.append(r)
ans = min(R) - max(L) + 1
if ans < 0:
	ans = 0
print(ans)


(n, m) = map(int, input().split())
L = [0] * m
R = [0] * m
for i in range(m):
	(L[i], R[i]) = list(map(int, input().split()))
print(max(min(R) - max(L) + 1, 0))


(_, __, *d) = map(int, open(0).read().split())
print(max(min(d[1::2]) - max(d[::2]) + 1, 0))


(N, M) = map(int, input().split())
mini = 0
maxi = N
for _ in range(M):
	(mini1, maxi1) = map(int, input().split())
	mini = max(mini, mini1)
	maxi = min(maxi, maxi1)
print(max(maxi - mini + 1, 0))


(N, M) = list(map(int, input().split()))
(a, b) = (0, N)
for _ in range(M):
	(tmpa, tmpb) = list(map(int, input().split()))
	(a, b) = (max(a, tmpa), min(b, tmpb))
ans = max(0, b - a + 1)
print(ans)


(n, m) = map(int, input().split())
(L, R) = (0, n)
for _ in range(m):
	(l, r) = map(int, input().split())
	(L, R) = (max(L, l), min(R, r))
print(max(R - L + 1, 0))


(N, M) = map(int, input().split())
ml = 0
mh = N
for i in range(M):
	(l, r) = map(int, input().split())
	ml = max(ml, l)
	mh = min(mh, r)
print(max(0, mh - ml + 1))


(n, m) = map(int, input().split())
le = 0
ri = n
for i in range(m):
	(a, b) = map(int, input().split())
	le = max(le, a)
	ri = min(ri, b)
print(max(0, ri - le + 1))


(N, M) = map(int, input().split())
n = 0
m = 999999
for i in range(M):
	(a, b) = map(int, input().split())
	n = max(a, n)
	m = min(b, m)
print(max(m - n + 1, 0))


(n, m) = map(int, input().split())
(x, y) = (1, n)
for _ in range(m):
	(a, b) = map(int, input().split())
	x = max(a, x)
	y = min(b, y)
print(max(0, y - x + 1))


import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque, Counter
from decimal import Decimal
import functools

def v():
	return input()

def k():
	return int(input())

def S():
	return input().split()

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

def X():
	return list(input())

def L():
	return list(input().split())

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

def lcm(a, b):
	return a * b // math.gcd(a, b)
sys.setrecursionlimit(10 ** 6)
mod = 10 ** 9 + 7
cnt = 0
ans = 0
inf = float('inf')
al = 'abcdefghijklmnopqrstuvwxyz'
AL = al.upper()
(N, M) = I()
count = 0
min_a = 1
max_b = N
for i in range(M):
	(a, b) = [int(i) for i in input().split()]
	if min_a < a:
		min_a = a
	if max_b > b:
		max_b = b
print(max(0, max_b - min_a + 1))


(n, m, *ls) = map(int, open(0).read().split())
l = max(ls[0::2])
r = min(ls[1::2])
print(max(0, r - l + 1))


(N, M) = map(int, input().split())
l = 1
r = N
for i in range(M):
	(a, b) = map(int, input().split())
	l = max(l, a)
	r = min(r, b)
print(max(0, r - l + 1))


(n, m) = list(map(int, input().split()))
ans = [i for i in range(1, n + 1)]
l_max = 0
r_min = n
for _ in range(m):
	(x, y) = list(map(int, input().split()))
	l_max = max(l_max, x)
	r_min = min(r_min, y)
print(len([i for i in range(l_max, r_min + 1)]))


import numpy as np
(n, m) = map(int, input().split())
x = [list(map(int, input().split())) for i in range(m)]
print(max(np.min(x, axis=0)[1] - np.max(x, axis=0)[0] + 1, 0))


(n, m) = map(int, input().split())
lll = []
rrr = []
for i in range(m):
	(l, r) = map(int, input().split())
	lll.append(l)
	rrr.append(r)
print(max(0, min(rrr) - max(lll) + 1))


(N, M) = list(map(int, input().split()))
diff = [0 for _ in range(N + 1)]
for _ in range(M):
	(L, R) = list(map(int, input().split()))
	diff[L - 1] += 1
	diff[R] -= 1
ans = 0
x = 0
for i in range(N):
	x += diff[i]
	if x == M:
		ans += 1
print(ans)


(e1, e2) = list(map(int, input().split()))
h = []
for i in range(e2):
	h.append(list(map(int, input().split())))
L = []
R = []
for i in range(len(h)):
	L.append(h[i][0])
for i in range(len(h)):
	R.append(h[i][1])
if min(R) >= max(L):
	print(min(R) - max(L) + 1)
else:
	print(0)


(N, M) = map(int, input().split())
L = -10 ** 9
R = +10 ** 9
for _ in range(M):
	(l, r) = map(int, input().split())
	L = max(L, l)
	R = min(R, r)
print(max(0, R - L + 1))


(n, m) = list(map(int, input().split()))
(prel, prer) = (0, 1000000.0)
for i in range(m):
	(l, r) = list(map(int, input().split()))
	(prel, prer) = (max(prel, l), min(prer, r))
if prer >= prel:
	print(prer - prel + 1)
else:
	print(0)


import math
import collections
import itertools

def YesNo(Bool):
	if Bool:
		print('Yes')
	else:
		print('No')
	return

def resolve():
	(N, M) = list(map(int, input().split()))
	L = [0] * M
	R = [0] * M
	Lmax = 0
	Rmin = 10 ** 10
	for i in range(M):
		(L[i], R[i]) = list(map(int, input().split()))
		Rmin = min(R[i], Rmin)
		Lmax = max(L[i], Lmax)
	print(max(0, Rmin - Lmax + 1))
resolve()


(n, m) = map(int, input().split())
mi = 1
mx = n
for i in range(m):
	(a, b) = map(int, input().split())
	mi = max(a, mi)
	mx = min(mx, b)
print(max(0, mx - mi + 1))


import numpy as np
(n, m) = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(m)]
s_np = np.array(s)
if min(s_np[:, 1]) - max(s_np[:, 0]) >= 0:
	print(min(s_np[:, 1]) - max(s_np[:, 0]) + 1)
else:
	print(0)


(N, M) = map(int, input().split())
(L, R) = ([], [])
for _ in range(M):
	(l, r) = map(int, input().split())
	L.append(l)
	R.append(r)
m = max(L)
M = min(R)
if M >= m:
	print(M - m + 1)
else:
	print(0)


(N, M, *X) = map(int, open(0).read().split())
ans = max(0, min(X[1::2]) - max(X[::2]) + 1)
print(ans)


(n, m) = map(int, input().split())
mn = 0
mx = n
for i in range(0, m):
	(a, b) = map(int, input().split())
	if mn < a:
		mn = a
	if mx > b:
		mx = b
print(max(mx - mn + 1, 0))


(n, m) = map(int, input().split())
a_max = 1
b_min = n
for i in range(m):
	(a, b) = map(int, input().split())
	if a > a_max:
		a_max = a
	if b < b_min:
		b_min = b
print(max(0, b_min - a_max + 1))


(n, m) = map(int, input().split())
l_r = [list(map(int, input().split())) for _ in range(m)]
l_ans = 0
r_ans = float('inf')
for (l, r) in l_r:
	l_ans = max(l_ans, l)
	r_ans = min(r_ans, r)
if r_ans < l_ans:
	ans = 0
else:
	ans = r_ans - l_ans + 1
print(ans)


(n, m) = map(int, input().split())
A = []
B = []
for i in range(m):
	(l, r) = map(int, input().split())
	A.append(l)
	B.append(r)
print(max(0, min(B) - max(A) + 1))


(n, m) = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
INF = float('inf')
(ans_l, ans_r) = (-INF, INF)
for (l, r) in lr:
	ans_l = max(ans_l, l)
	ans_r = min(ans_r, r)
print(max(0, ans_r - ans_l + 1))


(n, m) = map(int, input().split())
a = []
b = []
for i in range(m):
	(l, r) = map(int, input().split())
	a.append(l)
	b.append(r)
x = max(a)
y = min(b)
if y - x < 0:
	print('0')
else:
	print(y - x + 1)


(card_num, gate_num) = map(int, input().split())
(lmax, rmin) = map(int, input().split())
for i in range(1, gate_num):
	(l, r) = map(int, input().split())
	lmax = max(lmax, l)
	rmin = min(rmin, r)
print(max(0, rmin - lmax + 1))


(N, M) = map(int, input().split())
L = []
R = []
for i in range(M):
	(l, r) = map(int, input().split())
	L.append(l)
	R.append(r)
Rm = min(R)
LM = max(L)
if Rm - LM >= 0:
	print(Rm - LM + 1)
else:
	print(0)


(L, M) = map(int, input().split())
MAX = 0
MIN = 10 ** 5
for i in range(M):
	(A, B) = map(int, input().split())
	MAX = max(MAX, A)
	MIN = min(MIN, B)
print(max(0, MIN - MAX + 1))


(N, M) = map(int, input().split())
dp = [0] * (N + 1)
for i in range(M):
	(l, r) = map(int, input().split())
	dp[l - 1] += 1
	dp[r] -= 1
for i in range(N):
	dp[i + 1] += dp[i]
ans = 0
for i in range(N):
	if dp[i] == M:
		ans += 1
print(ans)


(n, m) = map(int, input().split())
lm = 0
rm = 10 ** 9
for i in range(m):
	(l, r) = map(int, input().split())
	lm = max(lm, l)
	rm = min(rm, r)
print(max(0, rm - lm + 1))


(n, m) = map(int, input().split())
L = 1
R = n
for i in range(m):
	(l, r) = map(int, input().split())
	L = max(l, L)
	R = min(r, R)
if R - L + 1 >= 0:
	print(R - L + 1)
else:
	print(0)


(N, M) = map(int, input().split())
L1 = 0
R1 = N
for m in range(M):
	(L2, R2) = map(int, input().split())
	L1 = max(L1, L2)
	R1 = min(R1, R2)
print(max(0, R1 - L1 + 1))


(n, m) = list(map(int, input().split()))
(l_list, r_list) = ([], [])
for i in range(m):
	(l, r) = list(map(int, input().split()))
	l_list.append(l)
	r_list.append(r)
limit_min = max(l_list)
limit_max = min(r_list)
cnt = 0
for i in range(1, n + 1):
	if limit_min <= i <= limit_max:
		cnt += 1
print(cnt)


import sys
(n, m) = map(int, sys.stdin.readline().split())
l = 1
r = n
for i in range(m):
	(new_l, new_r) = map(int, sys.stdin.readline().split())
	if new_l > l:
		l = new_l
	if new_r < r:
		r = new_r
ans = max(0, r - l + 1)
print(ans)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
(L, R) = ([], [])
for _ in range(m):
	(l, r) = map(int, input().split())
	L.append(l)
	R.append(r)
(l, r) = (max(L), min(R))
if l > r:
	print(0)
else:
	print(r - l + 1)


(N, M) = map(int, input().split())
LR = [list(map(int, input().split())) for _ in [0] * M]
l = max((L for (L, R) in LR))
r = min((R for (L, R) in LR))
print(max(0, r - l + 1))


(N, M) = map(int, input().split())
m0 = 1
m9 = N
for i in range(M):
	(L, R) = map(int, input().split())
	m0 = max(L, m0)
	m9 = min(R, m9)
print(max(0, m9 - m0 + 1))


(n, m) = map(int, input().split())
k = [list(map(int, input().split())) for i in range(m)]
(l, r) = [list(i) for i in zip(*k)]
x = max(l)
y = min(r)
print(max(y - x + 1, 0))


import numpy as np
(n, m) = map(int, input().split())
ans = [0] * n
for _ in range(m):
	(l, r) = map(int, input().split())
	ans[l - 1] += 1
	if r < n:
		ans[r] -= 1
c = list(np.cumsum(ans))
print(c.count(m))


(n, m) = map(int, input().split())
(l_max, r_min) = (-1, float('inf'))
for i in range(m):
	(li, ri) = map(int, input().split())
	l_max = max(l_max, li)
	r_min = min(r_min, ri)
ans = r_min - l_max + 1 if r_min - l_max >= 0 else 0
print(ans)


import sys
(_, _, *lst) = map(int, sys.stdin.read().split())
(l, r) = (max(lst[::2]), min(lst[1::2]))
print(0 if l > r else r - l + 1)


(N, M) = map(int, input().split())
card = [0] * (N + 2)
ans = [0] * (N + 1)
for i in range(M):
	(L, R) = map(int, input().split())
	card[L] += 1
	card[R + 1] -= 1
cnt = 0
for h in range(N + 1):
	cnt += card[h]
	ans[h] = str(cnt)
print(ans.count(str(M)))


(N, M) = map(int, input().split())
mm = 0
mx = 10 ** 5
for _ in range(M):
	(l, r) = map(int, input().split())
	mm = max(mm, l)
	mx = min(mx, r)
print(max(mx - mm + 1, 0))


(N, M) = map(int, input().split())
(lmax, rmin) = (1, N)
for i in range(M):
	(l, r) = map(int, input().split())
	if l > lmax:
		lmax = l
	if r < rmin:
		rmin = r
print(max(rmin - lmax + 1, 0))


(N, M) = list(map(int, input().split()))
la = 1
rm = N
for i in range(M):
	(a, b) = list(map(int, input().split()))
	la = max(la, a)
	rm = min(rm, b)
print(max(0, rm - la + 1))


(*t,) = map(int, open(0).read().split())
print(max(0, min(t[3::2]) - max(t[2::2]) + 1))


(n, M, *X) = map(int, open(0).read().split())
print(max(0, min(X[1::2]) - max(X[::2]) + 1))


(n, m) = map(int, input().split())
a = 1
for _ in range(m):
	(l, r) = map(int, input().split())
	if n > r:
		n = r
	if a < l:
		a = l
print(n - a + 1) if n - a >= 0 else print(0)


(n, m) = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(m)]
(max_l, min_r) = (0, float('inf'))
for i in range(m):
	max_l = max(s[i][0], max_l)
	min_r = min(s[i][1], min_r)
print(max(min_r - max_l + 1, 0))


(n, m) = map(int, input().split())
(lmax, rmin) = map(int, input().split())
for i in range(1, m):
	(l, r) = map(int, input().split())
	lmax = max(lmax, l)
	rmin = min(rmin, r)
print(max(0, rmin - lmax + 1))


p = 0
x = 100001
(n, m) = map(int, input().split())
for _ in range(m):
	(l, r) = map(int, input().split())
	p = max(p, l)
	x = min(x, r)
print(x - p + 1 if x >= p else 0)


(N, M) = [int(x) for x in input().split()]
L = list()
R = list()
for i in range(M):
	(l, r) = [float(x) for x in input().split()]
	L.append(l)
	R.append(r)
Lmax = max(L)
Rmin = min(R)
if Rmin - Lmax < 0:
	print(0)
else:
	print(int(Rmin - Lmax + 1))


(n, m) = map(int, input().split())
(l, r) = (1, n)
for _ in range(m):
	(nl, nr) = map(int, input().split())
	l = max(l, nl)
	r = min(r, nr)
print(max(0, r - l + 1))


(N, M) = map(int, input().split())
L = 1
R = N
for i in range(M):
	(l, r) = map(int, input().split())
	if L < l:
		L = l
	if R > r:
		R = r
	if R < L:
		print(0)
		break
else:
	print(R - L + 1)


import numpy as np
INT = lambda : int(input())
INTM = lambda : map(int, input().split())
STRM = lambda : map(str, input().split())
STR = lambda : str(input())
LIST = lambda : list(map(int, input().split()))
LISTS = lambda : list(map(str, input().split()))

def do():
	(n, m) = INTM()
	ls = []
	rs = []
	for i in range(m):
		(l, r) = INTM()
		ls.append(l)
		rs.append(r)
	ans = min(rs) - max(ls) + 1
	if ans <= 0:
		ans = 0
	print(ans)

def __starting_point():
	do()
__starting_point()


(N, M) = map(int, input().split())
L_lis = []
R_lis = []
for i in range(M):
	(L, R) = map(int, input().split())
	L_lis.append(L)
	R_lis.append(R)
(new_L, new_R) = (max(L_lis), min(R_lis))
if new_L <= new_R:
	print(new_R - new_L + 1)
else:
	print(0)


(n, m) = map(int, input().split())
l = 1
r = n
for i in range(m):
	(a, b) = map(int, input().split())
	if a > l:
		l = a
	if b < r:
		r = b
if r < l:
	r = l - 1
print(r - l + 1)


def resolve():
	(n, m) = map(int, input().split())
	l = 0
	r = n
	for i in range(m):
		(nl, nr) = map(int, input().split())
		l = max(l, nl)
		r = min(r, nr)
	print(max(r - l + 1, 0))
resolve()


(n, m) = map(int, input().split())
ok = [0] * (n + 2)
ans = 0
for _ in range(m):
	(l, r) = map(int, input().split())
	ok[l] += 1
	ok[r + 1] -= 1
for i in range(1, n + 2):
	ok[i] += ok[i - 1]
	if ok[i] == m:
		ans += 1
print(ans)


(l, m) = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
(ll, rr) = (1, l)
for (l, r) in lr:
	if ll <= l <= rr or l <= ll <= r:
		(ll, rr) = (max(ll, l), min(rr, r))
	else:
		print(0)
		return
print(rr - ll + 1)


(n, m) = map(int, input().split())
s = 1
e = n
for _ in range(m):
	(l, r) = map(int, input().split())
	s = max(s, l)
	e = min(e, r)
print(max(e - s + 1, 0))


details = list(map(int, input().split()))
N = details[0]
M = details[1]
first = []
second = []
for i in range(M):
	det = list(map(int, input().split()))
	first.append(det[0])
	second.append(det[1])
maxi = max(first)
mini = min(second)
if mini - maxi >= 0:
	print(mini - maxi + 1)
else:
	print(0)


def main():
	(_, _, *lst) = map(int, open(0).read().split())
	(l, r) = (max(lst[::2]), min(lst[1::2]))
	print(0 if l > r else r - l + 1)
main()


(N, M) = map(int, input().split())
Min = 1
Max = N
for i in range(M):
	(L, R) = map(int, input().split())
	if Min <= L and R <= Max:
		(Min, Max) = (L, R)
	elif Min <= R and R <= Max:
		Max = R
	elif Min <= L and L <= Max:
		Min = L
	elif L <= Min and Max <= R:
		pass
	else:
		(Max, Min) = (0, 1)
		break
print(Max - Min + 1)


(n, m) = list(map(int, input().split()))
gate = [[int(i) for i in input().split()] for _ in range(m)]
minv = gate[0][0]
maxv = gate[0][1]
for x in gate:
	minv = max(minv, x[0])
	maxv = min(maxv, x[1])
print(maxv - minv + 1 if maxv - minv + 1 >= 1 else 0)


(n, m) = map(int, input().split())
ma = float('inf')
mi = 0
for i in range(m):
	(a, b) = map(int, input().split())
	ma = min(ma, b)
	mi = max(mi, a)
print(max(ma - mi + 1, 0))


(n, m, *t) = map(int, open(0).read().split())
(a, c) = ([0] * -~n, 0)
for (l, r) in zip(t[::2], t[1::2]):
	a[l - 1] += 1
	a[r] -= 1
for i in range(n):
	a[i + 1] += a[i]
	c += a[i] == m
print(c)


(N, M) = map(int, input().split())
L = [0] * M
R = [0] * M
for i in range(M):
	(L[i], R[i]) = map(int, input().split())
lt = max(L)
rt = min(R)
print(max(0, rt - lt + 1))


(N, M) = map(int, input().split())
L = [0 for _ in range(N)]
for i in range(M):
	(l, r) = map(int, input().split())
	l -= 1
	r -= 1
	L[l] += 1
	if r + 1 < N:
		L[r + 1] -= 1
S = [0]
for i in range(N):
	temp = L[i] + S[-1]
	S.append(temp)
ans = 0
for i in range(1, N + 1):
	if S[i] == M:
		ans += 1
print(ans)


(n, m) = list(map(int, input().split()))
l = 0
r = 99999999
for i in range(m):
	(a, b) = list(map(int, input().split()))
	l = max(l, a)
	r = min(r, b)
if r >= l:
	print(r - l + 1)
else:
	print(0)


details = list(map(int, input().split()))
N = details[0]
M = details[1]
ab = [list(map(int, input().split())) for _ in range(M)]
smallest_max = ab[0][1]
for i in range(1, M):
	if ab[i][1] < smallest_max:
		smallest_max = ab[i][1]
largest_min = ab[0][0]
for i in range(1, M):
	if ab[i][0] > largest_min:
		largest_min = ab[i][0]
if smallest_max - largest_min >= 0:
	print(smallest_max - largest_min + 1)
else:
	print(0)


(N, M) = map(int, input().split())
L = []
R = []
ans = 0
for j in range(M):
	(l, r) = map(int, input().split())
	L.append(l)
	R.append(r)
Lmax = max(L)
Rmin = min(R)
for i in range(N):
	if Lmax <= i + 1 and i + 1 <= Rmin:
		ans += 1
print(ans)


(n, m) = map(int, input().split())
ans = 0
ml = 0
mr = n
for i in range(1, m + 1):
	(l, r) = map(int, input().split())
	ml = max(ml, l)
	mr = min(mr, r)
print(max(0, mr - ml + 1))


(N, M) = map(int, input().split())
(L, R) = (0, 10 ** 5 + 1)
for _ in range(M):
	(l, r) = map(int, input().split())
	L = max(L, l)
	R = min(R, r + 1)
if R > L:
	result = R - L
else:
	result = 0
print(result)


(n, m) = map(int, input().split())
ll = [list(map(int, input().split())) for i in range(m)]
a = 0
b = n
for (l, r) in ll:
	if a < l:
		a = l
	if r < b:
		b = r
print(max(b - a + 1, 0))


(n, m) = map(int, input().split())
(l, r) = (1, pow(10, 5))
for _ in range(m):
	(l_, r_) = map(int, input().split())
	l = max(l, l_)
	r = min(r, r_)
print(max(0, r - l + 1))


(n, m) = map(int, input().split())
l_max = 0
r_min = 10 ** 5 + 1
for i in range(m):
	(l, r) = map(int, input().split())
	l_max = max(l, l_max)
	r_min = min(r, r_min)
print(max(r_min - l_max + 1, 0))


def main():
	IN = list(map(int, open(0).read().split()[2:]))
	(L, R) = (max(IN[::2]), min(IN[1::2]))
	print(R - L + 1 if R >= L else 0)
main()


from itertools import accumulate
(N, M) = list(map(int, input().split()))
left = 1
right = N
for i in range(M):
	(a, b) = list(map(int, input().split()))
	left = max(left, a)
	right = min(right, b)
print(max(0, right - left + 1))


(n, m) = list(map(int, input().split()))
for i in range(m):
	if i == 0:
		(l, r) = list(map(int, input().split()))
	else:
		(l2, r2) = list(map(int, input().split()))
		l = max(l, l2)
		r = min(r, r2)
if l > r:
	print(0)
else:
	print(r - l + 1)


(N, M) = map(int, input().split())
max_L = 1
min_R = N
for _ in range(M):
	(L, R) = map(int, input().split())
	max_L = max(L, max_L)
	min_R = min(R, min_R)
if min_R - max_L < 0:
	print(0)
else:
	print(min_R - max_L + 1)


(N, M) = map(int, input().split())
p = 0
q = 100000000000000
for i in range(M):
	(l, r) = map(int, input().split())
	p = max(p, l)
	q = min(q, r)
print(max(q - p + 1, 0))


def main():
	import sys
	pin = sys.stdin.readline
	pout = sys.stdout.write
	perr = sys.stderr.write
	(N, M) = map(int, pin().split())
	(L, R) = map(int, pin().split())
	for i in range(M - 1):
		(l, r) = map(int, pin().split())
		if L < l and l <= R:
			L = l
		elif R < l:
			print(0)
			return
		if L <= r and r < R:
			R = r
		elif r < L:
			print(0)
			return
	print(R - L + 1)
	return
main()


(n, m) = map(int, input().split())
x = 1
y = n
for _ in range(m):
	(l, r) = map(int, input().split())
	if x < l:
		x = l
	if y > r:
		y = r
print(max(y - x + 1, 0))


f = lambda : map(int, input().split())
(_, m) = f()
(l, r) = zip(*[f() for _ in range(m)])
print(max(0, min(r) - max(l) + 1))


(n, m) = map(int, input().split())
lower = -float('inf')
upper = float('inf')
for i in range(m):
	(l, r) = map(int, input().split())
	if l > lower:
		lower = l
	if r < upper:
		upper = r
	if upper < lower:
		upper = 0
		lower = 0
		break
if upper == lower == 0:
	print(0)
else:
	print(upper - lower + 1)


(N, M) = map(int, input().split())
L = [0] * M
R = [0] * M
for i in range(M):
	(L[i], R[i]) = map(int, input().split())
L_SORT = sorted(L)
R_SORT = sorted(R)
L_MAX = L_SORT[M - 1]
R_MIN = R_SORT[0]
if R_MIN - L_MAX < 0:
	print(0)
else:
	print(R_MIN - L_MAX + 1)


(n, m) = map(int, input().split())
lr = []
for i in range(m):
	LR = list(map(int, input().split()))
	lr.append(LR)
(min, max) = (1, n)
for i in range(m):
	if lr[i][0] > min:
		min = lr[i][0]
	if lr[i][1] < max:
		max = lr[i][1]
if min > max:
	print('0')
else:
	print(max - min + 1)


(n, m) = map(int, input().split())
(al, bl) = ([], [])
for i in range(m):
	(a, b) = map(int, input().split())
	al.append(a)
	bl.append(b)
print(max(min(bl) - max(al) + 1, 0))


(n, m) = map(int, input().split())
(lmax, rmin) = (0, n)
for _ in range(m):
	(l, r) = map(int, input().split())
	lmax = max(lmax, l)
	rmin = min(rmin, r)
print(rmin - lmax + 1 if rmin - lmax + 1 > 0 else 0)


(n, m) = map(int, input().split())
a = 0
b = float('inf')
for i in range(m):
	(c, d) = map(int, input().split())
	a = max(a, c)
	b = min(b, d)
print(max(0, b - a + 1))


(n, m, *t) = map(int, open(0).read().split())
print(max(0, min(t[1::2]) - max(t[::2]) + 1))


(n, m) = map(int, input().split())
maxl = 1
minr = n
while m:
	(l, r) = map(int, input().split())
	maxl = max(maxl, l)
	minr = min(minr, r)
	m -= 1
print(max(0, minr - maxl + 1))


(N, M) = map(int, input().split())
left = 1
right = N
for i in range(M):
	(L, R) = map(int, input().split())
	left = max(left, L)
	right = min(right, R)
print(max(0, right - left + 1))


(N, M) = list(map(int, input().split()))
(L, R) = list(map(int, input().split()))
for i in range(M - 1):
	(H, I) = list(map(int, input().split()))
	if H > L:
		L = H
	if R > I:
		R = I
	if R < L:
		break
if R < L:
	print(0)
else:
	ans = list(range(L, R + 1))
	print(len(ans))


(n, m) = list(map(int, input().split()))
start = 1
end = n
for i in range(m):
	(x, y) = list(map(int, input().split()))
	start = max(start, x)
	end = min(end, y)
print(end - start + 1 if end - start >= 0 else 0)


from itertools import accumulate
(N, M) = list(map(int, input().split()))
imos = [0] * (N + 2)
for i in range(M):
	(l, r) = list(map(int, input().split()))
	imos[l] += 1
	imos[r + 1] -= 1
imos = list(accumulate(imos))
ans = 0
for im in imos:
	ans += im == M
print(ans)


(n, m) = map(int, input().split())
(l, r) = zip(*(list(map(int, input().split())) for _ in range(m)))
print(max(0, min(r) - max(l) + 1))


(n, m) = map(int, input().split())
min_id = 0
max_id = 10 ** 9
for _ in range(m):
	(l, r) = map(int, input().split())
	min_id = max(l, min_id)
	max_id = min(r, max_id)
ans = max_id - min_id + 1
if ans <= 0:
	print(0)
else:
	print(ans)


x = lambda : map(int, input().split())
(N, M) = x()
(l, r) = zip(*[tuple(x()) for _ in range(M)])
print(max(0, 1 + min(r) - max(l)))


(n, m) = map(int, input().split())
maxl = 1
minr = n
for i in range(m):
	(l, r) = map(int, input().split())
	maxl = max(maxl, l)
	minr = min(minr, r)
print(max(minr - maxl + 1, 0))


import sys
import copy
import math
import itertools
import numpy as np
l = [int(c) for c in input().split()]
N = l[0]
M = l[1]
LR = [list(map(int, input().split())) for c in range(M)]
small = 0
large = N
for i in range(M):
	if small < LR[i][0]:
		small = LR[i][0]
	if large > LR[i][1]:
		large = LR[i][1]
if small <= large:
	print(large - small + 1)
else:
	print(0)


(N, M) = map(int, input().split())
L = []
R = []
for I in range(M):
	(x, y) = map(int, input().split())
	L.append(x)
	R.append(y + 1)
F = list(range(max(L), min(R)))
print(len(F))


(n, m) = map(int, input().split())
left = set()
right = set()
for i in range(m):
	(l, r) = map(int, input().split())
	left.add(l)
	right.add(r)
mini = max(left)
maxi = min(right)
print(len(range(mini, maxi + 1)))


(n, m) = map(int, input().split())
l = 0
r = n
for i in range(m):
	(lt, rt) = map(int, input().split())
	l = max(l, lt)
	r = min(r, rt)
print(max(0, r - l + 1))


(n, m) = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]
f_inf = float('inf')
max_l = 0
min_r = f_inf
for (l, r) in LR:
	max_l = max(max_l, l)
	min_r = min(min_r, r)
if min_r >= max_l:
	print(min_r - max_l + 1)
else:
	print(0)


(n, m) = map(int, input().split())
l = -1
r = 10 ** 6
for _ in range(m):
	(il, ir) = map(int, input().split())
	l = max(l, il)
	r = min(r, ir)
print(max(0, r - l + 1))


(n, m) = map(int, input().split())
(ll, rr) = (0, n)
for i in range(m):
	(l, r) = map(int, input().split())
	ll = max(l, ll)
	rr = min(r, rr)
print(max(rr - ll + 1, 0))


(N, M) = map(int, input().split())
(L, R) = ([0] * M, [0] * M)
A = set(range(N))
(lm, rm) = (0, N)
for i in range(M):
	(L[i], R[i]) = map(int, input().split())
	lm = max(lm, L[i])
	rm = min(rm, R[i])
if lm <= rm:
	print(rm - lm + 1)
else:
	print(0)


(N, M) = map(int, input().split())
L = [0] * M
R = [0] * M
for i in range(M):
	(L[i], R[i]) = map(int, input().split())
print(0 if min(R) < max(L) else min(R) - max(L) + 1)


(N, M) = map(int, input().split())
L_list = []
R_list = []
for i in range(M):
	(A, B) = map(int, input().split())
	L_list.append(A)
	R_list.append(B)
if max(L_list) > min(R_list):
	print(0)
else:
	print(min(R_list) - max(L_list) + 1)


(N, M) = map(int, input().split())
res = 0
L_max = 0
R_min = 100000
for _ in range(M):
	(L, R) = map(int, input().split())
	if L_max < L:
		L_max = L
	if R < R_min:
		R_min = R
print(max(0, R_min - L_max + 1))


(N, M, *D) = map(int, open(0).read().split())
print(max(min(D[1::2]) - max(D[::2]) + 1, 0))


(N, M) = map(int, input().split())
lv = 1
rv = N
for _ in range(M):
	(L, R) = map(int, input().split())
	lv = max(lv, L)
	rv = min(rv, R)
print(0 if lv > rv else rv - lv + 1)


(n, m) = map(int, input().split())
(L, R) = (1, n)
for i in range(m):
	(l, r) = map(int, input().split())
	L = max(L, l)
	R = min(R, r)
print(len([int(i) for i in range(L, R + 1)]))


def main():
	(N, M) = list(map(int, input().split()))
	imos = [0] * (N + 2)
	ans = 0
	for _ in range(M):
		(l, r) = list(map(int, input().split()))
		imos[l] += 1
		imos[r + 1] -= 1
	for i in range(1, N + 2):
		imos[i] += imos[i - 1]
		if imos[i] == M and i != N + 1:
			ans += 1
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
lm = 1
rm = n
for i in range(m):
	(l, r) = map(int, input().split())
	lm = max(lm, l)
	rm = min(rm, r)
ans = rm - lm + 1
print(ans if ans >= 0 else 0)