peterverebics/CodeNet_Python_118 huggingface dataset:
#import math
#import bisect
#import numpy as np
#import itertools
#import copy
import sys

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n, m = list(map(int,ipti().split()))
    l = [0] * m
    r = [0] * m

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

    lower = -INF
    upper = INF

    for i in range(m):
        lower = max(lower, l[i])
        upper = min(upper, r[i])

    print(max(0, upper-lower+1))

if __name__ == '__main__':
    main()
N,M = map(int, input().split())
Key = [0 for i in range(N+1)]
for _ in range(M):
    L,R = map(int,input().split())
    L -= 1
    R -= 1
    Key[L] += 1
    Key[R+1] -= 1
count = 0
sum = 0
for i in range(len(Key)):
    sum += Key[i]
    if sum == M:
        count += 1
print(count)
n, m = map(int, input().split())
lmax, rmin = map(int, input().split())
for i in range(1, m):
  l, r = map(int, input().split())
  lmax = max(lmax, l)
  rmin = min(rmin, r)
print(max(0, rmin-lmax+1))
N,M = map(int,input().split())
L=[]
R=[]

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

if Rm-LM >= 0:
  print(Rm-LM+1)
  
else:
  print(0)
N,M=map(int,input().split())
lmax,rmin=1,N
for i in range(M):
  l,r=map(int,input().split())
  if l > lmax:lmax=l
  if r < rmin:rmin=r
print(max(rmin-lmax+1,0))
n, m = map(int, input().split())
a = 0; b = n+1
for _ in range(m):
    l, r = map(int, input().split())
    a = max(a, l)
    b = min(b, r)
print(max(0, b-a+1))
#!/usr/bin/env python3

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

min_, max_ = 0, 10**5
for i in range(m):
    a, b = list(map(int, input().split()))

    min_ = max(min_, a)
    max_ = min(max_, b)

print(max(max_-min_+1, 0))
import sys
input = lambda: sys.stdin.readline().rstrip()

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

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

for i in range(len(card)-1):
    card[i+1] += card[i]

print(card.count(M))
N, M = map(int, input().split())

lstL = list()
lstR = list()

for i in range(0, M):
    L, R = map(int, input().split())
    lstL.append(L)
    lstR.append(R)

minVal = max(lstL)
maxVal = min(lstR)

res = (maxVal - minVal + 1) if minVal <= maxVal else 0

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

ans = min(R)-max(L)+1
if ans>=1:
    print(ans)
else:
    print(0)
N, M = map(int, input().split())
limL = -454554565454
limR = 7896541265478456
for i in range(M):
  L, R = map(int, input().split())
  limL = max([limL, L])#lとしては，大きい方が嬉しい
  limR = min([limR, R])#rは小さい方がいい

print(limR-limL+1) if limR-limL+1>0 else print(0)
n,m = map(int, input().split())
ls = []
rs = []
for _ in range(m):
    l,r = map(int, input().split())
    ls.append(l)
    rs.append(r)

ls.sort()
rs.sort()

ans = rs[0] - ls[-1] + 1
ans = max(0,ans)

print(ans)
N,M = map(int,input().split())
left = []
right = []
for i in range(M):
    l,r = map(int,input().split())
    left.append(l)
    right.append(r)
if max(left)>min(right):
    print(0)
else:
    print(min(right)-max(left)+1)
n,m=map(int,input().split())
s=[]
S=[]
for i in range(m):
    l,r=map(int,input().split())
    s.append(l)
    S.append(r)

if max(s)<=min(S):
    print(min(S)-max(s)+1)
else:
    print('0')
N,M=map(int,input().split())
List = []
for i in range (M):
  List.append(list(map(int, input().split())))
resList = [0]*N
for i in range(M):
  resList[List[i][0]-1] +=1
  if List[i][1]<N:
    resList[List[i][1]] +=-1
for j in range(1,N):
  resList[j] = resList[j] + resList[j-1]
