CodeEff/ECCO huggingface dataset:
# Original solution
import copy

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

ls = []

for i in range(M):
    L, R = list(map(int,input().split()))
    l = [k+L for k in range(R-L+1)]
    ls.append(l)
    
if M == 1:
    print((len(l)))
else:
    for i in range(M-1):
        l_set = set(ls[i]) & set(ls[i+1])
        if len(l_set) == 0:
            break
        ls[i+1] = copy.deepcopy(l_set)

    print((len(l_set)))

# Optimized solution
import copy

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

if M == 1:
    L, R = list(map(int,input().split()))
    l = [k+L for k in range(R-L+1)]
    print((len(l)))
else:
    L, R = list(map(int,input().split()))
    l = [k+L for k in range(R-L+1)]
    l_tmp = copy.deepcopy(l)

    L, R = list(map(int,input().split()))
    l = [k+L for k in range(R-L+1)]

    l_set = list(set(l) & set(l_tmp))
    l_tmp = copy.deepcopy(l)
    
    for i in range(M-2):
        L, R = list(map(int,input().split()))
        l = [k+L for k in range(R-L+1)]

        l_set = list(set(l) & set(l_set))
        l_tmp = copy.deepcopy(l)
        
        if len(l_set) == 0:
            break

    print((len(l_set)))
# Original solution
import sys
import copy

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

Ls, Rs = [0 for _ in range(M)],  [0 for _ in range(M)]

for i in range(M):
    L, R = list(map(int,input().split()))
    Ls[i] = L
    Rs[i] = R
    
    if i != 0:
        if Rs[i-1] < Ls[i]:
            print((0))
            sys.exit( )
ls = []

for i in range(M):
    l = [k+Ls[i] for k in range(Rs[i]-Ls[i]+1)]
    ls.append(l)
    
if M == 1:
    print((len(l)))
else:
    for i in range(M-1):
        l_set = set(ls[i]) & set(ls[i+1])
        ls[i+1] = copy.deepcopy(l_set)

    print((len(l_set)))

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

Ls, Rs = [0 for _ in range(M)],  [0 for _ in range(M)]

for i in range(M):
    L, R = list(map(int,input().split()))
    Ls[i] = L
    Rs[i] = R
    
L_prime = max(Ls)
R_peime = min(Rs)

if L_prime > R_peime:
    print((0))
else:
    print((R_peime - L_prime + 1))
# Original solution
n, m = list(map(int, input().split()))
n = set([i for i in range(1,n+1)])

for i in range(m):
    a, b = list(map(int, input().split()))
    ab = set([j for j in range(a,b+1)])
    n = n & ab
    if len(n) == 0:
        print((0))
        exit()

print((len(n)))

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

left = 1
right = n

for i in range(m):
    a, b = list(map(int, input().split()))
    if a > left:
        left = a
    if b < right:
        right = b
    
    if left > right:
        print((0))
        exit()

print((right-left+1))
# Original solution
N, M = list(map(int, input().split()))
L, R = list(map(int, input().split()))
CARDS = {i for i in range(L,R+1)}

for i in range(M-1):
    L, R = list(map(int, input().split()))
    CARDS &= {card for card in range(L,R+1)}
    if CARDS == set():
        break

print((len(CARDS)))

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

for _ in range(M):
    L, R = list(map(int, input().split()))
    Ls.add(L)
    Rs.add(R)

print((len([i for i in range(max(Ls), min(Rs)+1)])))
# Original solution
#!/usr/bin/env python3
import sys

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

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

Ans = set(range(L[0], R[0] + 1))
for i in range(1, len(L)):
    Ans = set(range(L[i], R[i] + 1)) & Ans
    if not Ans:
        print((0))
        sys.exit(0)

print((len(Ans)))


# Optimized solution
#!/usr/bin/env python3
import sys

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

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

Ans = min(R) - max(L) + 1
if Ans < 0:
    Ans = 0
print(Ans)
# Original solution
N,M = list(map(int,input().split()))
for i in range(M):
    l,r = list(map(int,input().split()))
    if i == 0:
        set_a = set(list(range(l,r+1)))
    set_b = set(list(range(l,r+1)))
    set_a = set_a & set_b
print((len(set_a)))

