CodeEff/ECCO huggingface dataset:
# Original solution
N, M = list(map(int,input().split()))


def main():
	Ls = []
	Rs = []
	for i in range(M):
		inputs = list(map(int,input().split()))
		Ls.append(inputs[0])
		Rs.append(inputs[1])

	result = set()
	for i in range(1, M + 1):
		Li = Ls[i - 1]
		LiPlus = { j for j in range(Li + 1, Rs[i - 1] + 1) }
		LiPlus.add(Li)

		if len(result) == 0:
			result = LiPlus
			continue
		result = result & LiPlus

	print((len(result)))

if __name__ == '__main__':
	main()

# Optimized solution
N, M = list(map(int,input().split()))


def main():
	Ls = []
	Rs = []
	for i in range(M):
		inputs = list(map(int,input().split()))
		Ls.append(inputs[0])
		Rs.append(inputs[1])

	maxL = max(Ls)
	minR = min(Rs)

	if maxL > minR:
		print((0))
	else:
		print((minR - maxL + 1))

if __name__ == '__main__':
	main()
# Original solution
from collections import Counter

N, M = list(map(int, input().split()))
cnt = Counter()

for i in range(M):
    l, r = list(map(int, input().split()))
    for i in range(l, r + 1):
        cnt[i] += 1

ans = 0
for v in list(cnt.values()):
    if v == M:
        ans += 1
print(ans)

# Optimized solution
from collections import Counter

N, M = list(map(int, input().split()))
L = []
R = []

for i in range(M):
    l, r = list(map(int, input().split()))
    L.append(l)
    R.append(r)

maxL = max(L)
minR = min(R)

ans = max(0, minR - maxL + 1)
print(ans)
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

def main():
    n, m = list(map(int, input().split()))
    ans = set([i for i in range(1 , n + 1)])
    for _ in range(m):
        l, r = list(map(int, input().split()))
        card = set([i for i in range(l, r + 1)])
        ans = ans & card
    print((len(ans)))

if __name__ == '__main__':
    main()


# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

def solve(l1, r1, l2, r2):
    if r1 < l2 or r2 < l1:
        return None
    elif l1 < l2 and r1 < r2:
        return [l2, r1]
    elif l1 >= l2 and r1 <= r2:
        return [l1, r1]
    elif l1 <= l2 and r2 <= r1:
        return [l2, r2]
    else:
        return [l1, r2]


def main():
    n, m = list(map(int, input().split()))
    ans = [1, n]
    for _ in range(m):
        l, r = list(map(int, input().split()))
        # print(l, r)
        ans = solve(ans[0], ans[1], l, r)
        # print(ans)
        if ans is None:
            print((0))
            exit(0)
    print((ans[1] - ans[0] + 1))

if __name__ == '__main__':
    main()
# Original solution
# coding: UTF-8
# sample code

v = list(map(int, input().split()))
n, m = v[0], v[1]

l, r = [], []
for i in range(m):
	v = list(map(int, input().split()))
	l.append(v[0])
	r.append(v[1])

num = 0
for i in range(n):
	flag = 0
	for j in range(m):
		if not((l[j]<=i+1) and (i+1<=r[j])):
			flag += 1
	if flag == 0:
		num += 1

print(num)

# Optimized solution
# coding: UTF-8
# sample code

v = list(map(int, input().split()))
n, m = v[0], v[1]

l, r = [], []
for i in range(m):
	v = list(map(int, input().split()))
	l.append(v[0])
	r.append(v[1])

min = 1
max = n
for i in range(m):
	if l[i] > min:
		min = l[i]
	if max > r[i]:
		max = r[i]

num = max-min+1
if num < 0:
	print((0))
else:
	print(num)
# Original solution
n , m=list(map(int,input().split()))
l1 , r1 =list(map(int,input().split()))
A =set(range(l1,r1+1))

for i in range(m-1):
    l , r =list(map(int,input().split()))
    B = set(range(l,r+1))
    A = A.intersection(B)
print((len(A)))