print(resList.count(M))
n,m = map(int, input().split())
lr = [0, 10**6]
for _ in range(m):
  t = sorted(list(map(int, input().split())))
  lr[0] = max(lr[0], t[0])
  lr[1] = min(lr[1], t[1])

ans = lr[1] - lr[0] + 1
if ans < 0:
  print(0)
else:
  print(ans)
n,m=map(int,input().split())
l=1
r=n
for i in range(m):
    ll,rr = map(int,input().split())
    l=max(l,ll)
    r=min(r,rr)
if l<=r:
    print(r-l+1)
else:
    print(0)
N,M = map(int,input().split())
a = list()
b = list()
for i in range(M):
  tmp_a,tmp_b =  map(int, input().split())
  a.append(tmp_a)
  b.append(tmp_b)
print(min(b)-max(a)+1 if min(b)-max(a) >= 0 else 0)
n, m = map(int, input().split())
llist = []
rlist = []
for i in range(m):
    l, r = map(int, input().split())
    llist.append(l)
    rlist.append(r)

c = 0
lm = max(llist)
rm = min(rlist)
for i in range(1, n + 1):
    if lm <= i <= rm:
        c += 1

print(c)
N, M = map(int, input().split())
Llist = []
Rlist = []

for i in range(M):
    l, r = map(int, input().split())
    Llist.append(l)
    Rlist.append(r)

if min(Rlist)-max(Llist)+1 > 0:
    print(min(Rlist)-max(Llist)+1)
else:
    print(0)
N, M = map(int, input().split())
c = [0 for i in range(N+1)]
for x in range(M):
    L, R = map(int, input().split())
    c[L-1] += 1
    c[R] -= 1
r = 0
t = 0
for x in c[:-1]:
    t += x
    if t == M:
        r += 1
print(r)
n,m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(m)]
l_max = 0
r_min = 100000000
for i in range(m):
    if (l_max < a[i][0]):
        l_max = a[i][0]
for i in range(m):
    if (r_min > a[i][1]):
        r_min = a[i][1]
print(max(r_min-l_max+1, 0))
N, M = map(int, input().split())
MIN = 0
MAX = 110000
        
for i in range(M):
    L, R = map(int, input().split())
    MIN = max(MIN, L)
    MAX = min(MAX, R)
    #print(MIN, MAX)

ans = MAX-MIN+1 if MAX >= MIN else 0
print(ans)
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n, m = map(int, input().split())
    L, R = 1, n
    for _ in range(m):
        l, m = map(int, input().split())
        if l > L: L = l
        if m < R: R = m
    if R-L+1 < 0:
        print(0)
    else:
        print(R-L+1)
    
if __name__ == '__main__':
    main()
N, M = map(int, input().split())
left, right = map(int, input().split())
for _ in range(M-1):
    c_left, c_right = map(int, input().split())
    if c_left > left:
        left = c_left
    if c_right < right:
        right = c_right

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

L=[]
R=[]

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

if min(R)<max(L):
    print(0)
else:
    print(min(R)-max(L)+1)
N, M = map(int, input().split())
L = 1
R = N
for i in range(M):
  l, r = map(int, input().split())
  if L < l:
    L = l
  if R > r:
    R = r
print(max(1+R-L, 0))
def main():
  _, _, *lst = map(int, open(0).read().split())
  l, r = max(lst[::2]), min(lst[1::2])
  print(0 if l > r else r - l + 1)

main()
N,M= list(map(int,input().split()))
end = []
start = []
for i in range(M):
    A,B = list(map(int,input().split()))
    end.append(B)
    start.append(A)
end_min = min(end)
start_max = max(start)
if start_max > end_min:
    print(0)
else:
    print(end_min-start_max+1)
n, m = map(int, input().split())
L = 1
R = n
for i in range(m):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r)
if l > R:
    print(0)
else:
    print(R - L + 1)
from itertools import accumulate
N, M = map(int, input().split())

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

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

imos = list(accumulate(imos))

ans = 0
for im in imos:
    ans += (im == M)

print(ans)
# C - Prison
N,M = map(int,input().split())
L,R = 1,N
for _ in range(M):
    l,r = map(int,input().split())
    L,R = max(L,l),min(R,r)