# Optimized solution
N,M = list(map(int,input().split()))
lst_l=[]
lst_r=[]
for i in range(M):
    l,r = list(map(int,input().split()))
    lst_l.append(l)
    lst_r.append(r)
ans = min(lst_r)-max(lst_l)+1
if ans >=0:print(ans)
else:print((0))
# Original solution
n,m = list(map(int,input().split()))
l = []
r = []
for i in range(m):
    a,b = list(map(int,input().split()))
    l.append(a)
    r.append(b)
count = 0
for i in range(1,n+1):
        if l[m-1] <= i <= r[0]: 
            count += 1
for i in range(n):
    for j in range(m):
        l.append(1)
print(count)

# Optimized solution
n,m = list(map(int,input().split()))
l = []
r = []
for i in range(m):
    a,b = list(map(int,input().split()))
    l.append(a)
    r.append(b)
count = 0
ma = max(l)
mi = min(r)
for i in range(1,n+1):
        if ma <= i <= mi: 
            count += 1
print(count)
# Original solution
n,m = list(map(int,input().split()))
l,r = [],[]
for i in range(m):
    a,b = list(map(int,input().split()))
    l.append(a)
    r.append(b)
count = 0
for i in range(1,n+1):
    for j in range(m):
        if i < l[j] or i > r[j]:
            count -= 1
            break
print((n + count)) 

# Optimized solution
n,m = list(map(int,input().split()))
l,r = [],[]
for i in range(m):
    a,b = list(map(int,input().split()))
    l.append(a)
    r.append(b)
num = list(range(1,n+1))
lmax = max(l)
rmin = min(r)
if lmax > rmin:
    print((0))
else:
    gate = set(range(lmax,rmin+1))
    ans = list(set(num) & gate)
    print((len(ans)))
# Original solution
import fractions
n, m = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    x, y = list(map(int, input().split()))
    l.append(x)
    r.append(y)
ans = 0
for i in range(1,n+1):
    if max(l) <= i and i <= min(r):
        ans += 1
print(ans) 

# Optimized solution
import fractions
n, m = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    x, y = list(map(int, input().split()))
    l.append(x)
    r.append(y)
ans = 0
M = max(l)
m = min(r)
for i in range(1,n+1):
    if M <= i and i <= m:
        ans += 1
print(ans)
# Original solution
n, m = list(map(int, input().split()))
card = []
cntAppearance = []
for i in range(n):
        cntAppearance.append(0)
for _ in range(m):
        l ,r = list(map(int, input().split()))
        for i in range(l,r+1):
                card.append(i)
for j in card:
        cntAppearance[j-1] += 1
cnt = 0
for i in cntAppearance:
        if i == m:
                cnt += 1
print(cnt)

# Optimized solution
n, m = list(map(int, input().split()))
cnt = 0
id = []
for _ in range(m):
        l= list(map(int, input().split()))
        if len(id) == 0:
                id = l
        else:
                if id[0] < l[0]:
                        id[0] = l[0]
                elif id[1] > l[1]:
                        id[1] = l[1]
if id[1] < id[0]:
        cnt = 0
else:
        cnt = id[1]-id[0]+1
print(cnt)
# Original solution
n,m=list(map(int,input().split()))
lr=[list(map(int,input().split())) for i in range(m)]
a=set(range(lr[0][0],lr[0][1]+1))
for j in range(1,m):
  a=a&set(range(lr[j][0],lr[j][1]+1))
print((len(a)))

# Optimized solution
n,m=list(map(int,input().split()))
l,r=list(map(int,input().split()))
for i in range(m-1):
  x,y=list(map(int,input().split()))
  l=max(l,x)
  r=min(r,y)
print((r-l+1 if l<=r else(0)))
# Original solution
n, m = list(map(int, input().split()))
table = []
for i in range(m):
    nest_table = []
    l, r = list(map(int, input().split()))
    nest_table.append(l)
    nest_table.append(r)
    table.append(nest_table)

ans = 0 
for j in range(n):
    for k in range(m):
        frag = 0
        if j+1 >= table[k][0] and j+1 <= table[k][1]:
            pass
        else:
            frag += 1
            break
    if frag == 0:
        ans += 1
print(ans)   

