CodeEff/ECCO huggingface dataset:
# Original 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)
ans = 0
for i in range(n+1):
    count = 0
    for j in range(m):
        if i>=L[j] and i <=R[j]:
            count+=1
        if count==m:
            ans +=1
print(ans)

# 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)
L.sort(reverse=True)
R.sort()
ans =0
for i in range(n+1):
    if i >=L[0] and i <=R[0]:
        ans +=1
print(ans)
# Original solution
N, M = list(map(int, input().split()))
b = [] * M
r = []
for i in range(M):
    x, y = list(map(int, input().split()))
    g = list(range(x, y + 1))
    b.append(g)
# print(b)
for i in range(M - 1):
    r = list(set(b[i]) & set(b[i + 1]))
    # print(r)
    b[i + 1] = r
if M >= 2:
    print((len(r)))

else:
    print((len(b[0])))


# Optimized solution
N, M = list(map(int, input().split()))
l = 0
r = 10000000
for i in range(M):
    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()))
A = [1]*N
for i in range(M):
    L, R = list(map(int,input().split()))
    for j in range(0,L-1):
        A[j] = 0
    for j in range(R,N):
        A[j] =0
print((sum(A)))

# 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-1)
    R.append(R1+1)
if min(R) - max(L) -1<0:
    print("0")
else:
    print((min(R)-max(L)-1))
# Original solution
N, M = list(map(int, input().split()))

num = [0] * N

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

ans = 0

for i in range(N):
    if num[i] == M:
        ans += 1

print(ans)


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

num = [0] * N

L = 1
R = N

for _ in range(M):
    l, r = list(map(int, input().split()))
    if l > L:
        L = l
    if r < R:
        R = r

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

li_w = list()

for _ in range(m):
    li_w.append(tuple(map(int, input().split())))

answer = set()

for idx, w in enumerate(li_w):
    if idx == 0:
        answer = set(range(w[0], w[1] + 1))
    else:
        answer = answer.intersection(list(range(w[0], w[1] + 1)))

print((len(answer)))


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

li_w = list()

for _ in range(m):
    li_w.append(tuple(map(int, input().split())))

w_l = 0
w_r = 999999

for w in li_w:
    w_l = max(w_l, w[0])
    w_r = min(w_r, w[1])


if w_r >= w_l:
    print((w_r - w_l + 1))
else:
    print((0))
# Original solution
def main():
  IN= open(0).read().split()[2:]
  left,right=-1*float("inf"),float("inf")
  
  for L,R in zip(IN[::2],IN[1::2]):
    L,R=int(L),int(R)
    if right<L or R<left:
      print((0))
      break
    else:
      tmp=sorted([left,right,L,R])
      left,right=tmp[1],tmp[2]
  else:
    print((right-left+1))
  
if __name__=="__main__":
  main()

# Optimized solution
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))
if __name__=="__main__":
  main()
# Original solution
n, m = list(map(int, input().split()))
a = []
for i in range(m):
    a.append(input().split())
haba = a[0]
for i in a:
    if int(haba[0]) <= int(i[0]) <= int(haba[1]):
        haba[0] = i[0]
    elif int(haba[1]) < int(i[0]):
        print((0))
        exit()
    if int(haba[0]) <= int(i[1]) <= int(haba[1]):
        haba[1] = i[1]
    elif int(i[1]) < int(haba[0]):
        print((0))
        exit()
if n < int(haba[0]):
    ans = 0
elif int(haba[0]) <= n <= int(haba[1]):
    ans = n - int(haba[0]) + 1
else:
    ans = int(haba[1]) - int(haba[0]) + 1
print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
a = []
for i in range(m):
    a.append(list(map(int, input().split())))
haba = a[0]
for i in a:
    if haba[0] <= i[0] <= haba[1]:
        haba[0] = i[0]
    elif haba[1] < i[0]:
        print((0))
        exit()
    if haba[0] <= i[1] <= haba[1]:
        haba[1] = i[1]
    elif i[1] < haba[0]:
        print((0))
        exit()
if n < haba[0]:
    ans = 0
elif haba[0] <= n <= haba[1]:
    ans = n - haba[0] + 1
else:
    ans = haba[1] - haba[0] + 1
print(ans)
# Original solution
n,m = list(map(int,input().split()))

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


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

s,e = list(map(int,input().split()))
for i in range(m-1):
  l,r = list(map(int,input().split()))
  s = max(s,l)
  e = min(e,r)

print((len(list(range(s,e+1)))))
# Original solution
def enter_card(card, L, R):
    for k in range(L-1,R):
        card[k] += 1
    return card