print(max(0,R-L+1))
def main():
    N, M = map(int, input().split())

    cards = [0] * (N + 1)

    for _ in range(M):
        l, r = [int(v) - 1 for v in input().split()]
        cards[l] += 1
        cards[r+1] -= 1

    count = 0
    if cards[0] == M:
        count += 1

    for i in range(1, N+1):
        cards[i] += cards[i - 1]
        if cards[i] == M:
            count += 1

    print(count)


main()
n,m = map(int,input().split())
l = [0 for _ in range(m)]
r = [0 for _ in range(m)]
for i in range(m):
    l[i],r[i] = map(int,input().split())
s = [0 for _ in range(n+1)]
for i in range(m):
    s[l[i]-1] += 1
    s[r[i]] -= 1
h = [0]
for i in range(n):
    h.append(s[i]+h[i])
ans = 0
for i in range(n+1):
    if(h[i] == m):
        ans += 1
print(ans)
import sys
sys.setrecursionlimit(10000000)
input=sys.stdin.readline

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

ansl=-1
ansr=10**6

for _ in range(m):
    l,r=map(int,input().split())
    ansl=max(ansl,l)
    ansr=min(ansr,r)

print(max(ansr-ansl+1,0))
N, M = map(int, input().split())

maxL = 0
minR = float('inf')
for _ in range(M):
    l, r = map(int, input().split())
    maxL = max(maxL, l)
    minR = min(minR, r)


ans = minR - maxL + 1
ans = max(ans, 0)
print(ans)
n, m = map(int, input().split())
x = 1
y = n
for i in range(m):
    l, r = map(int, input().split())
    x = max(l, x)
    y = min(r, y)
print(max(0, y-x+1))
N, M = map(int, input().split())

L = [None] * M
R = [None] * M

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

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

if L_max > R_min:
    print(0)
else:
    print(R_min - L_max + 1)
n, m = map(int,input().split())
l = [0]*m
r = [0]*m
for i in range(m):
    l1,r1 = map(int,input().split())
    l[i] = l1
    r[i] = r1
print(max(min(r) - max(l) + 1, 0))
N, M = map(int, input().split())
L = [0] * M
R = [0] * M

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

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

ma = 0
mi = float("INF")
for i in range(0,m):
    ma = max(ma,A[i][0])
    mi = min(mi,A[i][1])

ans = mi-ma+1
if ans >0:
  print(ans)
else :
  print(0)
n,m=[int(x) for x in input().rstrip().split()]
ll=[]
rl=[]
for i in range(m):
  l,r=[int(x) for x in input().rstrip().split()]
  ll.append(l)
  rl.append(r)

ans=0
if min(rl)-max(ll)<0:
  print(0)
else:
  print(min(rl)-max(ll)+1)
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]

L = []
R = []
L_append = L.append
R_append = R.append
for l, r in LR:
    L.append(l)
    R.append(r)

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

l=1
r=N

for i in LR:
    #print(i)
    if i[0]>l:
        l=i[0]
    if i[1]<r:
        r=i[1]

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


if __name__ == '__main__':
    main()
#

import sys
input=sys.stdin.readline

def main():
    N,M=map(int,input().split())
    lr=[tuple(map(int,input().split())) for i in range(M)]
    imos=[0]*(N+2)
    cnt=0
    for i in lr:
        imos[i[0]]+=1
        imos[i[1]+1]-=1
    for j in range(N+1):
        imos[j+1]+=imos[j]
    for i in imos:
        if i == M:
            cnt+=1
    print(cnt)
    
if __name__=="__main__":
    main()
if __name__ == "__main__":
    n, m = map(int, input().split())
    l = 1
    r = n

    for _ in range(m):
        li, ri = map(int, input().split())
        if l < li:
            l = li
        if r > ri:
            r = ri

    print(max(r-l+1,0))
l = input().split(" ")
n = int(l[0])
m = int(l[1])

r = []
l = []

