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

for i in range(M):
  L, R = list(map(int, input().split()))
  pass_card = [i for i in range(L, R+1)]
  if  i == 0:
    ans = pass_card
  elif i > 0:
    ans = set(ans) & set(pass_card)

print((len(ans)))

# 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(L_list)
R = min(R_list)

if R < L:
  print((0))
else:
  print((R - L + 1))
# Original solution
N, M= list(map(int, input().split()))
L1, R1= list(map(int, input().split()))
Ans = set(range(L1, R1+1))

for x in range(M-1):
    L, R = list(map(int, input().split()))
    List = set(range(L, R+1))
    Ans = (Ans) & (List)

print((len(Ans)))

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

for x in range(M-1):
    L, R = list(map(int, input().split()))
    if L > L1:
        L1 = int(L)
    if R < R1:
        R1 = int(R)

if R1 == L1:
    print("1")
if L1 > R1:
    print("0")
if L1 < R1: 
	print((R1-L1+1))
# Original solution
import sys
input = sys.stdin.readline
import collections

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

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

        for y in range(L, R+1):
            arr.append(y)

    arrcnt = list(collections.Counter(arr).values())
    print((sum(i == M for i in arrcnt)))


if __name__ == '__main__':
    main()

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

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

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

        if i == 0:
            ori_arr = set(range(L, R+1))
            continue

        next_arr = set(range(L, R+1))

        ori_arr = set(ori_arr & next_arr)


    print((len(ori_arr)))

if __name__ == '__main__':
    main()
# Original solution
import sys
input = sys.stdin.readline
import collections

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

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

        if i == 0:
            ori_arr = set(range(L, R+1))
            continue

        next_arr = set(range(L, R+1))

        ori_arr = set(ori_arr & next_arr)


    print((len(ori_arr)))

if __name__ == '__main__':
    main()

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

def main():
    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 = (int(min(R)) - int(max(L)) + 1)
    if ans < 0:
        print('0')
    else:
        print(ans)

if __name__ == '__main__':
    main()
# Original solution
N,M = list(map(int,input().split()))
tempL,tempR = list(map(int,input().split()))
gate_id = set(range(tempL,tempR+1))
for i in range(M-1):
  tempL,tempR = list(map(int,input().split()))
  temp_id = set(range(tempL,tempR+1))
  gate_id = gate_id & temp_id
print((len(gate_id)))

# Optimized solution
N,M = list(map(int,input().split()))
gateid = [list(map(int,input().split())) for i in range(M)]
idleft = max(gateid,key=lambda x:x[0])
idright = min(gateid,key=lambda x:x[1])
ans = idright[1]-idleft[0]+1
if ans<0:
  ans = 0
print(ans)
# Original solution
#C
n, m = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    lin = list(map(int, input().split()))
    l.append(lin[0])
    r.append(lin[1])
lma = max(l)
rmi = min(r)
if lma > rmi:
    print((0))
else:
    print((rmi - lma + 1))

# Optimized solution
#C
n, m = list(map(int, input().split()))
l = []
r = []
for _ in range(m):
    tmp=list(map(int,input().split()))
    l.append(tmp[0])
    r.append(tmp[1])
lmax = max(l)
rmin = min(r)
# print(lmax,rmin)
if rmin >= lmax:
    print((rmin -lmax + 1))
else:
    print((0))
# Original solution
N,M=list(map(int,input().split()))
l,r=list(map(int,input().split()))
s={i for i in range(l,r+1)}
for _ in range(M-1):
    l, r = list(map(int, input().split()))
    t = {i for i in range(l, r + 1)}
    s = s & t
    if len(s) == 0:
        print((0))
        exit(0)
print((len(s)))

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

print((max(rmin-lmax+1,0)))
# Original solution
N,M = list(map(int, input().split()))
L,R = list(map(int, input().split()))
ans = set(list(range(L,R+1)))

for _ in range(M-1):
    L,R = list(map(int, input().split()))
    gate = set(range(L,R+1))
    ans &= gate

print((len(ans)))    


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

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

if (R-L+1)>0:
    print((R-L+1))
else:
    print((0))
# Original solution
n,m=list(map(int,input().split()))
a=[0]*(n+1)
for i in range(n):
  a[i+1]=i+1
l,r=list(map(int,input().split()))
s=set(a[l:r+1])
for i in range(1,m):
  l,r=list(map(int,input().split()))
  s=s&set(a[l:r+1])