def open_list(a, M):
    b = []
    for (index, i) in enumerate(a):
        if i == M:
            b.append(index)
    return b

N, M = list(map(int, input().split(' ')))
idcard = [0 for i in range(N)]
for i in range(M):
    L,  R = list(map(int, input().split(' ')))
    idcard = enter_card(idcard, L, R)
a = open_list(idcard, M)
print((len(a)))

# Optimized solution
def enter_card(L, R, f_point, e_point):
    if f_point < L:
        f_point = L
    if e_point > R:
        e_point = R
    return f_point, e_point

def open_list(L, R):
    if L > R:
        return 0
    else:
        return R - L + 1
   

N, M = list(map(int, input().split(' ')))
f_point = 1
e_point = N
for i in range(M):
    L,  R = list(map(int, input().split(' ')))
    f_point, e_point = enter_card(L, R, f_point, e_point)
a = open_list(f_point, e_point)
print(a)
# Original solution
n,m = list(map(int, input().split()))
a = set(range(1,n+1))
for i in range(m):
    l,r = list(map(int,input().split())) 
    a &= set(range(l,r+1))
    
print((len(a)))

# Optimized solution
n,m = list(map(int, input().split()))
a = n
b = 1
for i in range(m):
    l,r = list(map(int,input().split())) 
    a = min(a,r)
    b = max(b,l)
    
print((max(a-b+1,0)))
# Original solution
N, M = list(map(int, input().split()))
dic = {i:0 for i in range(1, N+1)}
c = 0

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

for id, val in list(dic.items()):
    if val == M:
        c += 1

print(c)

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

for i in range(M):
    L_new, R_new = list(map(int, input().split()))
    if L < L_new:
        L = L_new
    if R_new < R:
        R = R_new

if L > R:
    print((0))
else:
    print((R-L+1))
# Original solution
n,m=list(map(int, input().split()))
ans=[0]*n
for _ in range(m):
    l,r=list(map(int, input().split()))
    for i in range(l, r+1):
        if l<=i<=r:
            ans[i-1]+=1
print((ans.count(m)))

# Optimized solution
n,m=list(map(int, input().split()))
maxl=0
minr=n
for _ in range(m):
    l,r=list(map(int, input().split()))
    maxl = max(maxl, l)
    minr = min(minr, r)
print((minr-maxl+1 if maxl<=minr else 0))
# Original solution
import copy
N, M = list(map(int, input().split()))
LR = []
LR2 = []
count = 0
for i in range(M):
    l, r = list(map(int, input().split()))
    LR2.append(l)
    LR2.append(r)
    LR3 = copy.copy(LR2)
    LR.append(LR3)
    LR2.clear()
for i in range(1, N+1):
    flag = 1
    for j in LR:
        if not (i >= j[0] and i <= j[1]):
            flag = 0
            break
    if flag == 1:
        count += 1

print(count)






# Optimized solution
import copy
N, M = list(map(int, input().split()))
LR = []
LR2 = []
count = 0
min = 1
max = N
for i in range(M):
    l, r = list(map(int, input().split()))
    LR2.append(l)
    LR2.append(r)
    LR3 = copy.copy(LR2)
    LR.append(LR3)
    LR2.clear()
#print(M, N, LR)

for i in LR:
    if min < i[0]:
        min = i[0]
    if max > i[1]:
        max = i[1]
    #print(min, max)
if max - min >= 0:
    ans = max - min + 1
else:
    ans = 0
print(ans)
# Original solution
f=lambda:list(map(int,input().split()))
_,m=f()
l,r=list(zip(*[f() for _ in range(m)]))
print((max(0,(min(r)-max(l)+1))))

# Optimized solution
f=lambda:list(map(int,input().split()))
_,m=f()
l,r=list(zip(*[f() for _ in range(m)]))
print((max(0,min(r)-max(l)+1)))
# Original solution
# import math
# import statistics
# a=input()
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
e1,e2 = list(map(int,input().split()))
#f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
h = []
for i in range(e2):
    h.append(list(map(int,input().split())))

ans=[]
ha1=[]
ha = [k for i in range(e2) for k in range(1,e1+1) if h[i][0]<=k<=h[i][1]]
sha=sorted(ha)

ma=0

count=1
#最大値求める
for i in range(len(sha)-1):
    if sha[i]==sha[i+1]:
        count+=1
    else:
        if count>=e2:
            ha1.append(count)
        count=1

if count>=e2:
    ha1.append(count)
print((len(ha1)))