# Optimized solution
n, m = list(map(int, input().split()))
lower = 1
upper = 100000
for i in range(m):
    l, r = list(map(int, input().split()))
    if l >= lower:
        lower = l
    if r <= upper:
        upper = r
        
if lower > upper:
    print((0))
else:    
    if n < upper:
        upper = n
    print((upper - lower + 1))
# Original solution
N, M = list(map(int, input().split()))
l, r = list(map(int, input().split()))

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

print((len(s)))

# Optimized solution
N, M = list(map(int, input().split()))
l_max = -1
r_min = N + 1
for m in range(M):
    l, r = list(map(int, input().split()))
    if l > l_max:
        l_max = l
    if r < r_min:
        r_min = r
print((max(r_min - l_max + 1, 0)))
# Original 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()))
    

d={}

cnt = 0

for i in range(M):
    l = L[i]
    r=R[i]
    
    for j in range(l,r+1):
        
        if j in d:
            d[j] += 1
        else:
            d[j] = 1
            
        if d[j] >= M:
            cnt+=1
    
print(cnt)    

# 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()))
    
L.sort()
R.sort()

l= L[len(L)-1]
r = R[0]

cnt =0 

for i in range(l,r+1):
    cnt+=1

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

print((flag.count(m)))

# Optimized solution
n,m=list(map(int,input().split()))
ansl=1
ansr=n
flag=0
for i in range(m):
    l,r=list(map(int,input().split()))
    if (ansl>r)or(ansr<l):
        flag=1
        break
    if ansl<l:
        ansl=l
    if ansr>r:
        ansr=r
if flag==0:
    print((ansr-ansl+1))
else:
    print((0))
# Original solution
n,m=list(map(int,input().split()))
cards=[i for i in range(n)]
for j in range(m):
    l,r=list(map(int,input().split()))
    for i in range(l-1):
        cards[i]=-1
    for i  in range(r,n):
        cards[i]=-1

ans=sum(1 for cars in cards if cars>=0)
print(ans)

# Optimized solution
n,m=list(map(int,input().split()))
l=[]
r=[]
for j in range(m):
    templ,tempr=list(map(int,input().split()))
    l.append(templ)
    r.append(tempr)
ans=min(r)-max(l)+1
if ans<0:
    ans=0
print(ans)
# Original solution
n,m=list(map(int,input().split()))
l=[list(map(int,input().split())) for _ in range(m)]
ans=0
for i in range(l[0][0],l[0][1]+1):
    if all([i in range(l[k][0],l[k][1]+1) for k in range(1,m)]):
        ans+=1
print(ans)

# Optimized solution
n,m=list(map(int,input().split()))
l=[list(map(int,input().split())) for _ in range(m)]
a=0
z=10**5
for i in range(m):
    a=max(a,l[i][0])
    z=min(z,l[i][1])
if a<=z:
    print((z-a+1))
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))
L = [0] * M
R = [0] * M
ID = [0] * N
N = {i for i in range(1, N + 1)}
for i in range(M):
    L[i], R[i] = list(map(int, input().split()))
    N = N.intersection({j for j in range(L[i], R[i] + 1)})

print((len(N)))

# 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()))
LL = max(L)
RR = min(R)
ans = 0
for i in range(N):
    if LL <= i + 1 <= RR:
        ans += 1
    else:
        pass
print(ans)
# Original solution
n,m=list(map(int, input().split()))
s=set(range(1, n+1)) #1~nのset
for i in range(m):
  l,r=list(map(int, input().split()))
  s= s & set(range(l, r+1))
print((len(s)))

# Optimized solution
n,m=list(map(int, input().split()))
lmax=0
rmin=1000000
for i in range(m):
  l,r=list(map(int, input().split()))
  lmax=max(l, lmax)
  rmin=min(r, rmin)

if rmin-lmax>=0:
  print((rmin-lmax+1))
else:
  print((0))
# Original solution
from collections import Counter
n, m = list(map(int, input().split()))
ll = []

for i in range(m):
    l, r = list(map(int, input().split()))
    ll.extend(list(range(l,r+1)))

ans = 0
c = Counter(ll)
for k, v in list(c.items()):
    if v == m:
        ans = ans + 1
print(ans)