# Optimized solution
n , m = list(map(int,input().split()))
L = []
R = []
for i in range(m):
    l , r = list(map(int,input().split()))
    L.append(l)
    R.append(r)
Lmax = max(L)
Rmin = min(R)
s = list(range(Lmax,Rmin+1))
print((len(s)))
# Original solution
li = input().split()
n = int(li[0])
m = int(li[1])
# IDカードn枚，　ゲートm個
l_r = []
# i番目のゲートはl~rのIDカードを持っていれば通過できる
l_r = [input().split() for i in range(m)]
answer = 0
for i in range(n):
    for lr in l_r:
        l = int(lr[0])
        r = int(lr[1])
        if l <= i + 1 and i + 1 <= r:
            continue
        else:
            break
    else:
        answer += 1
print(answer)

# Optimized solution
li = input().split()
n = int(li[0])
m = int(li[1])
# IDカードn枚，　ゲートm個
# i番目のゲートはl~rのIDカードを持っていれば通過できる
l_r = [input().split() for i in range(m)]
l_r.sort(key=lambda x:int(x[0]))
l_max = int(l_r[-1][0])
l_r.sort(key=lambda x:int(x[1]))
r_min = int(l_r[0][1])
answer = 0
# for i in range(n):
#     if l_max <= i + 1 and i + 1 <= r_min:
#         answer += 1
diff = r_min - l_max + 1
if diff > 0:
    print(diff)
else:
    print((0))
# Original solution
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 _pf(s): return print(s, flush=True)


N, M = LI()
LR = []

for i in range(M):
    LR.append(LI())

#低い方と高い方を求める
lower = None
found = False
for i in range(1,N+1):
    if found:
        break
    for lr in LR:
        if not (lr[0] <= i <= lr[1]):
            break
    else:
        lower = i
        found = True

if lower == None:
    print(0)
    exit()
#  print('lower', lower)
if lower == N:
    print(1)
    exit()

upper = None
found = False
for i in reversed(range(lower+1, N+1)):
    if found:
        break
    for lr in LR:
        if not (lr[0] <= i <= lr[1]):
            break
    else:
        upper = i
        found = True
if upper == None:
    upper = lower
#  print('upper', upper)
print(upper - lower + 1)




# Optimized solution
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 _pf(s): return print(s, flush=True)


N, M = LI()
LR = []

for i in range(M):
    LR.append(LI())

left, right = 0, inf
for lr in LR:
    left = max(left, lr[0])
    right = min(right, lr[1])
#  print(left, right)
if left > right:
    print(0)
else:
    print(right - left + 1)
# Original solution
N, M = list(map(int, input().split()))
L = [None] * M
R = [None] * M

ans = [0] * (N + 1)

for i in range(M):
  L[i], R[i] = list(map(int, input().split()))

for i in range(M):
  for j in range(L[i], R[i] + 1):
    ans[j] += 1


print((ans.count(M)))


# Optimized solution
N, M = list(map(int, input().split()))
L = [None] * M
R = [None] * M

for i in range(M):
  L[i], R[i] = list(map(int, input().split()))

ans = list(range(max(L), min(R) + 1))


print((len(ans)))
# Original solution
N,M=list(map(int,input().split()))
LR=list(range(N+1))
for i in range(M):
    L,R=list(map(int,input().split()))
    LR=list(set(list(range(L,R+1))) & set(LR))
print((len(LR)))

# Optimized solution
N,M=list(map(int,input().split()))
LR=set(range(N+1))
l=[]
r=[]
for i in range(M):
    L,R=list(map(int,input().split()))
    l.append(L)
    r.append(R)
ans=min(r)-max(l)+1
if ans<0:
    ans=0
print(ans)
# Original solution
inputted = list(map(int, input().split()))
N = inputted[0]
M = inputted[1]

LR = [list(map(int, input().split())) for i in range(M)]

left = 0
right = N
for LRi in LR:
    left = max(left, LRi[0])
    right = min(right, LRi[1])

    if left > right: break
    
answer = max(0, right - left + 1)
print(answer)


# Optimized solution
inputted = list(map(int, input().split()))
N = inputted[0]
M = inputted[1]