# for i in range(1,e1+1):
#     count=0
#     for k in range(e2):
#         if h[k][0]<=i<=h[k][1]:
#             count+=1
#     if count==e2:
#         ans.append(count)

# print(len(ans))
    




# Optimized solution
# import math
# import statistics
# a=input()
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
e1,e2 = list(map(int,input().split()))
#f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
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))
# Original solution
N,M= list(map(int,input().split()))
LRlis = [list(map(int, input().split())) for i in range(M)]
A = list(range(LRlis[0][0],LRlis[0][1]+1))
for i in range(M):
  B = list(range(LRlis[i][0],LRlis[i][1]+1))
  C = list(set(A) & set(B))

print((len(C)))

# Optimized solution
N,M= list(map(int,input().split()))
LRlis = [list(map(int, input().split())) for i in range(M)]
maxL = 0
minR = 10**5
for i in range(M):
  maxL = max(LRlis[i][0],maxL)
  minR = min(LRlis[i][1],minR)

ans = minR-maxL    
if ans>=0:
  print((ans+1))
else:
  print((0))
# Original solution
#127_C
n, m = list(map(int, input().split()))
s = set(range(1,n+1))

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

print((len(s)))

# Optimized solution
#127_C
n, m = list(map(int, input().split()))
L = []
R = []
for _ in range(m):
    l, r = list(map(int, input().split()))
    L.append(l)
    R.append(r)
print((max(0, min(R)-max(L)+1)))
# Original solution
"""
i番目のゲートは LiからRiまでのIDカードのうちどれか1枚を持っていれば通過できます
"""

N, M = list(map(int, input().split()))
arr = [0]*N

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

print((arr.count(M)))


# Optimized solution
"""
i番目のゲートは LiからRiまでのIDカードのうちどれか1枚を持っていれば通過できます
"""

N, M = list(map(int, input().split()))
left = 0
right =float("inf")

for i in range(M):
    L, R = list(map(int, input().split()))
    if left<L:
        left = L
    if right > R:
        right = R

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

max_L = []
min_R = []

cnt = 0
for i in range(m):
    L,R = list(map(int,input().split()))
    max_L.append(L)
    min_R.append(R)

for i in range(1,n+1):
    if max(max_L) <= i <= min(min_R):
        cnt += 1
print(cnt)

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

max_L = []
min_R = []

cnt = 0
for i in range(m):
    L,R = list(map(int,input().split()))
    max_L.append(L)
    min_R.append(R)
if max(max_L) <= min(min_R):
    print((min(min_R)-max(max_L)+1))
else:
    print((0))
# Original solution
n,m=list(map(int,input().split()))
L=[0]*m
R=[0]*m
for i in range(m):
    l,r=list(map(int,input().split()))
    L[i],R[i]=l,r
ans=min(R)-max(L)+1
print((max(ans,0)))


# 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)+1
print((max(ans,0)))
# Original solution
from collections import Counter as co
n,m=list(map(int,input().split()))
a=[]
for i in range(m):
    l,r=list(map(int,input().split()))
    
    a.extend(list(range(l,r+1)))
d=co(a)
x,y=list(zip(*list(d.items())))
print((y.count(m)))


# Optimized solution
from collections import Counter as co
n,m=list(map(int,input().split()))
a=[]
x=0
y=10**6
for i in range(m):
    l,r=list(map(int,input().split()))
    if(l>x):
        x=l
    if(r<y):
        y=r
if(y>=x):
    print((y-x+1))
else:
    print((0))
# Original solution
N, M = list(map(int,input().split()))

ans = set(range(1,N+1))
for _ in range(M):
    a,b = list(map(int,input().split()))
    filset = set(range(a,b+1))
    
    ans = ans & filset

print((len(ans)))

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

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

print((len(list(range(L,R+1)))))
# Original solution
n,m = list(map(int,input().split()))
l,r = list(map(int,input().split()))
for i in range(m-1):
    in_l,in_r = list(map(int,input().split()))
    l = max(l,in_l)
    r = min(r,in_r)
print((max(r-l+1,0)))

# Optimized solution
n,m = list(map(int,input().split()))
lm,rm = list(map(int,input().split()))
for i in range(m-1):
    l,r = list(map(int,input().split()))
    lm = max(lm,l)
    rm = min(rm,r)
print((max(rm-lm+1,0)))
# Original solution
import collections

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

lr = [list(map(int, input().split())) for i in range(m)]
lr_gate = []

for i in range(m):
    for j in range(lr[i][0], lr[i][1] + 1):
        lr_gate.append(j)