print((len(s)))

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

l,r = list(map(int,input().split()))
Kouho = [int(i) for i in range(l,r+1)]
for i in range(M-1):
    l,r = list(map(int,input().split()))
    tmp = [int(i) for i in range(l,r+1)]
    for j in Kouho[:]:
        if not j in tmp:
            Kouho.remove(j)

   
print((len(Kouho)))


# Optimized solution
N,M = list(map(int,input().split()))
Max_l = 0
Min_r = float('inf')

for i in range(M):
    l,r = list(map(int,input().split()))
    Max_l = max(Max_l,l)
    Min_r = min(Min_r,r)
ans = 0
if Max_l<=Min_r:
    ans = Min_r-Max_l+1
    

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

# Optimized solution
n,m = map(int,input().split())
L = [0]*m; R = [0]*m; cnt = 0
for i in range(m):
    L[i],R[i] = map(int,input().split())
ml = max(L); mr = min(R)
print(mr-ml+1) if ml <= mr else print(0)
# Original solution
#!/usr/bin/env python
# coding: utf-8

# In[1]:


import sys
#input = sys.stdin.readline
N, M = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(M)]


# In[9]:


union = set(range(1, N+1))
for i in range(M):
    union = union & set(range(lr[i][0], lr[i][1]+1))
print((len(union)))


# In[ ]:






# Optimized solution
#!/usr/bin/env python
# coding: utf-8

# In[15]:


import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))


# In[ ]:


"""
4 2
1 3
2 4
"""


# In[19]:


l_ = 0
r_ = N
for i in range(M):
    l, r = list(map(int, input().split()))
    l_ = max(l_, l)
    r_ = min(r_, r)


# In[21]:


#print(l_, r_)
ids = r_-l_+1
if ids < 0:
    print((0))
else:
    print(ids)


# In[ ]:
# Original solution
n,m = list(map(int,input().split()))
ansl = 0
ansr = n+1
for i in range(m):
    l,r = list(map(int,input().split()))
    ansl = max(ansl,l)
    ansr = min(ansr,r)
print((0 if ansl > ansr else ansr-ansl+1))

# 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)
ansl = max(L)
ansr = min(R)
print((0 if ansl > ansr else ansr-ansl+1))
# Original solution
n,m = list(map(int,input().split()))
para_val = []
for i in range(m) :
    a,b = list(map(int,input().split()))
    para_val.append([i for i in range(a,b+1)])

ans = [i for i in range(1,n+1)]

for i in range(m):
    ans = list(set(ans) & set(para_val[i]))

print((len(ans)))

# Optimized solution
n,m = list(map(int,input().split()))
para_val = []
for i in range(m) :
    a,b = list(map(int,input().split()))
    para_val.append([a,b])

migi = []
hidari = []
for i,j in para_val:
    migi.append(i)
    hidari.append(j)

a = max(migi)
b = min(hidari)

if b - a >= 0 : print(("{}".format(b-a+ 1)))
else : print("0")
# Original solution
n, m = (int(x) for x in input().split())
ss = set(range(n))
for _ in range(m):
  l, r = (int(x) for x in input().split())
  ss = ss.intersection(list(range(l-1, r)))
print((len(ss)))