LR = [list(map(int, input().split())) for i in range(M)]

left = 1
right = N
for LRi in LR:
    left = max(left, LRi[0])
    right = min(right, LRi[1])

    if left > right: break
    
answer = max(0, right - left + 1)
print(answer)
# Original solution
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var, sep = ' ', end = '\n'):
    print(*var, file=sys.stderr, end = end, sep = sep)

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

n, m = I()
a = [0] * (n + 10)
for i in range(m):
    x, y = I()
    a[x] += 1
    a[y + 1] -= 1
ans = 0
for i in range(1, n + 10):
    a[i] = a[i] + a[i - 1]
    if a[i] == m:
        ans += 1
print(ans)


# Optimized solution
def mi():
    return list(map(int, input().split()))
'''
3 4
*G*S
G**S
*G*S
'''
n,m = mi()
l = -1
r = 1e10
for i in range(m):
    a,b=mi()
    if l>b or b<a:
        print((0))
        exit()
    l = max(l,a)
    r = min(r,b)
if r<l:
    print((0))
else:
    print((r-l+1))
# Original solution
n, m = list(map(int, input().split()))
ans_set = {x for x in range(1, n+1)}

for _ in range(m):
    a, b = list(map(int, input().split()))
    sub_set = {x for x in range(a, b+1)}
    ans_set = ans_set & sub_set
    
ans = len(ans_set)
print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
l = [0]*m
r = [0]*m

for i in range(m):
    l[i], r[i] = list(map(int, input().split()))
    

    
ans = min(r) - max(l)
if ans >= 0 :
    print((ans+1))
else:
    print((0))
# Original solution
N,M = list(map(int,input().split()))

#list_aaa = []
#
#for i in range(M):
#    L,R = map(int,input().split())
#    aaa = set(range(L,R+1))
#    list_aaa.append(aaa)
#print(list_aaa)

#and_set = list_aaa[0]

for i in range(M):
    L,R = list(map(int,input().split()))
    aaa = set(range(L,R+1))
    if i == 0:
        and_set = aaa
    else:
        and_set = and_set.intersection(aaa)


print((len(and_set)))



# Optimized solution
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)
L_max = max(L_list)
R_min = min(R_list)

#print(range(L_max,R_min+1))
print((len(list(range(L_max,R_min+1)))))
# Original solution
N, M = list(map(int, input().split()))
card = False
for _ in range(M):
    L, R = list(map(int, input().split()))
    if card is not False:
        card = card & set([i for i in range(L, R + 1)])
    else:
        card = set([i for i in range(L, R + 1)])
print((len(card)))

# Optimized solution
N, M = list(map(int, input().split())) 
left = 1
right = N
for _ in range(M):
    L, R = list(map(int, input().split()))
    if left < L:
        left = L
    if R < right:
        right = R
    if left > right:
        print((0))
        exit()
print((right - left + 1))
# Original solution
n,m=input().split()
n=int(n)
m=int(m)
i=1
x=list()
y=list()
ans=0
g=0
i=i+1
l,r=input().split()
l=int(l)
r=int(r)
while l != r + 1:
     x.append(l)
     l = l + 1

i=1

while m!=i:
    i=i+1
    l,r=input().split()
    l=int(l)
    r=int(r)
    while l != r + 1:
        y.append(l)
        l = l + 1

    while g!=len(x):
        v=x[g] in y
        if v==False:
            x.remove(x[g])
        else:
            g=g+1
    g=0
    y=[]



print((len(x)))

# Optimized solution
n,m=input().split()
n=int(n)
m=int(m)
i=0
x=list()
y=list()
ans=0
g=0
ll=[]
rr=[]


while i!=m:
    i=i+1
    l,r=input().split()
    l=int(l)
    r=int(r)
    ll.append(l)
    rr.append(r)
l=max(ll)
r=min(rr)

ans=r-l+1
if l>r:
    ans=0
print(ans)
# Original solution
n, m = list(map(int, input().split()))
x = list()
for i in range(m):
    temp = list(map(int, input().split()))
    tempall = set()
    for j in range(temp[0],temp[1]+1):
        tempall.add(j)
    x.append(tempall)