lr_gate = list(map(str, lr_gate))
lr_collect = collections.Counter(lr_gate)

count = 0
for v in list(lr_collect.values()):
    if v == m:
        count += 1

print(count)

# Optimized solution
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(m)]
lr.sort()
r_min = 100000
l_max = 0
lr_min = 100000

for i in range(m):
    l_max = max(lr[i][0], l_max)
    r_min = min(lr[i][1], r_min)

lr_min = [r_min - l_max + 1 if l_max <= r_min else 0]
print((lr_min[0]))
# Original solution
N, M = list(map(int, input().split(' ')))

for _ in range(M):
    l, r = list(map(int, input().split(' ')))
    
    if _ == 0:
        if r > N:
            r = N
        ans = set(range(l, r+1))
        
    else:
        if r > N:
            r = N
        _ans = ans
        ans = set(range(l, r+1))
        ans = ans & _ans

print((len(ans)))

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

L = 1
R = N

for _ in range(M):
    l, r = list(map(int, input().split(' ')))
    if l > L:
        L = l
    if r < R:
        R = r

ans = R-L+1
if ans >= 0:
  print(ans)
else:
  print((0))
# Original solution
n,m = list(map(int,input().split()))
li=[list(map(int,input().split())) for i in range(m)]
cnt = []
ans=[]

for j in li:
    cnt.append(set(num for num in range(j[0],j[1]+1)))

ans = cnt[0]
for k in range(len(cnt)-1):
    ans = ans & cnt[k+1]
print((len(ans)))


# Optimized solution
n,m = list(map(int,input().split()))
li=[list(map(int,input().split())) for i in range(m)]
max_l,min_r=0,10**9

for i in li:
    max_l = max(max_l,i[0])
    min_r = min(min_r,i[1])
if min_r >= max_l:
    print((min_r-max_l+1))
else:
  print((0))
# Original solution
N, M = list(map(int, input().split()))

id = [0] * (N+1)

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

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

print(cnt)


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

left = 1
right = N

for i in range(M):
    L, R = list(map(int, input().split()))
    if L > left :
        left = L
    if R < right:
        right = R

if right-left+1 > 0:
    print((right-left+1))
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))

id_set = set(range(N + 1))

for m in range(M):
  L, R = list(map(int, input().split()))
  id_set = id_set & set(range(L, R + 1, 1))
  if len(id_set) == 0:
    break


print((len(id_set)))

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

L = list(range(M))
R = list(range(M))

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

L_max = max(L)
R_min = min(R)

