peterverebics/CodeNet_Python_118 huggingface dataset:
import numpy as np
N,M = map(int,input().split())
A = (N+2)*[0]

for m in range(M):
  L,R = map(int,input().split())
  A[L]+=1
  A[R+1]-=1

A = np.cumsum(A)
print(sum(a==M for a in A))
n,m=map(int,input().split())
l=1
r=n
for i in range(m):
  li,ri=map(int,input().split())
  l = max(l,li)
  r = min(r,ri)
print(max(r-l+1,0))
N, M = map(int, input().split())
max_L, min_R = 1, N
for i in range(M):
    L, R = map(int, input().split())
    if L > max_L:
        max_L = L
    if R < min_R:
        min_R = R
ans = min_R - max_L + 1
if ans < 0:
    ans = 0
print(ans)
N, M = map(int, input().split())

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

nega = 0
posi = N

check = 1
for (L, R) in ID:
    if L > nega:
        nega = L
    if R < posi:
        posi = R
    if R < nega or posi < L:
        check = 0

if check == 1:
    print(posi-nega+1)
else:
    print(0)
def solve(l,r):
  l_max = max(l)
  r_min = min(r)
  return max(r_min - l_max + 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(solve(L,R))
n,m = map(int,input().split())
gate_list = [ list(map(int,input().split())) for i in range(m)]
#print(gate_list)
ans1 = gate_list[0][0]
ans2 = gate_list[0][1]
for i in range(1,m):
    ans1 = max(ans1,gate_list[i][0])
for i in range(1,m):
    ans2 = min(ans2,gate_list[i][1])
ans = ans2 - ans1
if ans < 0:
    print(0)
else:
    print(ans+1)
n,m = map(int,input().split())
k = [list(map(int,input().split())) for i in range(m)]
l,r = [list(i) for i in zip(*k)]
x = max(l)
y = min(r)
print(max(y-x+1,0))
import numpy as np
def main():
    N, M = map(int, input().split())
    maxl = -float('inf')
    minr = float('inf')
    for _ in range(M):
        l, r = map(int, input().split())
        if l >= maxl:
            maxl = l
        if r <= minr:
            minr = r
    val = minr - maxl + 1
    if val >= 0:
        print(val)
    else:
        print(0)
main()
n,m = map(int, input().split())
ls = []
rs = []
for _ in range(m):
  l,r = map(int, input().split())
  ls.append(l)
  rs.append(r)
  
ans = min(rs) - max(ls) + 1

if ans > 0: print(ans)
else: print(0)
n,m = map(int,input().split())
l = [list(map(int,input().split())) for i in range(m)]

left = [i[0] for i in l]
right = [i[1] for i in l]

max_left = max(left)
min_right = min(right)

ans = min_right - max_left

if ans < 0:
    print(0)

else:
    print(ans+1)
n,m = map(int,input().split())
l,r = map(int,input().split())

for i in range(m-1):
    
    nl,nr = map(int,input().split())
    if l<nl :
        l = nl
    if r > nr:
        r = nr
print(max(r-l + 1, 0))
n,m = map(int, input().split())

lmax = 0
rmin = int(1e10)

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

ans = max(0, rmin - lmax + 1)
print(ans)
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
a = right - left
print(max(0, a + 1))
N, M = map(int, input().split())
LL = 0
RR = N
for i in range(M):
    L, R = map(int, input().split())
    if LL < L:
        LL = L
    if RR > R:
        RR = R
print(max(0, RR - LL + 1))
n,m=map(int,input().split())
s,t=map(int,input().split())
for i in range(m-1):
  x,y=map(int,input().split())
  if s<x:
    s=x
  if t>y:
    t=y
print(max(0,t-s+1))
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())
if min(r)-max(l) < 0:
    print(0)
else:
    print(min(r)-max(l)+1)
n,m,*a=map(int,open(0).read().split())
b=min(a[1::2])-max(a[::2])+1
print(b if b>0 else 0)
n, m = map(int, input().split())
list_score = [ list(map(int,input().split(" "))) for i in range(m)]

left = 1
right = n
for i in range(m):
    k = list_score[i]
    if left < k[0]:
        left = k[0]
    if right > k[1]:
        right = k[1]

if left > right:
    print(0)
else:
    print(right-left+1)