ans = set(x[0])
for i in range(m):
    ans = ans & x[i]

print((len(ans)))

# Optimized solution
n, m = list(map(int, input().split()))
l = [1,n]
for i in range(m):
    temp = list(map(int, input().split()))
    l[0] = max(l[0],temp[0])
    l[1] = min(l[1],temp[1])

print((int(max(l[1]-l[0]+1, 0))))
# Original solution
n, m = list(map(int, input().split()))

l, r = list(map(int, input().split()))
ID = list(range(l, r + 1))
for _ in range(m - 1):
    l, r = list(map(int, input().split()))
    if r < min(ID):
        print((0))
        exit()
    elif max(ID) < l:
        print((0))
        exit()
    elif min(ID) >= l and max(ID) >= r:
        l = min(ID)
        ID.clear()
        ID.extend(list(range(l, r + 1)))
    elif min(ID) < l and max(ID) >= r:
        ID.clear()
        ID.extend(list(range(l, r + 1)))
    elif min(ID) < l and max(ID) < r:
        r = max(ID)
        ID.clear()
        ID.extend(list(range(l, r + 1)))

print((len(ID)))


# Optimized solution
n, m = list(map(int, input().split()))

l, r = list(map(int, input().split()))
for _ in range(m - 1):
    li, ri = list(map(int, input().split()))
    if ri < l:
        print((0))
        exit()
    elif r < li:
        print((0))
        exit()
    elif l >= li and r >= ri:
        r = ri
    elif l < li and r >= ri:
        l, r = li, ri
    elif l < li and r < ri:
        l = li

print((r - l + 1))
# Original solution

n,m=list(map(int,input().split()))
zahyo=[0]*m
for i in range(m):
    zahyo[i]=list(map(int,input().split()))

collec_lis=[]
for i in range(m):
    collec_lis.extend(list(range(int(zahyo[i][0]),int(zahyo[i][1])+1)))
    
import collections
c = collections.Counter(collec_lis)

kyotu=[int(i) for i in list(c.values()) if i==m]
print((len(kyotu)))

# Optimized solution

# 左右の範囲を絞っていくことで共通要素のみを炙り出す
n,m = list(map(int,input().split()))
l = -1
r = n+1
for i in range(m):
    l_i,r_i = list(map(int,input().split()))
    l = max(l,l_i)
    r = min(r,r_i)
print((max(0,r-l+1)))
# Original solution
n,m=list(map(int,input().split()))
memo=[]
l,r=list(map(int,input().split()))
for i in range(r-l+1):
    memo.append(l+i)
for v in range(m-1):
    memo1=[]
    l,r=list(map(int,input().split()))
    for k in range(len(memo)):
        if l<=memo[k]<=r:
            memo1.append(memo[k])
    memo=memo1
print((len(memo)))

# Optimized solution
n,m=list(map(int,input().split()))
l=[]
r=[]
for i in range(m):
    l1,r1=list(map(int,input().split()))
    l.append(l1)
    r.append(r1)
print((0 if min(r)-max(l)+1<0 else min(r)-max(l)+1))
# Original solution
N, M = [int(n) for n in input().split()]
A = [1 for _ in range(N)]

for _ in range(M):
    L, R = [int(n) for n in input().split()]
    for i in range(0, L-1, 1):
        A[i] = 0
    for i in range(R, N, 1):
        A[i] = 0

cnt = 0
for i in range(N):
    if A[i] == 1:
        cnt += 1

print(cnt)


# Optimized solution
N, M = [int(n) for n in input().split()]
left, right = 1, N

for _ in range(M):
    L, R = [int(n) for n in input().split()]
    if left < L:
        left = L
    if R < right:
        right = R

if left <= right:
    print((right - left + 1))
else:
    print((0))
# Original solution
import copy
N,M=list(map(int,input().split()))
D=[]
for _ in range(M):
    if _==0:
        LR=list(map(int,input().split()))
        for u in range(LR[0],LR[1]+1):
            D.append(u)
    else:
        LR=list(map(int,input().split()))
        DD=copy.deepcopy(D)
        for v in DD:
            if v not in list(range(LR[0],LR[1]+1)):
                D.remove(v)