print((R_min - L_max + 1 if L_max <= R_min 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 i in range(1, M):
    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 = []

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

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

# Optimized solution
N, M = list(map(int, input().split()))
L_lis = []
R_lis = []
for i in range(M):
  L, R = list(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))
# Original solution
N, M = list(map(int, input().split()))
list1 = [0]*N

for i in range(M):
    a, b = list(map(int, input().split()))
    for j in range(a-1,b):
        list1[j] = list1[j]+1

count1 = 0
for i in range(len(list1)):
     if list1[i]==M:
        count1 +=1

print(count1)


# Optimized solution
N, M = list(map(int, input().split()))
a1 = 0
b1 = N
for i in range(M):
    a, b = list(map(int, input().split()))
    if a1 <= a:
        a1 =a
    if b1 >= b:
        b1 = b

ans = b1-a1+1
if ans <0:
    ans = 0
print(ans)
# Original solution
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return eval(input())

n,m = Is()
s = set(range(1,n+1))
for i in range(m):
    l,r = Is()
    s = s & set(range(l,r+1))
print((len(s)))

# Optimized solution
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return eval(input())

n,m = Is()
left,right = 0,n
for i in range(m):
    l,r = Is()
    left = max(left,l)
    right = min(right,r)
#    print(left,right)
if right-left < 0:
    print((0))
else:
    print((right-left+1))
# Original solution
n,m=list(map(int,input().split()))
c=[0]*(n+2)
for i in range(m):
  l,r=list(map(int,input().split()))
  c[l-1]+=1
  c[r]-=1
for i in range(1,n+2):
  c[i]=c[i]+c[i-1]
ans=0
for i in range(n+2):
  if c[i]==m:
    ans+=1
print(ans)

# Optimized solution
n,m=list(map(int,input().split()))
l,r=list(map(int,input().split()))
for i in range(m-1):
  tl,tr=list(map(int,input().split()))
  if l<tl:
    l=tl
  if tr<r:
    r=tr
print((max(r-l+1,0)))
# Original solution
import sys
input = sys.stdin.readline
def main():
    N, M = list(map(int, input().split()))
    arr = [list(map(int, input().split())) for i in range(M)]
    ID = [True]*N
    for arrs in arr:
        L, R = arrs
        for i in range(N):
            if L-1 <= i and i < R:
                continue
            else:
                ID[i] = False
    print((ID.count(True)))

if __name__ == '__main__':
    main()

# Optimized solution
import sys
input = sys.stdin.readline
def main():
    N, M = list(map(int, input().split()))
    maxL, minR = 0, N+1
    for i in range(M):
        L, R = list(map(int, input().split()))
        maxL = max(L, maxL)
        minR = min(R, minR)
    ans = minR -maxL + 1
    print((ans if ans>0 else 0))

if __name__ == '__main__':
    main()
# Original solution
n, m = list(map(int, input().split()))
cnt = [0]  * n
for i in range(m):
    l, r = list(map(int, input().split()))
    for j in range(l, r + 1):
        cnt[j - 1] += 1

print((cnt.count(m)))

# 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)
L1 = max(l)
R1 = min(r)
if R1 >= L1:
    print((R1 - L1 + 1))
else:
    print((0))
# Original solution
import sys
input = sys.stdin.readline
num = []
num = [int(i) for i in input().split()]

gate = []

for temp in range(num[1]):
    temp = []
    temp = [int(i) for i in input().split()]
    gate.append(temp)

check = [0] * num[0]

for temp in gate:
    while temp[0] <= temp[1]:
        check[temp[0]-1] += 1
        temp[0] += 1

print((check.count(num[1])))

# Optimized solution
import sys
input = sys.stdin.readline
num = []
num = [int(i) for i in input().split()]

syou = []
dai = []

for temp in range(num[1]):
    suuzi = []
    suuzi = [int(i) for i in input().split()]
    syou.append(suuzi[0])
    dai.append(suuzi[1])

result= min(dai) - max(syou) + 1
if result > 0:
    print(result)
else:
    print((0))
# Original solution
# -*- coding: utf-8 -*-
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()))
count = 0
for i in range(n):
  for j in range(m):
    if (i+1 < l[j]) or (i+1 > r[j]):
      break
  else:
    count = count + 1
print(count)

# Optimized solution
# -*- coding: utf-8 -*-
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()
if r[0] - l[m-1] + 1 > 0:
  print((r[0] - l[m-1] + 1))
else:
  print("0")
# Original solution
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(m)]

ans = set(range(lr[0][1] + 1)) - set(range(lr[0][0]))
for l, r in lr:
    ans &= (set(range(r + 1)) - set(range(l)))
print((len(ans) if n > len(ans) else n))

# Optimized solution
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(m)]

max_l = float("-inf")
min_r = float("inf")

for l, r in lr:
    max_l = max(max_l, l)
    min_r = min(min_r, r + 1)

ans = min_r-max_l
print(("0" if ans <= 0 else ans if ans < n else n))
# Original solution
n, m = list(map(int, input().split()))
lr = [[int(l), int(r)] for l, r in [input().split() for i in range(m)]]
for j in range(m):
  lr[j] = [l for l in range(lr[j][0], lr[j][1]+1)]
  lr[j] = set(lr[j])
lr_intersection = lr[0]
for k in range(1,m):
  lr_intersection = lr_intersection.intersection(lr[k])
print((len(lr_intersection)))

# Optimized solution
import heapq
n, m = list(map(int, input().split()))
lr = []
heap1 = []
heap2 = []
for i in range(m):
    lr.append(list(map(int, input().split())))
for j in range(m):
    heapq.heappush(heap1, -lr[j][0])
    heapq.heappush(heap2, lr[j][1])
d = heapq.heappop(heap2) + heapq.heappop(heap1) +1 
if d < 0:
    print((0))
else:
    print(d)
# Original solution
N, M = list(map(int, input().split()))

C = [0]*N
for i in range(M):
    L, R = list(map(int, input().split()))
    for j in range(L, R+1):
        C[j-1] += 1

ans = 0
for i in range(N):
    if C[i] == M:
        ans += 1

print(ans)

# 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)

ans = 0
l = max(L)
r = min(R)
for i in range(N):
    if i+1 >= l and i+1 <= r:
        ans += 1

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

set_ID = {i for i in range(N)}

for _ in range(M):
    L, R = list(map(int, input().split()))
    set_M = {i for i in range(L - 1, R)}
    set_ID = set_ID & set_M
    
print((len(set_ID)))

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

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