n,m=map(int,input().split())
hidari=-10**100
migi=10**100
keep=1
for i in range(m):
  l,r=map(int,input().split())
  if l>migi or r<hidari:
    keep=0
    break
  elif hidari<=l<=r<=migi:
    hidari=l
    migi=r
  elif l<=hidari<=migi<=r:
    pass
  elif l<=hidari<=r<=migi:
    migi=r
  elif hidari<=l<=migi<=r:
    hidari=l

if keep==1:
  res=migi-hidari+1       
  print(min(res,n))
else:
  print(0)
# coding: utf-8

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

ids = list(range(1, N+1))

L_max = 0
R_min = 10000000000000000000
for _ in range(M):
    (L, R) = map(int, input().split(" "))
    if L > L_max:
        L_max = L
    if R < R_min:
        R_min = R

ids = [i for i in ids if L_max <= i <= R_min]
print(len(ids))
n,m = map(int,input().split())
l_max,r_min = 0,n
for i in range(m):
    l,r = map(int,input().split())
    l_max = max(l,l_max)
    r_min = min(r,r_min)
print(max(r_min - l_max +1 , 0))
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
L = 0; R = 10 ** 6
for _ in range(M):
    l, r = lr()
    if l > L:
        L = l
    if r < R:
        R = r

answer = max(0, R - L + 1)
print(answer)
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(0)
else:
    print(right - left + 1)
def main():
    n, m = map(int,input().split()) 
    startlis = []
    endlis = []

    for i in range(m):
        a, b = map(int, input().split())
        startlis.append(a)
        endlis.append(b)
    
    start = max(startlis)
    end = min(endlis)

    print(max(0,end - start + 1))





    
if __name__ == "__main__":
    main()
N, M = [int(a) for a in input().split()]
L = []
R = []

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

mingate = max(L)
maxgate = min(R)
ans = max(maxgate - mingate + 1, 0)
print(ans)
#!/usr/bin/env python3


def main():
    n, m = map(int, input().split())
    range_list = [list(map(int, input().split())) for _ in range(m)]
    min_list = [x[0] for x in range_list]
    max_list = [x[1] for x in range_list]
    min_id = max(min_list)
    max_id = min(max_list)
    answer = max_id - min_id + 1
    answer = max(answer, 0)
    print(answer)


if __name__ == '__main__':
    main()
n, m = map(int, input().split())
Authority = [list(map(int, input().split())) for _ in range(m)]
cards = list(range(1, n + 2))
l_list = []
r_list = []
for i in range(m):
    l_list.append(Authority[i][0])
    r_list.append(Authority[i][1])
        
print(len(cards[max(l_list):min(r_list) + 1]))
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(min(R) - max(L) + 1 if min(R) >= max(L) else 0)
_, _, *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)
N, M = list(map(int, input().split()))
L = 0
R = float('inf')

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

print(max(R-L+1, 0))
def solve(n, m, LR):
    maxl = 0
    minr = n
    for l, r in LR:
        if maxl < l:
            maxl = l
        if minr > r:
            minr = r
    return minr - maxl + 1 if maxl <= minr else 0

_n, _m = map(int, input().split())
_LR = [list(map(int, input().split())) for _ in range(_m)]
print(solve(_n, _m, _LR))
N, M = map(int, input().split())

mn = 0
mx = 10**6

for i in range(M):
    l, r = map(int, input().split())
    mn = max(l, mn)
    mx = min(r, mx)

result = max(0, mx - mn + 1)
print(result)
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)
count = min(R) - max(L) + 1
if count <= 0:
    print(0)
else:
    print(count)
N, M = map(int, input().split())
L = 1
R = N

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

print(max(0, R - L + 1))
n,m=map(int,input().split())
max_ = 0
min_ = n+1
for i in range(m):
  a,b=map(int, input().split())
  max_ = max(max_, a)
  min_ = min(min_, b)
#print(max_, min_)
if(min_ < max_):print(0)
else:print(min_-max_+1)
N,M = map(int,input().split())
L = [0 for _ in range(N)]
for i in range(M):
  l,r = map(int,input().split())
  l-=1;r-=1 #0index
  L[l] += 1
  if r+1 < N:
    L[r+1] -= 1
S = [0]
for i in range(N):
  temp = L[i] + S[-1]
  S.append(temp)