# Optimized solution
n, m = (int(x) for x in input().split())
max_l = float('-inf')
min_r = float('inf')
for _ in range(m):
  l, r = (int(x) for x in input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
print((max(0, min_r - max_l+1)))
# Original solution
n,m=list(map(int,input().split()))
ln=[]
for i in range(m):
    ln.append(list(map(int,input().split())))

    ln[i]=list(range(ln[i][0],ln[i][1]+1))

nxl=ln[0]
for i in range(1,m):
    nxl=set(nxl)&set(ln[i])
print((len(list(nxl))))


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

print((max(min(r)-max(l)+1,0)))
# 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(1,n+1):
    if max(L) <= i and min(R) >= i:
        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(1,n+1):
    if L <= i and R >= i:
        ans += 1
print(ans)
# Original solution

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

cnt = 0
for i in range(n):
    if judge[i] == 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((min(R) - max(L) + 1 if min(R) >= max(L) else 0))
# Original solution
N,M=list(map(int,input().split()))
s=[list(map(int,input().split())) for x in range(M)]

max_left=0
min_right=N

for i in range(M):
    if s[i][0]>max_left:
        max_left=s[i][0]
    if s[i][1]<min_right:
        min_right=s[i][1]
        
cnt=0
for j in range(max_left,min_right+1):
    cnt+=1
print(cnt)

# Optimized solution
n, m = list(map(int, input().split()))
 
max_left = 1
min_right = n
 
for i in range(m):
    left, right = list(map(int, input().split()))
    max_left = max(max_left, left)
    min_right = min(min_right, right)
 
print((max(min_right - max_left + 1, 0)))
# Original solution
import sys

N, M = [int(x) for x in sys.stdin.readline().split(" ")]

prev = set(range(1, N+1))
for i in range(0, M):
    L, R = [int(x) for x in sys.stdin.readline().split(" ")]
    accepted = set(range(L, R+1))
    prev = prev & accepted
print((len(prev)))


# Optimized solution
import sys

N, M = [int(x) for x in sys.stdin.readline().split(" ")]

_min, _max = 1, 100000
for _ in range(0, M):
    L, R = [int(x) for x in sys.stdin.readline().split(" ")]
    _min = max(_min, L)
    _max = min(_max, R)
if _min > _max:
    print((0))
else:
    print((_max - _min + 1))
# Original solution
N,M = (int(x) for x in input().split())
lr = []
for i in range(1,N+1):
  lr.append(i)
l,r = (int(x) for x in input().split())
d = set(lr[l-1:r])
for i in range(1,M):
  l,r = (int(x) for x in input().split())
  d = d & set(lr[l-1:r])
print((len(d)))

  

# Optimized solution
N,M = (int(x) for x in input().split())
R = N
L = 1
for i in range(M):
  l,r = (int(x) for x in input().split())
  if l > L:
    L = l
  if r < R:
    R = r
if R-L < 0:
  print((0))
else:
  print((R-L+1))
# Original solution
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for _ in range(m)]
count = 0
for i in range(1,n+1):
  flag = True
  for j in range(m):
    if not lr[j][0] <= i <= lr[j][1]:
      flag = False
      break
  if flag:
    count += 1
print(count)
  
      
      
      
      
           
  

# Optimized solution
n, m = list(map(int, input().split()))
lll = []
rrr = []
for _ in range(m):
  lr = list(map(int, input().split()))
  lll.append(lr[0])
  rrr.append(lr[1])
maxl = max(lll)
minr = min(rrr)
count = 0
for i in range(1,n+1):
  if maxl <= i <= minr:
    count += 1
print(count)
# Original solution
N, M = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(M)]
  
count = 0
for i in range(1, N+1):
  flag = 0
  for j in range(M-1, -1, -1):
    if not (LR[j][0] <= i <= LR[j][1]):
      break
    else:
      flag += 1
    if flag == M:
      count += 1

print(count)

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

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

L = []
R = []
for i in LR:
  L.append(i[0])
  R.append(i[1])
  
if min(R) - max(L) < 0:
  print((0))
else:
  print((min(R) - max(L) + 1))
# Original solution
data = list([int(x) for x in input().split(" ")])

cardNum = []

for n in range(data[1]):
	cardNum.append(list([int(x) for x in input().split(" ")]))

#print(cardNum)

card = [0]*(data[0]+1)

for n in range(data[1]):
	for m in range(cardNum[n][0],cardNum[n][1]+1):
		card[m] += 1

print((card.count(data[1])))

# Optimized solution
data = list([int(x) for x in input().split(" ")])

cardNum = []

for n in range(data[1]):
	cardNum.append(list([int(x) for x in input().split(" ")]))


card = [0]*(data[0])

check = [0,1000000]

for n in range(data[1]):
	if check[0] < cardNum[n][0]:
		check[0] = cardNum[n][0]
	if check[1] > cardNum[n][1]:
		check[1] = cardNum[n][1]

if check[1]-check[0] < 0:
	print((0))
else:
	print((check[1]-check[0]+1))
# Original solution
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    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 = N
    for i in range(N):
        for j in range(M):
            c = 0
            if not L[j] <= i + 1 <= R[j]:
                ans -= 1
                c = 1
            if c == 1:
                break
    print(ans)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    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(0, ans)))

if __name__ == '__main__':
    main()
# Original solution
N, M = list(map(int, input().split()))

lst = [True] * N
for i in range(M):
    L, R = list(map(int, input().split()))
    L -= 1
    R -= 1
    for j in range(0, L):
        lst[j] = False
    for j in range(R+1, N):
        lst[j] = False
ans = 0
for i in lst:
    if i:
        ans += 1