# Optimized solution
from collections import Counter
n, m = list(map(int, input().split()))
ll = []
x = 0
y = 10 ** 5

for i in range(m):
    l, r = list(map(int, input().split()))
    x, y = max(x, l), min(y, r)

s = (y - x) + 1
if s < 0:
    s = 0
print(s)
# Original solution
n,m=list(map(int,input().split()))
lr = [list(map(int,input().split())) for _ in range(m)]
s = {i:0 for i in range(1,n+1)}
cnt = 0
for i in range(m):
    for j in range(lr[i][0],lr[i][1]+1):
        s[j] += 1
for v in list(s.values()):
    if v == m:cnt += 1
print(cnt)

# 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()))
print((max(min(r) - max(l) + 1, 0)))
# Original solution
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
input = sys.stdin.readline
 
# bisect_left(lists, 3)
# bisect_right(lists, 3)
 
def main():
	N, M = list(map(int, input().split(" ")))
	#N = int(input())
	#S = input()

	LR = []
	min_i = -1
	max_i = -1
	for i in range(M):
		L, R = list(map(int, input().split(" ")))
		LR.append((L, R))
		if i == 0:
			min_i = L
			max_i = R
		else:
			if L < min_i:
				min_i = L
			if max_i < R:
				max_i = R

	ans = 0
	for i in range(min_i, max_i+1):
		flg = True
		for j in LR:
			L, R = j

			if i <= R and i >= L:
				flg = True
			else:
				flg = False
				break
		if flg == True:
			#print("sss")
			ans += 1

	print(ans)


if __name__ == "__main__":
	main()

# Optimized solution
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
input = sys.stdin.readline
 
# bisect_left(lists, 3)
# bisect_right(lists, 3)
 
def main():
	N, M = list(map(int, input().split(" ")))
	#N = int(input())
	#S = input()

	LR = []
	min_i = -1
	max_i = -1
	min_a = -1
	max_a = -1
	for i in range(M):
		L, R = list(map(int, input().split(" ")))
		LR.append((L, R))
		if i == 0:
			min_i = L
			max_i = R
			min_a = L
			max_a = R
		else:
			if L > min_i:
				min_i = L
			if max_i > R:
				max_i = R

	ans = 0
	for i in range(min_i, max_i+1):
		ans += 1

	print(ans)


if __name__ == "__main__":
	main()
# Original solution
N, M = list(map(int, input().split()))
st = set(range(1, N+1))
for _ in range(M):
    a, b = list(map(int, input().split()))
    st.intersection_update(set(range(a, b+1)))
print((len(st)))

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

for i in range(M):
  l1 = list(range(l[i][0],l[i][1]+1))
  if i == 0:
    valid = l1
  valid = set(valid) & set(l1)
print((len(valid)))



# Optimized solution
N,M = list(map(int,input().split()))
l = [list(map(int,input().split())) for _ in range(M)]

for e in l:
  L,R = e[0],e[1]
  if e == l[0]:
    _L,_R = L,R
  else:
    if _L < L:
      _L = L
    if _R > R:
      _R = R
if _R - _L >= 0:
  print((_R - _L + 1))
else:
  print((0))
# Original solution
Ns,Ms=input().split()
N=int(Ns)
M=int(Ms)
LR=input().split()
ans=set(range(int(LR[0]),int(LR[1])+1))
for i in range(M-1):
	LR=[]
	LR=input().split()
#	print("ssssssssss")
#	print(ans)
	ansy=set(range(int(LR[0]),(int(LR[1])+1)))
	ans=ans&ansy
print((len(ans)))


# Optimized solution
Ns,Ms=input().split()
N=int(Ns)
M=int(Ms)
lists=[]
ansmins,ansmaxs=input().split()
ansmin=int(ansmins)
ansmax=int(ansmaxs)
for i in range(M-1):
	tmpmin,tmpmax=input().split()
	if int(tmpmin)>ansmin:
		ansmin=int(tmpmin)
	if int(tmpmax)<ansmax:
		ansmax=int(tmpmax)

if (ansmax-ansmin+1)<0:
	ans=0
else:
	ans=ansmax-ansmin+1
print(ans)
# Original solution

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

ans = set(range(1,n+1))

for i in range(m):
    l,r = list(map(int,input().split()))
    x = set(range(l,r+1))
    ans = ans & x
    