#print(S)
ans = 0
for i in range(1,N+1):
  if S[i] == M:
    ans += 1
print(ans)
N,M = map(int,input().split())
mi = 0
ma = 10 ** 6
for i in range(M):
  L,R = map(int,input().split())
  mi = max(mi,L)
  ma = min(ma,R)
if(ma - mi + 1 <= 0):
  print("0")
else:
  print(str(ma - mi + 1))
k,m=map(int,input().split())
l=[0 for i in range(k+1)]
for i in range(m):
  a,s=map(int,input().split())
  l[a-1]+=1;l[s]-=1
ans=0
f=0
for j in range(k):
  l[j]+=f
  f=l[j]
print(len([1 for i in l if i==m]))
import sys
n,m = map(int,input().split())
ls = [list(map(int,input().split())) for i in range(m)]
a = ls[0][0]
b = ls[0][1]
for j in range(1,m):
    a = max(a,ls[j][0])
    b = min(b,ls[j][1])
    if b-a<0:
        print(0)
        sys.exit()
print(b-a+1)
n,m = map(int,input().split())

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

max_l = -1*float('INF')
min_r = float('INF')
for i in range(m):
    max_l = max(max_l,l[i][0])
    min_r = min(min_r,l[i][1])

ans = min_r - max_l + 1

if(ans < 0):
    print(0)
    exit()

if(ans <= n):
    print(ans)
else:
    print(n)
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]

L_max = 0
R_min = 10 ** 5 + 1
for L, R in LR:
    L_max = max(L_max, L)
    R_min = min(R_min, R)

ans = R_min - L_max + 1
if ans < 0:
    print(0)
else:
    print(ans)
N,M=map(int,input().split())
L_max=0
R_min=10**9
for i in range(M):
  L,R=map(int,input().split())
  L_max=max(L_max,L)
  R_min=min(R_min,R)
print(R_min-L_max+1 if R_min>=L_max else 0)
N, M = map(int, input().split())

tmp1, tmp2 = 0, 10**5

for i in range(M):
    L, R = map(int, input().split())
    tmp1, tmp2 = max(L, tmp1), min(R, tmp2)


if tmp2-tmp1 >= 0:
    print(tmp2-tmp1+1)
else:
    print(0)
n, m = map(int, input().split())
a = [ list(map(int, input().split())) for i in range(m) ]
print(max(min(a, key=lambda x: x[1])[1] - max(a, key=lambda x: x[0])[0] + 1, 0))
n,m = map(int,input().split())
lr = [list(map(int,input().split())) for i in range(m)]

max = 0
min = n+1
for i in range(m):
    if lr[i][0] > max :
        max = lr[i][0]
    if lr[i][1] < min :
        min = lr[i][1]

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


l_max = 0
r_min = float('inf')

for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)

if r_min >= l_max:
    ans = int(r_min-l_max+1)
else :
    ans = 0
print(ans)
N,M = map(int, input().split())
LR = [[int(x) for x in input().split()] for _ in range(M)]
L = []
R = []
for i in range(M):
    L.append(LR[i][0])
    R.append(LR[i][1])
maxL = max(L)
minR = min(R)
if minR - maxL < 0:
    print(0)
else:
    print(minR-maxL+1)
n, m = map(int, input().split())
id_card = [0] * (n + 1)

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

ans = 0
cnt = 0
for i in range(n):
    cnt += id_card[i]
    if cnt == m:
        ans += 1

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

L = [L_R[i][0] for i in range(M)]
R = [L_R[i][1] for i in range(M)]

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

if L_max <= R_min:
    ans = R_min - L_max + 1
else:
    ans = 0

print(ans)
n,m = map(int,input().split())
left = 0
right = 1000000000
for _ in range(m):
    a, b = (int(x) for x in input().split())
    left = max(left, a)
    right = min(right, b)
if right-left+1 < 0:
    print(0)
else:
    print(right-left+1)
n,m = map(int,input().split())
L,R=0,n
for _ in range(m):
    l,r = map(int,input().split())
    L,R = max(L,l),min(R,r)
print(max(R-L+1,0))
n, m = map(int, input().split())
lr = [[int(i) for i in input().split()] for _ in range(m)]
mi, ma = 1, n
for l, r in lr:
    mi, ma = max(mi, l), min(ma, r)