print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
L = []
R = []
for i in range(M):
    tmp = list(map(int, input().split()))
    L.append(tmp[0])
    R.append(tmp[1])

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

#print(r)
#print(l)
count = 0
k = 0
for i in range(1,n+1):
    k=0
    for j in range(m):
        if (l[j] <= i) and (i <= r[j]):
            k += 1

            
    #print(k)
    if k== m:
        count +=1


print(count)

# Optimized solution
n,m = list(map(int,input().split()))
l = [-1]*(m)
r = [-1]*m
for i in range(m):
    a,b = list(map(int,input().split()))
    l[i]=a
    r[i]=b
right = n
left = 0 
#print(r)
#print(l)
count = 0
k = 0
for i in range(m):
    right = min(r[i],right)
    left = max(l[i],left)

ans = max(0,right -left +1)
print(ans)
# Original solution
N,R = list(map(int,input().split()))
id = [0]*N
ans = 0
for i in range(R):
  A,B = list(map(int,input().split()))
  for j in range(B-A+1):
    id[j+A-1]+=1
for i in id:
  if i == R:
    ans += 1
print(ans)

# Optimized solution
N,R = list(map(int,input().split()))
min = 0
max = 10000000
ans = 0
for i in range(R):
  A,B = list(map(int,input().split()))
  if A > min:
    min = A
  if B < max:
    max = B
if min < max:
  ans = max - min + 1
elif min == max:
  ans = 1
print(ans)
# Original solution
n, m = list(map(int, input().split()))
lst = [0 for i in range(n+1)]
cnt = 0
for _ in range(m):
    l, r = list(map(int, input().split()))
    for i in range(l, r+1):
        lst[i] += 1 

cnt = lst.count(m)
print(cnt)

# Optimized solution
n, m = map(int, input().split())
l_list = []
r_list = []
cnt = 0
for _ in range(m):
    l, r = map(int, input().split())
    l_list.append(l)
    r_list.append(r)

print(min(r_list) - max(l_list) + 1) if min(r_list) >= max(l_list) else print(0)
# Original solution
n, m = list(map(int, input().split()))

ans=set()
l, r = list(map(int, input().split()))
for j in range(l,r+1):
    ans.add(j)

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

print((len(ans)))

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

ans=set()
lm=0
rm=n
for i in range(m):
  l, r = list(map(int, input().split()))
  lm=max(l, lm)
  rm=min(r, rm)
if rm - lm < 0:
  print((0))
else:
  print((rm - lm + 1))
# Original solution
n,k = list(map(int,input().split()))
rc = [list(map(int,input().split())) for i in range(k)]

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

for i in rc:
    tmp = tmp & set(range(i[0],i[1]+1))

print((len(tmp)))

# Optimized solution
n,k = list(map(int,input().split()))
rc = [list(map(int,input().split())) for i in range(k)]
transpose = [x for x in zip(*rc)]

ans = min(transpose[1]) - max(transpose[0])

if ans<0:
    print((0))
else:
    print((ans+1))
# Original solution
N, M = [int(x) for x in input().split()]

a, b = [int(x) for x in input().split()]
s = set(range(a, b + 1))
for _ in range(M-1):
  a, b = [int(x) for x in input().split()]
  temp = set(range(a, b + 1))
  s = s & temp
print((len(s)))

# Optimized solution
N, M = [int(x) for x in input().split()]

max_a, min_b = 0, 10000000
for _ in range(M):
  a, b = [int(x) for x in input().split()]
  if a > max_a:
    max_a = a
  if b < min_b:
    min_b = b
if min_b < max_a:
  print((0))
else:
  print((min_b - max_a + 1))
# Original solution
N, M = list(map(int, input().split()))
LR = [tuple(map(int, input().split())) for _ in range(M)]

count_dict = {}

gate = set(range(10**5 + 1))

for i in range (M):
    l = LR[i][0]
    r = LR[i][1]
    temp = set(range(l, r+1))

    gate = gate & temp

print((len(gate)))


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

count_dict = {}

gate = set(range(10**5 + 1))

L = 1
R = 10**5

for i in range (M):
    l = LR[i][0]
    r = LR[i][1]

    if l > L:
        L = l
    if r < R:
        R = r

print((len(list(range(L, R+1)))))
# Original solution
N, M = list(map(int, input().split()))
LR = [tuple(map(int, input().split())) for _ in range(M)]

count_dict = {}

gate = set(range(10**5 + 1))