for i in range(m):
    gatei = input().split(" ")
    li = int(gatei[0])
    ri = int(gatei[1])
    l.append(li)
    r.append(ri)

l.sort()
r.sort()

count = r[0] - l[m-1] + 1

if count >= 0:
    print(count)
else:
    print(0)
n, m = map(int,input().split())
l_lis = []
r_lis = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    l_lis.append(tmp[0])
    r_lis.append(tmp[1])
ll = max(l_lis)
rr = min(r_lis)
count = rr - ll + 1
if count <= 0:
    print(0)
else:
    print(count)
def main():
    N, M = map(int, input().split())
    L, R = 0, 1001001
    for i in range(M):
        l, r = map(int, input().split())
        L = max(L, l)
        R = min(r, R)
    print(max(0, R - L + 1))


if __name__ == '__main__':
    main()
n,m=map(int,input().split())
low=1
high=n 
for _ in range(m):
    l,r=map(int,input().split())
    low=max(l,low)
    high=min(r,high)
if high-low+1>=0:
    print(high-low+1)
else:
  print(0)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


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


if __name__ == "__main__":
    main()
import sys
sys.setrecursionlimit(10**6)

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

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

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

ans = max(0, minR-maxL+1)
print(ans)
n ,m = map(int, input().split())
L = []
R = []
for i in range(m):
    l, r = input().split()
    L.append(int(l))
    R.append(int(r))
x = min(R) - max(L) + 1
print(x if x > 0 else "0")
N,M=map(int,input().split())
LR=set(range(N+1))
l=[]
r=[]
for i in range(M):
    L,R=map(int,input().split())
    l.append(L)
    r.append(R)
ans=min(r)-max(l)+1
if ans<0:
    ans=0
print(ans)
def myAnswer(N,M,L,R) -> int:
   MAXL=max(L)
   MINR =min(R)
   ans = [i for i in range(1,N+1) if(MAXL<= i and MINR >= i)]
   return len(ans)


def modelAnswer():
   tmp=1
def main():
   N, M = map(int,input().split())
   L =[]
   R = []
   for _ in range(M):
      l,r = map(int,input().split())
      L.append(l)
      R.append(r)
   print(myAnswer(N,M,L[:],R[:]))
   

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

min_ = 1
max_ = 10**5
for i in range(m):
    l,r = map(int, input().split())
    min_ = max(min_, l)
    max_ = min(max_, r)

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

if l <= r:
    ans = r-l+1
else:
    ans = 0

print(ans)
#!/usr/bin/env python3

def main():
    N, M = map(int, input().split())
    L =[0 for i in range(M)]
    R = [0 for i in range(M)]

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


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

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

print(max(0,min(rlist)-max(llist)+1))
n,m = map(int,input().split())
ans = [1, n]

for _ in range(m):
    l,r = map(int,input().split())
    ans[0] = max(l, ans[0])
    ans[1] = min(r, ans[1])

if ans[1] - ans[0] >= 0:
    print(ans[1] - ans[0] + 1)
else:
    print(0)
N,M = map(int,input().split())
min = 0
max = N

for _ in range(M):
    L_i,R_i = map(int,input().split())
    if L_i > min:
        min = L_i
    if R_i < max:
        max = R_i

if min > max:
    print(0)
else:
    print(max - min + 1)
n,m = map(int, input().split())

left = 1
right = n

for i in range(m):
    a, b = map(int, input().split())
    left = max(left, a)
    right = min(right,b)
    
print(max(right - left +1 , 0))
n,m = map(int,input().split())
a = [list(map(int,input().split())) for i in range(m)]
l,r = [list(i) for i in zip(*a)]
print(max(0,min(r)-max(l)+1))
n,m=map(int,input().split())
L=[0]*m
R=[0]*m
ans=0
for i in range(m):
        L[i],R[i]=map(int,input().split())
print(max(0,(min(R)-max(L)+1)))
#!/usr/bin/env python
# coding: utf-8

# In[7]:


N,M = map(int, input().split())
lr = []
l_max = 1
r_min = N
for _ in range(M):
    lr.append(list(map(int, input().split())))
    l_max = max(l_max, lr[-1][0])
    r_min = min(r_min, lr[-1][1])

print(max(0, r_min - l_max +1))


# In[ ]:
n,m=map(int,input().split())
ll,rr=0,n
for i in range(m):
    l,r=map(int,input().split())
    ll=max(l,ll)
    rr=min(r,rr)
print(max(rr-ll+1,0))
N, M = map(int, input().split())
L_list = []
R_list = []
for i in range(M):
  L, R = map(int, input().split())
  L_list.append(L)
  R_list.append(R)
if min(R_list) - max(L_list) < 0:
  print("0")
else:
  print(min(R_list) - max(L_list) + 1)
n,m = map(int,input().split())
lScope = []
rScope = []
ans = 0
for i in range(m):
    l,r = map(int,input().split())
    lScope.append(l)
    rScope.append(r)
# k in keyScope[i] (i= 0~m) を確かめたい
if min(rScope) >= max(lScope):
    print(min(rScope) - max(lScope) + 1)
else:
    print(0)
n,m = map(int,input().split())
count = 0
lmax = 0
rmin = 100000
for i in range(m):
  l,r = map(int,input().split())
  lmax = max(l,lmax)
  rmin = min(r,rmin)
print(max(rmin-lmax+1,0))
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]
ans =[0] * (n + 1)

for l, r in LR:
  ans[l - 1] += 1
  ans[r] -= 1
  
accu =[0] * (n + 1)
for i in range(n):
  accu[i + 1] += ans[i] + accu[i]
  
ans = accu.count(m)

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

if left <= right:
    print(right - left + 1)
else:
    print("0")
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]

left = 1
right = N

for l, r in LR:
    left = max(left, l)
    right = min(right, r)

ans = right - left

print(ans + 1 if ans >= 0 else 0)
n,m=map(int,input().split())
l=[]
r=[]
for i in range(m):
    templ,tempr=map(int,input().split())
    l.append(templ)
    r.append(tempr)
l_max=max(l)
r_min=min(r)

if l_max<=r_min:
    print(r_min-l_max+1)
else:
    print(0)
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
l = 0
r = inf
for _ in range(m):
  a, b = rm()
  l = max(a, l)
  r = min(b, r)
c = r-l+1
if c >= 1:
  print(c)
else:
  print(0)
n,m=map(int,input().split())
count=0
s=[0]*(n+1)
L=0
R=10**5
 
for i in range(m):
  l,r=map(int,input().split())
  L=max(l,L)
  R=min(r,R)
  
if (R-L+1)>=0:
  print(R-L+1)
else:
  print(0)
N, M=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
LR=[[int(i) for i in input().split()] for i in range(M)]  # 二次元配列入力　二次元マップみたいな入力のとき

mi, ma = 0, 10**5

for i in LR:
    mi = max(mi, i[0])
    ma = min(ma, i[1])

if ma-mi < 0:
    print(0)
else:
    print(ma-mi + 1)
def resolve():
    N, M = list(map(int, input().split()))
    interval = []
    l, r = 0, 10**5
    for i in range(M):
        L, R = list(map(int, input().split()))
        l = max(l, L)
        r = min(r, R)

    interval = set(list(range(l, r + 1)))
    interval = interval & set(range(1, N + 1))

    print(len(interval))

    return


resolve()
n = list(map(int, input().split()))
N = n[0]
M = n[1]
X = []
Y = []
for i in range(M):
    a = list(map(int, input().split()))
    X.append(a[0])
    Y.append(a[1])
v=0
w=0
x=max(X)
y=min(Y)
if y>=x:
    print(y-x+1)
else:
    print(0)
N, M = map(int, input().split())
maxL = 0
minR = N
for i in range(M):
    L, R = map(int, input().split())
    maxL = max(maxL, L)
    minR = min(minR, R)
if minR - maxL >= 0:
    print(minR - maxL + 1)
else:
    print(0)
N, M = map(int, input().split())
table = [[int(i) for i in input().split()] for _ in range(M)]