if mi <= ma:
    print(ma - mi + 1)
else:
    print(0)
n,m = map(int,input().split())

l = 0
r = (10**5)+1

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

ans = r-l+1
if ans>=0:
    print(ans)
else:
    print(0)
n,m = list(map(int, input().split()))

gates = [0] * (n + 2)

for _ in range(m):
    l,r = list(map(int, input().split()))
    gates[l] += 1
    gates[r+1] -= 1

from itertools import accumulate
gates = list(accumulate(gates))

print(gates.count(m))
n, m = map(int, input().split())
apple = []
for _ in range(m):
    apple.append(list(map(int, input().split())))
cat = []
dog = []
for i in apple:
    cat.append(i[0])
    dog.append(i[1])

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

l = -float("inf")
r = float("inf")

for i in range(M):
    l_i, r_i = map(int, input().split())
    if l_i > l:
        l = l_i
    if r_i < r:
        r = r_i

if (l <= r):
    print(r-l+1)
else:
    print(0)
hoge = input().split()
N = int(hoge[0])
M = int(hoge[1])

mi = 0
ma = N

for j in range(M):
    i = input().split(" ")
    l = int(i[0])
    r = int(i[1])
    if l >= mi:
        mi = l
    if r <= ma:
        ma = r
r = ma - mi + 1
if r > 0:
    print(r)
else:
    print(0)
n,m = map(int,input().split())
li,lia = [],[]
for i in range(m):
    a,b = map(int,input().split())
    li.append(a)
    lia.append(b)
if min(lia)-max(li)+1 <= 0:
    print(0)
else:
    print(min(lia)-max(li)+1)
n,m=map(int,input().split())
l=0
r=99999999
for i in range(m):
  a,b=map(int,input().split())
  l=max(l,a)
  r=min(r,b)
if r>=l:
  print(r-l+1)
else:
  print(0)
n, m = map(int, input().split())
L, R = 0, n
for i in range(m):
    l, r = map(int, input().split())
    if l > L:
        L = l
    if r < R:
        R = r
if R-L+1 > 0:
    print(R-L+1)
else:
    print(0)
N, M = map(int, input().split())

in_min = 1
in_max = N

for i in range(M):
	l, r = map(int, input().split())
	# 最小値(今の最小値と取得値のうち、大きいほう)
	in_min = max([in_min, l])
	# 最大値(今の最大値と取得値のうち、小さいほう)
	in_max = min([in_max, r])

# 個数を出力
if(in_max < in_min):
	# 最小値が大きくなった場合は、通らない
	print(0)
else:
	cnt = in_max - in_min +1
	print(cnt)
n, m = map(int, input().split())

L = 1
R = 10**5
ans = 1

for i in range(m):
    l, r = map(int, input().split())
    if L <= l <= R:
        L = l
    if L <= r <= R:
        R = r
    if R < l or r < L:
        ans = 0
        break

if ans != 0:
    ans = R - L + 1

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

l = 0
r = N

for i in range(M):
    L, R = map(int, input().split())
    
    if L > l:
        l = L
            
    if R < r:
        r = R
                
    ans = r - l + 1
    if ans < 0:
        print(0)
        break

else:
    print(ans)
N,M=map(int,input().split())
L=-10**9
R=+10**9
for _ in range(M):
    l,r=map(int,input().split())
    L=max(L,l)
    R=min(R,r)
print(max(0,R-L+1))
N, M = map(int, input().split())

L = []

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

res = [L[0][0], L[0][1]]

for i in range(M):
    res = [max(res[0], L[i][0]), min(res[1], L[i][1])]

if res[1] < res[0]:
    print(0)
else: 
    print(res[1] - res[0] + 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)
print(min(R)-max(L)+1 if min(R)-max(L)+1>0 else 0)
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)
Lmax = max(L)
Rmin = min(R)
s = range(Lmax,Rmin+1)
print(len(s))
n, m = map(int, input().split())
l = list()
r = list()
for i in range(m):
    tmp_l, tmp_r = map(int, input().split())
    l.append(tmp_l)
    r.append(tmp_r)
maxl = max(l)
minr = min(r)
if maxl > minr:
    print(0)
elif maxl == minr:
    print(1)