L = 1
R = 10**5

for i in range (M):
    l = LR[i][0]
    r = LR[i][1]

    if l > L:
        L = l
    if r < R:
        R = r

print((len(list(range(L, R+1)))))

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

L = 1
R = 10**5

for i in range (M):
    l = LR[i][0]
    r = LR[i][1]

    if l > L:
        L = l
    if r < R:
        R = r

print((len(list(range(L, R+1)))))
# Original solution
N, M = list(map(int, input().split()))

s = set()
for i in range(M):
  L, R = list(map(int, input().split()))
  tmp = set()
  for j in range(L, R + 1):
    tmp |= {j}
  if i == 0:
    s |= tmp
  else:
    s &= tmp

print((len(s)))

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

tmpL = 0
tmpR = float("inf")
for i in range(M):
  L, R = list(map(int, input().split()))
  tmpL = max(tmpL, L)
  tmpR = min(tmpR, R)

if tmpR - tmpL < 0:
  print((0))
else:
  print((tmpR - tmpL + 1))
# Original solution
from sys import stdin

def main():
    N, M = list(map(int, input().split()))
    seki = set(list(range(1,N+1)))
    for i in range(M):
        l, r = list(map(int, stdin.readline().rstrip().split()))
        #l, r = map(int, input().split())
        seki &= set(list(range(l, r+1)))

    print((len(seki)))

if __name__ == '__main__':
    main()

# Optimized solution
# 解説を見ての解答
from sys import stdin

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

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

print((max(0,r-l+1)))
# Original solution
N, M = list(map(int, input().split()))
A = []
for i in range(M):
    L, R = list(map(int, input().split()))
    if i == 0:
        S = set(list(range(L, R + 1)))
    else:
        S = S & set(list(range(L, R + 1)))
        if len(S) == 0:
            break

# L = set(A[0])
# for i in range(1, M):
#     L = L & set(A[i])

print((len(S)))


# Optimized solution
N, M = list(map(int, input().split()))
A = []
for i in range(M):
    L, R = list(map(int, input().split()))
    if i == 0:
        Lmax = L
        Rmin = R
    else:
        Lmax = max(L, Lmax)
        Rmin = min(R, Rmin)

# L = set(A[0])
# for i in range(1, M):
#     L = L & set(A[i])

if Rmin-Lmax < 0:
    print((0))
else:
    print((Rmin-Lmax+1))
# Original solution


n,m = list(map(int,input().split(" ")))
gates = []
for i in range(m):
    gates.append(list(map(int,input().split(" "))))

result = [1, n]

for card in gates:
    if result[0] < card[0]:
        result[0] = card[0]
    if result[1] > card[1]:
        result[1] = card[1]

tmp = result[1] - result[0] + 1 
if tmp >= 0:
    print(tmp)
else:
    print((0))

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

lr = []

ming = 0
maxg = 10 **6

for i in range(m):
    a,b = list(map(int,input().split(" ")))
    lr.append((a,b))

for i in lr:
    if i[0] > ming:
        ming = i[0]
    if i[1] < maxg:
        maxg = i[1]

print((max([0, maxg - ming + 1])))
# Original solution
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().rstrip().split()]
    l,r = [int(x) for x in stdin.readline().rstrip().split()]
    ans = set(range(l,r+1))

    for i in range(m-1):
        l,r = [int(x) for x in stdin.readline().rstrip().split()]
        cards = set(range(l,r+1))
        ans = ans & cards

    print((len(ans)))
    
if __name__=='__main__':
    main()

# Optimized solution
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().rstrip().split()]

    _l = 0
    _r = n

    for i in range(m):
        l,r = [int(x) for x in stdin.readline().rstrip().split()]
        _l = max(l, _l)
        _r = min(r, _r)

    if _r - _l < 0:
        print((0))
    else:
        print((_r - _l + 1))
    
if __name__=='__main__':
    main()
# Original solution
n, m = list(map(int, input().split()))
card = [i for i in range(1, n+1)]
for _ in range(m):
   l, r = list(map(int, input().split()))
   x = [i for i in range(l, r+1)]
   card = list(set(card) & set(x))

print((len(card)))


# Optimized solution
n, m = list(map(int, input().split()))
L = 1
R = n
for _ in range(m):
   l, r = list(map(int, input().split()))
   L = max(L, l)
   R = min(R, r)

if (L <= R):
   print((R - L + 1))
else:
   print((0))