print((len(D)))

# Optimized solution
N,M=list(map(int,input().split()))
LR=list(list(map(int,input().split())) for _ in range(M))
L=1
R=N
for i in range(M):
    L=max(L,LR[i][0])
    R=min(R,LR[i][1])
print((R-L+1 if L<=R else 0))
# Original solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))

ans = set(range(l, r + 1))
for _ in range(m - 1):
    l, r = list(map(int, input().split()))
    ans &= set(range(l, r + 1))

print((len(ans)))

# Optimized solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))

for _ in range(m - 1):
    a, b = list(map(int, input().split()))
    l = max(l, a)
    r = min(r, b)

print((max(0, r - l + 1)))
# Original solution
import sys

input = sys.stdin.readline

n, m = list(map(int, input().split()))

l_first, r_first = list(map(int, input().split()))

answer_set = {value for value in range(l_first, r_first + 1)}

for _ in range(m - 1):
    l_current, r_current = list(map(int, input().split()))
    current_set = {value for value in range(l_current, r_current + 1)}
    answer_set.intersection_update(current_set)

print((len(answer_set)))


# Optimized solution
import sys

input = sys.stdin.readline

n, m = list(map(int, input().split()))

l_list = []
r_list = []

for _ in range(m):
    l, r = list(map(int, input().split()))
    l_list.append(l)
    r_list.append(r)

l_maybe_answer = max(l_list)
r_maybe_answer = min(r_list)

if l_maybe_answer <= r_maybe_answer:
    print((min(r_list) - max(l_list) + 1))
else:
    print((0))
# Original solution
def main():
    N, M = list(map(int, input().split()))
    box = []
    for i in range(M):
        LR = [j for j in map(int, input().split())]
        box += LR

    # for i in range(M):
    #     L, R = map(int, input().split())
    #     box.append(L)
    #     box.append(R)

    # print(box)
    num = 0
    for j in range(1, N+1):
        plus = 1
        # print(j)
        if plus == 1:
            for i in range(0, len(box), 2):
                # print(box[i], box[i+1])
                if j < box[i] or box[i+1] < j:
                    plus = 0
                    break

            num += plus

    print(num)


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    N, M = list(map(int, input().split()))

    L = []
    R = []
    for i in range(M):
        l, r = list(map(int, input().split()))
        L.append(l)
        R.append(r)

    Lout = max(L)
    Rout = min(R)
    ans = Rout - Lout+1
    if ans < 0:
        print((0))
    else:
        print(ans)


if __name__ == "__main__":
    main()
# Original solution
def doqueri(S,N,L,R):
	s = S[L-1:R]
	l = [0] * (L-1)
	r = [0] * (N-R)

	return l+s+r

if __name__ == '__main__':
	N, M = list(map(int, input().split()))
	S = [1] * N
	for _ in range(M):
		L,R = list(map(int, input().split()))
		S = doqueri(S[:],N,L,R)
	print((S.count(1)))

# Optimized solution
if __name__ == '__main__':
	N, M = list(map(int, input().split()))
	lmax = 0
	rmin = N
	for _ in range(M):
		L,R = list(map(int, input().split()))
		if L > lmax:
			lmax = L
		if R < rmin:
			rmin = R
	ans = rmin - lmax + 1
	if ans < 0:
		ans = 0

	print(ans)
# Original solution
N, M = list(map(int, input().split()))

stepper = 0
gate = []

for j in range(M):
    gate_1, gate_2 = list(map(int, input().split()))
    gate.append([gate_1, gate_2])


def numJudge(arr, num):
    if arr[0] <= num <= arr[1]:
        return 1
    else:
        return 0


def judge(num):
    num += 1
    ctr = 0
    for k in range(M):
        if gate[k][0] <= num <= gate[k][1]:
            ctr += 1
    if ctr == M:
        return 1
    else:
        return 0


ans = list(map(judge, list(range(N))))
print((sum(list(ans))))