ans = [table[0][0], table[0][1]]
cnt = 0

for i in table[1:]:
    if ans[0] <= i[0] <= ans[1]:
        ans[0] = i[0]
    if ans[0] <= i[1] <= ans[1]:
        ans[1] = i[1]
    if ans[1] < i[0] or i[1] < ans[0]:
        cnt += 1
        break

if cnt != 0:
    print(0)
else:
    print(ans[1] - ans[0] + 1)
N, M = map(int, input().split())

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

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

num = 0 
for j in range(maxL,minR+1):
    num +=1

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

if __name__ == "__main__":
    main()
N, M = map(int, input().split())

for m in range(M):
    L, R = map(int, input().split())
    if m==0:
        l = L
        r = R
    else:
        if l<L:
            l = L
        if R<r:
            r = R

print(len(list(range(l,r+1))))
N,M=map(int,input().split())
ans=0
L,R=0,(10**5)+1
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l); R=min(R,r)
    if r<l: break
R=min(R,N)
print(abs((L-1)-R) if 0<=R-L else 0)
n, m = map(int, input().split())
left = 1
right = n
for _ in range(m):
  l, r = map(int, input().split())
  if l > left:
    left = l
  if r < right:
    right = r
print(max(0, right - left + 1))
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
input = sys.stdin.readline

N, M = map(int, input().split())
list_LR = [list(map(int, input().split())) for i in range(M)]
# print(list_LR)

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

a, b = max(L), min(R)
if a<=b:
    print(b-a+1)
else:
    print(0)
n,m=map(int,input().split())
r=[0]*m
l=[0]*m
for i in range(m):
   l[i],r[i]=map(int,input().split())
p=min(r)-max(l)+1
if p>0:
    print(p)
else:
    print(0)
N, M = map(int, input().split())
Lmi, Lma = [], []
for i in range(M):
  mi, ma = map(int, input().split())
  Lmi.append(mi)
  Lma.append(ma)
r = max(min(Lma) - max(Lmi) + 1, 0)
print(r)
N, M = list(map(int,input().rstrip().split()))
Amin = 1
Amax = N
for i in range(M):
    L, R = list(map(int,input().rstrip().split()))
    Amin = max(Amin,L)
    Amax = min(Amax,R)
    if Amin>Amax:
        print(0)
        break
else:
    print(Amax-Amin+1)
import sys
import numpy as np

n, m, *lr = map(int, sys.stdin.read().split())
l, r = np.array(lr).reshape(-1, 2).T

def main():
    res = max(0, r.min() - l.max() + 1)
    print(res)

if __name__ ==  '__main__':
    main()
n,m=map(int,input().split())
p=[0]*(n+1)
for _ in range(m):
  l,r=map(int,input().split())
  p[l-1]+=1
  p[r]-=1
for i in range(n):
  p[i+1]+=p[i]
print(p.count(m))
N, M = map(int, input().split())
maxL = 0
minR = N-1
LR = [0, N-1]
for i in range(M):
  LR = [int(i)-1 for i in input().split()]
  maxL = max(maxL, LR[0])
  minR = min(minR, LR[1])
if minR-maxL>=0:
  print(minR-maxL+1)
else:
  print(0)
N, M = map(int,input().split())
maxL = 0
minR = 10 ** 9
for i in range(M):
  L, R = map(int,input().split())
  maxL = max(maxL,L)
  minR = min(minR,R)
print(max(0,minR - maxL + 1))
n, m = map(int, input().split())
inf = 0
sup = 100001
for i in range(m) :
    l, r = map(int, input().split())
    inf = max(inf, l)
    sup = min(sup, r)
if sup >= inf :
    print(sup-inf+1)
else :
    print("0")
n,m = map(int,input().split())
left = 0
right = n
for i in range(m):
  l,r = map(int,input().split())
  left = max(left,l)
  right = min(right,r)
print(max(0,right-left+1))
n, m = map(int, input().split())

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

print(min(r) - max(l) + 1 if min(r) >= max(l) else 0)