print((len(ans)))
    

# Optimized solution

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

ans,ansf = 1,n

for i in range(m):
    l,r = list(map(int,input().split()))
    ans = max(ans,l)
    ansf = min(ansf,r)
    
if ansf - ans <0:
    print((0))
else:
    print((ansf-ans+1))
# Original solution
import sys
input=sys.stdin.readline

n,m=list(map(int,input().split()))
for i in range(m):
  a,b=list(map(int,input().split()))
  l=[i for i in range(a,b+1)]
  if i==0:
    cards=l
  else:
    cards=list(set(l)&set(cards))
print((len(cards)))

# Optimized solution
import sys
input=sys.stdin.readline

n,m=list(map(int,input().split()))
leftgate=[]
rightgate=[]
for i in range(m):
  l,r=list(map(int,input().split()))
  leftgate.append(l)
  rightgate.append(r)
if (min(rightgate)-max(leftgate))>-1:
  print((min(rightgate)-max(leftgate)+1))
else:
  print((0))
# Original solution
N, M = list(map(int,input().split()))
c = 0
l2 = []
a, b = list(map(int,input().split()))
l = list(range(a,b+1))  
for i in range(M-1):
  c, d = list(map(int,input().split()))
  l1 = list(range(c,d+1))  
  for j in l1:
    if j in l:
      l2.append(j)
  l = l2
  l2 = []
print((len(l)))
    
    

# Optimized solution
n, m = list(map(int,input().split()))
a, b = list(map(int,input().split()))
cnt = 0
for i in range(1, m):
  l, r = list(map(int,input().split()))
  if (l > b and r > b) or (l < a and r < a):
    print((0))
    exit()
  if a<=l<=b:
    a = l
  if r <= b:
    b = r
print((b-a+1))
# Original solution
n,m=list(map(int,input().split()))
an=[1]*n
for i in range(m):
    l,r=list(map(int,input().split()))
    for j in range(1,n+1):
        if l <= j <= r:
            pass
        else:
            an[j-1]=0

print((sum(an)))

# Optimized solution
n,m=list(map(int,input().split()))
lm=0
rm=10**9

for i in range(m):
    l,r=list(map(int,input().split()))
    lm=max(lm,l)
    rm=min(rm,r)

print((max(0,rm-lm+1)))
# Original solution
N, M= (int(i) for i in input().split())

ans = set(range(1,N+1))

for i in range(M):
    L_tmp, R_tmp = list(map(int, input().split()))
    ans = ans & set(range(L_tmp,R_tmp+1))

print((len(ans)))


# Optimized solution
N, M= (int(i) for i in input().split())

L_max = 1
R_min = N

for i in range(M):
    L, R = list(map(int, input().split()))
    L_max = max(L_max,L)
    R_min = min(R_min,R)

ans = set(range(L_max,R_min+1))

print((len(ans)))
# Original solution
n,m=list(map(int,input().split()))
a=[0 for i in range(n)]
for i in range(m):
  l,r=list(map(int,input().split()))
  for j in range(l,r+1):
    a[j-1]+=1
print((a.count(m)))

# Optimized solution
n,m=list(map(int,input().split()))
lll=[]
rrr=[]
for i in range(m):
  l,r=list(map(int,input().split()))
  lll.append(l)
  rrr.append(r)
print((max(0,min(rrr)-max(lll)+1)))
# Original solution
ans = 0
n, m = list(map(int, input().split()))
listL, listR = [], []

for i in range(m):
    l, r = list(map(int, input().split()))
    listL.append(l)
    listR.append(r)

for j in range(n):
    for i in range(m):
        if not (listL[i] <= j + 1 <= listR[i]):
            break
    else:
        ans += 1


print(ans)

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

for _ in range(m):
    tmpL, tmpR = list(map(int, input().split()))
    l = max(l, tmpL)
    r = min(r, tmpR)

print((max(0, r - l + 1)))
# Original solution
n, m = list(map(int, input().split()))
L = [list(map(int, input().split())) for _ in range(m)]
print((sum([all([l <= i+1 <= r for l, r in L]) for i in range(n)])))