else:
    print(minr - maxl + 1)
n, m = map(int, input().split())
lmax = 1
rmin = n
for i in range(m):
  l, r = map(int, input().split())
  lmax = max(l, lmax)
  rmin = min(r, rmin)
print(rmin - lmax + 1 if rmin - lmax + 1 >= 0 else 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())
if min(R) - max(L) >= 0:
    print(min(R)-max(L)+1)
else:
    print(0)
n,m = map(int,input().split())
l = []
r = []

while True:
  try:
    tl,tr = map(int,input().split())
    l.append(tl)
    r.append(tr)
  except:
    break

R = min(r)
L = max(l)
cnt = 0
for i in range(1,n+1):
  if i>=L and i<=R:
    cnt +=1
    
print(cnt)
n, m = map(int, input().split())
max_l = 0
min_r = n
for _ in range(m):
    l, r = map(int, input().split())
    max_l = max(l, max_l)
    min_r = min(r, min_r)
print(max(min_r - max_l + 1, 0))
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)
m = min(r) - max(l)
if m < 0:
    print(0)
else:
    print(m + 1)
n,m = map(int, input().split())
l = []
for _ in range(m):
    data = list(map(int,input().split()))
    l.append(data)

l_max=0
r_min=10**9
for tmp in l:
    l=tmp[0]
    r=tmp[1]
    if l_max < l:
        l_max=l
    if r_min > r:
        r_min=r
ans = r_min-l_max+1
if ans >= 0:
    print(ans)
else:
    print(0)
n, m = map(int, input().split())
l_max = 0
r_min = float('inf')
for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)
if r_min >= l_max:
    print(r_min - l_max + 1)
else:
    print('0')
n,m = map(int,input().split())
lr = [list(map(int,input().split())) for _ in range(m)]

l = 0
r = 10**10

for l1,r1 in lr:
    l = max(l, l1)
    r = min(r, r1)

print(max(0, r - l + 1))
n, m = map(int, input().split())
L = 1
R = n
for _ in range(m):
   l, r = map(int, input().split())
   L = max(L, l)
   R = min(R, r)

if (L <= R):
   print(R - L + 1)
else:
   print(0)
n,m=map(int,input().split())

left=1
right=n

for i in range(m):
    p,q=map(int,input().split())
    left=max(left,p)
    right=min(right,q)

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

imos = [0] * (N + 1)

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

for i in range(N):
    imos[i + 1] = imos[i] + imos[i + 1]

# print(imos)
print(imos[:-1].count(M))
A, B = map(int, input().split())
L = 0
R = A
for i in range(B):
  x,y = map(int, input().split())
  L = max(L,x)
  R = min(R,y)
ans = R-L+1 if R>=L else 0

print(ans)
N,M = map(int,input().split())
MAXL = -1
MINR = float("inf")
for _ in range(M):
    L,R = map(int,input().split())
    MAXL = max(MAXL, L)
    MINR = min(MINR, R)

print(max(0, MINR - MAXL + 1))
A = list(map(int , input().split()))
sc = [list(map(int, input().split())) for i in range(A[1])]
a = []
b = []
for i in sc:
    a.append(i[0])
    b.append(i[1])
if max(a) <= min(b):
    ans = min(b) - max(a) + 1
else:
    ans = 0
print(ans)
import sys
n, m = map(int, input().split())

min_id = 1
max_id = n
for i in range(m):
    l, r = map(int, input().split())

    if min_id < l <= max_id:
        min_id = l
    elif max_id < l:
        print(0)
        sys.exit()

    if min_id <= r < max_id:
        max_id = r
    elif r < min_id:
        print(0)
        sys.exit()

print(max_id - min_id + 1)
n,m=map(int,input().split())
L=1
R=10**5
for i in range(m):
  l,r=map(int,input().split())
  L,R = max(l,L), min(r,R)
  if L>R:
    print(0)
    exit()
print(R-L+1)
n,m=map(int,input().split())
l,r=map(int,input().split())
l_max,r_min=l,r
for i in range(m-1):
    l,r=map(int,input().split())
    if l_max<l: l_max=l
    if r_min>r: r_min=r
print(max(r_min-l_max+1,0))
n,m=map(int,input().split())
a=[]
for x in range(m):
  s=list(map(int,input().split()))
  a.append(s)
  