# Optimized solution
N, M = list(map(int, input().split()))

stepper = 0
gate = []

for j in range(M):
    gate_1, gate_2 = list(map(int, input().split()))
    gate.append([gate_1, gate_2])

gate_min = 0
gate_max = N

for m in range(M):
    if gate[m][0] > gate_min:
        gate_min = gate[m][0]

    if gate[m][1] < gate_max:
        gate_max = gate[m][1]

#print(gate_min, gate_max)
for k in range(N):
    k = k+1
    if gate_min <= k <= gate_max:
        stepper += 1

print(stepper)
# Original solution
n, m = list(map(int, input().split()))
l, r = [0]*m, [0]*m
for i in range(m):
    l[i], r[i] = list(map(int, input().split()))

ans = [0]*n
for i in range(m):
    memo = [0]*n
    for j in range(l[i]-1, r[i]):
        memo[j] = 1        
    for k in range(n):
        ans[k] += memo[k]

print((ans.count(m)))

# Optimized solution
n, m = list(map(int, input().split()))
l, r = [0]*m, [0]*m
for i in range(m):
    l[i], r[i] = list(map(int, input().split()))

ll = max(l)
rr = min(r)
if rr-ll >= 0:
    print((rr-ll+1))
else:
    print((0))
# Original solution
n, m = list(map(int, input().split()))
array = [list(map(int, input().split())) for i in range(m)]
result = set([i for i in range(1, n+1)])
for el in array:
    L, R = el[0], el[1]
    work = set([i for i in range(L, R+1)])
    result = work & result
print((len(result)))

# Optimized solution
n, m = list(map(int, input().split()))
array = [list(map(int, input().split())) for i in range(m)]
result = [1, n]
for el in array:
    if el[0] > result[1] or result[0] > el[1]:
        result = [1, 0]
        break
    else:
        work = sorted(result + el)
        result = [work[1], work[2]]
print((result[1]-result[0]+1))
# Original solution
n,m=list(map(int,input().split()))
l,r=list(map(int,input().split()))
a=set(range(l,r+1))
for i in range(m-1):
    l,r=list(map(int,input().split()))
    a.intersection_update(list(range(l,r+1)))
print((len(a)))


# Optimized solution
n,m=list(map(int,input().split()))
ll,rr=0,n
for i in range(m):
    l,r=list(map(int,input().split()))
    ll=max(l,ll)
    rr=min(r,rr)
print((max(rr-ll+1,0)))
# Original solution
N,M = list(map(int,input().split()))
 
listA=[]
while True:
    try:
        listA.append(list(map(int,input().split())))
    except:
        break;
    
count = 0
for i in range(1,N+1):
  for j in range(M):
    if  i < listA[j][0] or listA[j][1] < i:
      break;
    if j == M-1:
      count += 1
print(count)

# Optimized solution
N,M = list(map(int,input().split()))

listA=[]
while True:
    try:
        listA.append(list(map(int,input().split())))
    except:
        break;
L=[]
R=[]
for i in listA:
  L.append(i[0])
  R.append(i[1])
maxL = max(L)
minR = min(R)

count = 0
for i in range(1,N+1):
  if maxL <= i <= minR:
    count+=1
print(count)
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, m = list(map(int, readline().split()))
lr = [list(map(int, readline().split())) for _ in range(m)]

id_list = [i for i in range(lr[0][0], lr[0][1] + 1)]
for i in range(1, m):
    x = 1
    id_list = [j for j in id_list if lr[i][0] <= j <= lr[i][1]]
print((len(id_list)))

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, m = list(map(int, readline().split()))
lr = [list(map(int, readline().split())) for _ in range(m)]

left = 0
right = n
for i in range(m):
    left = max(left, lr[i][0])
    right = min(right,lr[i][1])
print((max(0,right-left+1)))
# Original solution
N, M = list(map(int, input().split()))

K = set(range(1, N + 1))
for i in range(M):
    l, r = list(map(int, input().split()))
    K = K & set(range(l, r + 1))

print((len(K)))

# Optimized solution
N, M = list(map(int, input().split()))
L = 1
R = N

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