# Optimized solution
n, m = list(map(int, input().split()))
L = [0]*m
R = [0]*m
for i in range(m): 
    *I, = list(map(int, input().split()))
    L[i] = I[0]
    R[i] = I[1]
print((max(min(R)-max(L)+1, 0)))
# Original solution
n, m = [int(x) for x in input().split()]
l, r = [int(x) for x in input().split()]
s1 = {int(x) for x in range(l, r+1)}
for i in range(m-1):
    l, r = [int(x) for x in input().split()]
    s2 = {int(x) for x in range(l, r+1)}
    s1 = s1 & s2
    
print((len(s1)))

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

L = max(l)
R = min(r)

if L <= R:
    print((R - L + 1))
else:
    print((0))
# Original solution
import sys
input=lambda :sys.stdin.readline().rstrip()
n, m = list(map(int, input().split()))

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

# Optimized solution
n, m = list(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))
# Original solution
n, m = list(map(int, input().split()))
vote = n * [0]
for i in range(m):
    l, r = list(map(int, input().split()))
    for j in range(l - 1, r):
        vote[j] += 1
print((vote.count(m)))


# Optimized solution
n, m = list(map(int, input().split()))
max_value = 10 ** 5
min_value = 1
for i in range(m):
    l, r = list(map(int, input().split()))
    min_value = max(min_value, l)
    max_value = min(max_value, r)

ans = max_value - min_value + 1
if ans < 0:
    ans = 0
print(ans)
# Original solution
N,M=list(map(int,input().split()))
L,R=list(map(int,input().split()))
A=list(range(L,R+1))
for i in range(1,M):
  L,R=list(map(int,input().split()))
  A=set(A)&set(range(L,R+1))
print((len(A)))

# Optimized solution
N,M=list(map(int,input().split()))
L,R=list(map(int,input().split()))
a,b=L,R
for i in range(1,M):
  L,R=list(map(int,input().split()))
  a=max(a,L)
  b=min(b,R)
if a>b:
  print((0))
else:
  print((b-a+1))
# Original solution
m, n = list(map(int, input().split()))
l = []
for i in range(n):
  a, b = list(map(int, input().split()))
  l.append(set(range(a,b+1)))
if n >= 2:
  for i in range(1,n):
    d = l[0].intersection(l[i])
else:
  d = l[0]
print((len(d)))

# Optimized solution
m, n = list(map(int, input().split()))
a, b = list(map(int, input().split()))
for i in range(n-1):
  c, d = list(map(int, input().split()))
  a = max(a,c)
  b = min(b,d)
if b - a >=0 :
  print((b-a +1))
else:
  print((0))
# Original solution
N, M = list(map(int, input().split()))
l, m = list(map(int, input().split()))
ans = set(list(range(l, m+1)))
for i in range(M-1):
    l, m = list(map(int, input().split()))
    ans = ans & set(list(range(l, m+1)))
print((len(ans)))
    

# Optimized solution
N, M = list(map(int, input().split()))
l = 1
m = N
for i in range(M):
    l_i, m_i = list(map(int, input().split()))
    l = max(l, l_i)
    m = min(m, m_i)
if m < l:
    print((0))
else:
    print((m - l + 1))
# Original solution
# ABC127 C - Prison
si = lambda: eval(input())
ni = lambda: int(eval(input()))
nm = lambda: list(map(int, input().split()))
nl = lambda: list(map(int, input().split()))
n,m=nm()
l,r=0,10000000

for i in range(m):
    cl,cr=nm()
    l=max(cl,l)
    r=min(cr,r)

if r-l>=0:
    print((r-l+1))
elif r-l<0:
    print((0))



# Optimized solution
n,m=list(map(int,input().split()))
t_max=10**9
t_min=0
for i in range(m):
  l,r=list(map(int,input().split()))
  t_min=max(l,t_min)
  t_max=min(r,t_max)
print((max(t_max-t_min+1,0)))
# Original solution
N, M = list(map(int, input().split()))

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

print((len(s)))


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

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

if left > right:
    print((0))
else:
    print((right - left + 1))
# Original solution
n, m = list(map(int, input().split()))
ans = set(range(1,n+1))
for i in range(m):
  l, r = list(map(int, input().split()))
  x = set(range(l, r+1))
  ans = ans & x
print((len(ans)))

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