min1=1
max1=n

for y in range(m):
  l=a[y][0]
  r=a[y][1]
  if min1<l:
    min1=l
  if max1>r:
    max1=r
    
if max1-min1>=0:
  print(max1-min1+1)
  
else:
  print(0)
n, m = map(int, input().split())
l_max = 0
r_min = 100001
for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)

ans = max(r_min - l_max + 1, 0)
print(ans)
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
L, R = [list(i) for i in zip(*LR)]

l = 0
r = N+1

for i in range(M):
    l = max(l, L[i])
    r = min(r, R[i])

print(max(0, r-l+1))
def solve():
    N, M = map(int, input().split())
    Count = [0] * (N + 1)
    for i in range(M):
        l, r = map(int, input().split())
        Count[l-1] += 1
        Count[r] -= 1
    ans = 0
    if Count[0] == M: ans = 1
    for i in range(1, N):
        Count[i] += Count[i-1]
        if Count[i] == M: ans += 1
    print(ans)


if __name__ == "__main__":
    solve()
N, M = map(int, input().split())
L = []
R = []
for i in range(M):
    x1, y1 = [int(i) for i in input().split()]
    L.append(x1)
    R.append(y1)

high=N
low=0

ans = [True for i in range(N)]
for i in range(M):
    for c,k in enumerate(range(L[i]-2, low-1, -1)):
        if(low < k):
            low=k
        ans[k]=False


    for c,j in enumerate(range(R[i],high)):
        if(high > j):
            high=j
        ans[j]=False


print(ans.count(True))
def main():
    N, M = (int(i) for i in input().split())
    A = [[int(i) for i in input().split()] for j in range(M)]
    S = [0]*(N+2)
    for le, ri in A:
        S[le] += 1
        S[ri+1] += -1
    ans = 0
    for i in range(N):
        S[i+1] += S[i]
        if S[i+1] == M:
            ans += 1
    # print(S)
    print(ans)


if __name__ == '__main__':
    main()
N, M = list(map(int, input("").split(" ")))
permit = [list(map(int, input("").split(" "))) for i in range(M)]
l,r = 1, N
for i,j in permit:
  l = max(l, i)
  r = min(r, j)
print(max(r-l+1,0))
n, m = map(int, input().split())
L = 0
R = 100000

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

if L <= R:
    print(R - L + 1)
else:
    print(0)
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)
#C
n, m = 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)
n, m = map(int, input().split())
ok = [0] * (n + 2)
ans = 0
for _ in range(m):
    l, r = map(int, input().split())
    ok[l] += 1
    ok[r + 1] -= 1
for i in range(1, n + 2):
    ok[i] += ok[i - 1]
    if ok[i] == m:
        ans += 1
print(ans)
N,M=map(int,input().split())
Lmax = 0
Rmin = 1e8
for _ in range(M):
    L,R=map(int,input().split())
    if L > Lmax:
        Lmax = L
    if R < Rmin:
        Rmin = R

print(max(0, Rmin - Lmax + 1))
n,m=map(int,input().split())
l=0;r=10**6
for i in range(m):
    ln,rn=map(int,input().split())
    l=max(l,ln);r=min(r,rn)
if l>r:
    print(0)
else:
    print(r-l+1)
N, M = list(map(int, input().split()))

min_num = 1
max_num = N
for i in range(M):
    min_input, max_input = list(map(int, input().split()))
    if min_input > min_num:
        min_num = min_input
    
    if max_input < max_num:
        max_num = max_input

output = max_num- min_num + 1

if output <= 0:
	print(0)
else :
  	print(output)
n,m = map(int,input().split())
ans = [1, n]
for i in range(m):
    l,r = map(int,input().split())
    if r < ans[0] or ans[1] < l:
        print(0)
        exit()
    elif ans[0] <= l and r <= ans[1]:
        ans = [l,r]
    else:
        ans = [max(ans[0], l), min(ans[1], r)]
print(ans[1]-ans[0] + 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)
count=0
if max(L)>min(R):
  print(0)
else:
  print(min(R)-max(L)+1)
import numpy as np
N,M=map(int,input().strip().split())
LR=np.array([list(map(int,input().strip().split())) for _ in range(M)])
print(max(min(LR[:,1])-max(LR[:,0])+1,0))