peterverebics/CodeNet_Python_118 huggingface dataset:
# input = sys.stdin.readline

def resolve():
    N,M=map(int,input().split())
    l, r = map(int, input().split())
    for i in range(1,M):
        ll,rr=map(int,input().split())
        l=max(l,ll)
        r=min(r,rr)

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


resolve()
n, m = map(int, input().split())
lrl = list(list(map(int, input().split())) for _ in range(m))
lmax = 0
rmin = n

for lr in lrl:
    if lr[0] > lmax:
        lmax = lr[0]
    if lr[1] < rmin:
        rmin = lr[1]
if lmax <= rmin:
    print(rmin-lmax+1)
else:
    print(0)
N, M = map(int, input().split())
minimum = []
maximum = []
for i in range(M):
  L, R = map(int, input().split())
  minimum.append(L)
  maximum.append(R)
  
ans = int(min(maximum) - max(minimum) + 1)
if ans < 0:
  print(0)
else:
  print(ans)
N, M = map(int, input().split())
lmax = 0
rmin = 10**5
for i in range(M):
    L, R = map(int, input().split())
    lmax = max(lmax, L)
    rmin = min(rmin, R)
ans = rmin - lmax
if ans >= 0:
    print(ans + 1)
else:
    print(0)
n,m=map(int,input().split())
ltmp=1
rtmp=n
for i in range(m):
  l,r=map(int,input().split())
  ltmp=max(l,ltmp)
  rtmp=min(r,rtmp)
if ltmp>rtmp:
  print(0)
else:
  print(rtmp-ltmp+1)
# coding: utf-8
import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
l_list = []
r_list = []
for i in range(M):
    l, r = lr()
    l_list.append(l)
    r_list.append(r)

for i in range(M):
    if i == 0:
        maxL = l_list[i]
        minR = r_list[i]
        continue
    maxL = max(maxL, l_list[i])
    minR = min(minR, r_list[i])
ans = max(minR - maxL + 1, 0)
print(ans)
n, m = map(int, input().split())
l, r = [0] * m, [0] * m
for i in range(m):
    li, ri = map(int, input().split())
    l[i] = li
    r[i] = ri
maxl = max(l)
minr = min(r)
if minr < maxl:
    print(0)
else:
    print(minr - maxl + 1)
n,m = list(map(int,input().split()))
la = 0
ra = n
for i in range(m):
    l,r=list(map(int,input().split()))
    la = max(la,l)
    ra = min(ra,r)
print(max(0,ra-la+1))
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]
f_inf = float("inf")

max_l = 0
min_r = f_inf
for l, r in LR:
    max_l = max(max_l, l)
    min_r = min(min_r, r)

if min_r >= max_l:
    print(min_r - max_l + 1)
else:
    print(0)
N, M = map(int,input().split())

L = 1
R = N

for m in range(M):
    L_, R_ = map(int,input().split())
    if L < L_:
        L = L_
    if R_ < R:
        R = R_

if R < L:
    print(0)
else:
    print(R-L+1)
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)
if l > r:
    print(0)
else:
    print(r-l+1)
def abc127c():
    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 = max(min(rs)-max(ls)+1, 0)
    print(ans)
abc127c()
N,M = map(int,input().split())

L_max = 0
R_min = N

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

print(max(R_min-L_max+1,0))
def INT():
    return int(input())

def LI():
    return list(map(int, input().split()))

def MI():
    return map(int, input().split())

N, M = MI()
gate = [0] * (N + 2)
ans = 0

for _ in range(M):
    L, R = MI()
    gate[L] += 1
    gate[R + 1] -= 1

for i in range(1, N + 1):
    gate[i] += gate[i - 1]
    
for i in range(1, N + 1):
    if gate[i] == M:
        ans += 1
        
print(ans)
N, M = map(int, input().split())
LR = [tuple(map(int, input().split())) for i in range(M)]

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

ans = max(0, r - l + 1)
print(ans)
# 36
n, m = map(int, input().split())
l_max, r_min = -1, float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    l_max = max(l_max, li)
    r_min = min(r_min, ri)

ans = r_min - l_max + 1 if (r_min - l_max >= 0) else 0
print(ans)
n,m= map(int,input().split())
l1,r1 = map(int,input().split())
lis = [l1,r1]

for i in range(m-1):
  l,r = map(int,input().split())
  if lis[0]<=l<lis[1] and lis[0]<r<=lis[1]:
    lis = [l,r]
  elif (lis[1]<l and lis[1]<r) or (l<lis[0] and r<lis[0]):
    print(0)
    exit()
  elif lis[0]<l<=lis[1] and lis[1]<r:
    lis[0] = l
  elif l<lis[0] and lis[0]<=r<lis[1]:
    lis[1] = r
    
print(lis[1]-lis[0]+1)
n, m = map(int, input().split())
cardid_2_num_map = [0] * (10**5 + 2)

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

for i in range(1, n + 1):
    cardid_2_num_map[i] += cardid_2_num_map[i - 1]

max_sum_num = max(cardid_2_num_map)
count_max_card = cardid_2_num_map.count(max_sum_num)

if max_sum_num == m:
    print(count_max_card)
else:
    print(0)
n,m=map(int,input().split())

bottom=0
top=10**5
for i in range(m):
  a,b=map(int,input().split())
  bottom=max(bottom,a)
  top=min(top,b)
  
if top<bottom:
  print(0)
else:
  print(top-bottom+1)
n ,m = map(int, input().split())
l = set()
r = set()
for i in range(m):
  li ,ri = map(int, input().split())
  l.add(li)
  r.add(ri)
r.add(n)
lm = max(l)
rm = min(r)
if lm > rm:
  ans = 0
else:
  ans = rm-lm+1
print(ans)
# -*- coding: utf-8 -*-
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)
    L.sort(reverse=True)
    R.sort()
    lmax = L[0]
    rmin = R[0]
    print(max(0,rmin-lmax+1))



if __name__ == '__main__':
    main()
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())

ll = max(l)
rr = min(r)
if rr-ll >= 0:
    print(rr-ll+1)
else:
    print(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)

limit_min = max(l_list)
limit_max = min(r_list)
cnt = 0

for i in range(1, n + 1):
	if limit_min <= i <= limit_max:
		cnt += 1

print(cnt)
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)
big=min(r)
small=max(l)
print(big-small+1 if big-small>=0 else 0)
def main():
    N, M = map(int, input().split())
    left, right = 0, N-1
    for _ in range(M):
        l, r = map(int, input().split())
        left = max(left, l-1)
        right = min(right, r-1)
    
    print(max(0, right - left + 1))

if __name__ == "__main__":
    main()
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

if L > R:
    print(0)
else:
    print(R - L + 1)
def main():
    import sys

    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

N, M = map(int, input().split())
l, r = None, None
for _ in range(M):
    L, R = map(int, input().split())
    if l is None or l < L:
        l = L
    if r is None or R < r:
        r = R
print(max(0, r - l + 1))
N,M = map(int,input().split())

left=1
right=pow(10,5)

for i in range(M):
    L,R=map(int,input().split())
    left=max(left, L)
    right=min(right,R)
ans=right-left+1
print(0 if left>right else ans)
N, M = map(int, input().split())
sp = 0
ep = 10**5+1
for i in range(M):
    L, R = map(int, input().split())
    sp = max(sp, L)
    ep = min(ep, R)

print(max(0, ep-sp+1))
from sys import stdin
import numpy as np
N, M = [int(x) for x in stdin.readline().rstrip().split()]
C = np.zeros(N)
Rmin = N
Lmax = 1
for i in range(M):
    L, R = [int(x) for x in stdin.readline().rstrip().split()]
    if R < Rmin:
        Rmin = R
    if L > Lmax:
        Lmax = L
if Lmax > Rmin:
    print(0)
else:
    print(N-(Lmax-1)-(N-Rmin))
"""
i番目のゲートは LiからRiまでのIDカードのうちどれか1枚を持っていれば通過できます
"""

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

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

print(max(right-left+1,0))
n,m = map(int,input().split())
l,r = [],[]
for i in range(m):
    x,y = map(int,input().split())
    l.append(x)
    r.append(y)
print(max(0,min(r)-max(l)+1))
N,M=list(map(int,input().split()))
l=1
r=N
for m in range(M):
    l_,r_ = list(map(int,input().split()))
    l = max(l,l_)
    r = min(r,r_)
    
ans = r-l+1

if ans<0:
    ans=0
print(ans)
n, m = map(int, input().split())
l, r = 0, 100001
for i in range(m):
    x, y = map(int, input().split())
    if l <= x:
        l = x
    if r >= y:
        r = y
print(len([x for x in range(l, r+1)]))
n,m = map(int,input().split())

ans,ansf = 1,n

for i in range(m):
    l,r = map(int,input().split())
    ans = max(ans,l)
    ansf = min(ansf,r)
    
if ansf - ans <0:
    print(0)
else:
    print(ansf-ans+1)
N,M = map(int, input().split())
L,R = map(int, input().split())

for _ in range(M-1):
    l,r = 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)
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(min(r) - max(l) + 1, 0))
N, M = map(int, input().split())
A = [1, N]
for i in range(M):
  L, R = map(int, input().split())
  A = [max(A[0], L), min(A[1], R)] 
print(max(0, (A[1] - A[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 min(R)-max(L)+1>=0:
    print(min(R)-max(L)+1)
else:
    print(0)
N,M=map(int,input().split())
G=[list(map(int,input().split())) for _ in range(M)]
mi=0
ma=float("inf")
for i in range(M):
    if G[i][0]>mi:
        mi=G[i][0]
    if G[i][1]<ma:
        ma=G[i][1]
    #print(mi,ma)
print(max(ma-mi+1,0))
n,m = map(int, input().split())
Lmin = 0
Rmax = n
for i in range(m):
    L,R = map(int, input().split())
    Lmin = max(L-1, Lmin)
    Rmax = min(R, Rmax)
if Lmin < Rmax:
    ans = Rmax - Lmin
else:
    ans = 0
print(ans)
def main():
    import sys
    input = sys.stdin.readline

    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
    ans=0
    for i in range(n):
        if CARD[i]==m:ans+=1
        CARD[i+1]=CARD[i+1]+CARD[i]

    print(ans)

if __name__=='__main__':
    main()
N,M=map(int,input().split())
diff=[0 for _ in range(N+1)]
for _ in range(M):
    L,R=map(int,input().split())
    diff[L-1]+=1
    diff[R]-=1
ans=0
x=0
for i in range(N):
    x+=diff[i]
    if x==M:
        ans+=1
print(ans)
N, M = map(int, input().split())
LR = [tuple(map(int, input().split())) for _ in range(M)]

max_l, min_r = 0, N

for l, r in LR:
    max_l = max(l, max_l)
    min_r = min(r, min_r)
    if min_r < max_l:
        print(0)
        exit()

print(min_r - max_l + 1)
N,M = map(int,input().split())
LR = [[int(x) for x in input().split()] for _ in range(M)]
L = 1
R = N
for l,r in LR:
  L = max(L,l)
  R = min(R,r)
  
if L > R:
  print(0)
else:
  print(R-L+1)
n, m = map(int, input().split())
lmax = 0
rmin = 1000000
for _ in range(m):
    l, r = map(int, input().split())
    lmax = max(lmax, l)
    rmin = min(rmin, r)
if lmax > rmin:
    print(0)
else:
    print(rmin-lmax+1)
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:
    if left < l:
        left = l
    if right > r:
        right = r

ans = right - left

print(ans + 1 if ans >= 0 else 0)
N,M=map(int,input().split())
LR=[list(map(int,input().split())) for i in range(M)]
l=1
r=N
for lr in LR:
    if l<lr[0]:
        l=lr[0]
    if lr[1]<r:
        r=lr[1]
print(max(0,r-l+1))
n, m = map(int, input().split())
max_l = 0
min_r = 10**5
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
res = min_r - max_l + 1
if res<0:
  res = 0
print(res)
n,m = map( int,input().split() )
a = [ list(map(int,input().split())) for _ in range(m)]

#n,m = 5,3
#a = [[3,6],[5,7],[6,9]]

L = a[0][0]
R = a[0][1]

for i in range(1,m):
  L = max(L,a[i][0])
  R = min(R,a[i][1])

R = min(R,n)
if R >= L :
  print(R-L+1)
else:
  print(0)
N_ID, M_gate = map(int, input().split())
mini = float('inf')
MAX = 0

for i in range(M_gate):
    L, R = map(int, input().split())
    MAX = max(MAX, L)
    mini = min(mini, R)

answer = max(0, mini - MAX + 1)
print(answer)
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)
  
ans=min(R)-max(L)+1
if ans>=0:
  print(ans)
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.sort()
R.sort()

if L[-1] > R[0]:
    print(0)
else:
    print(R[0] - L[-1] + 1)
n, m = [int(i) for i in input().split()]
left, right = 0, n
for i in range(m):
    l, r = [int(i) for i in input().split()]
    left = max(left, l)
    right = min(right, r)
print(max(right - left + 1, 0))
n, m = map(int, input().split())
Lmax = 0
Rmin = n
for _ 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())

L,R= map(int,input().split())

for i in range(M-1):
    x,y= map(int,input().split())
    L = max(L,x)
    R = min(R,y)

print(R-L+1 if L<=R else 0)
n,m = map(int,input().split())
a = []
lmax = 0
rmin = 10**6
for i in range(m):
    a.append(list(map(int,input().split())))
    if a[i][0] > lmax:
        lmax = a[i][0]
    if a[i][1] < rmin:
        rmin = a[i][1]
if rmin-lmax < 0 :
    print(0)
else :   
    print(1+rmin-lmax)
n,m = map(int,input().split())
lr = [list(map(int,input().split()))for i in range(m)]

low = 1
high = 10**5

for l,r in lr:
    if low <= l:
        low = l 
    if r <= high:
        high = r 

if high < low:
    print(0)
    exit()
print(high - low + 1)
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)

print(max(0, right - left + 1))
N, M = map(int, input().split())
LR = [(map(int, input().split())) for i in range(M)]

L = 1
R = N
for i in range(M):
    a, b = LR[i]
    L = max(L,a)
    R = min(R,b)
if R-L+1>=0:   
	print(R-L+1)
else:
  	print(0)
n,m=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])

print(max(min(r)-max(l)+1,0))
n, m = map(int, input().split())
lrs = [ list(map(int, input().split())) for _ in range(m)]

cards = [lrs[0][0], lrs[0][1]] # [min, max]

for lr in lrs[1:]:
    if (lr[1] < cards[0]) or (lr[0] > cards[1]):
        print(0)
        exit()

    cards[0] = max(cards[0], lr[0])
    cards[1] = min(cards[1], lr[1])
    

print(cards[1] - cards[0] + 1)
n, m = map(int, input().split())

min_l = 0
max_l = 10**6

for _ in range(m):
    L, R = map(int, input().split())
    min_l = max(min_l, L)
    max_l = min(max_l, R)

print(max_l - min_l + 1 if max_l - min_l + 1 > 0 else 0)
n, m = map(int, input().split())
l_lst = [0] * m
r_lst = [0] * m
for i in range(m):
    l, r = map(int, input().split())
    l_lst[i] = l
    r_lst[i] = r

if max(l_lst) <= min(r_lst):
    print(min(r_lst) - max(l_lst) + 1)
else:
    print(0)
N, M = map(int, input().split())

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

for m in range(M):
  L[m], R[m] = 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)
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)
L1 = max(l)
R1 = min(r)
if R1 >= L1:
    print(R1 - L1 + 1)
else:
    print(0)
n,m=map(int,input().split())
low=1
high=n
for i in range(m):
  l,r=map(int,input().split())
  if l>low:
    low=l
  if r<high:
    high=r
if high>=low:
  print(high-low+1)
else:
  print(0)
N, M = map(int, input().split())

low = 1
high = N
for i in range(M) :
    l, r = map(int,input().split())
    low = max(low, l)
    high = min(high, r)

if high - low + 1 > 0 :
    print(high - low + 1)
else :
    print(0)
n, m=map(int, input().split())
l1 = [[] for _ in range(n)]
mn=1
ma=n
for i in range(1, m+1):
  l, r=map(int, input().split())
  mn=max(mn, l)
  ma=min(ma, r)
  if ma<mn:
    print(0)
    exit()
print(ma-mn+1)
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(max(0, min(R) - max(L) + 1))
N,M= map(int,input().split())
gate=[list(map(int,input().split())) for i in range(M)]
count2=0

min1=1
max1=N

for j in gate:
  if j[0]>min1:
    min1=j[0]
  if j[1]<max1:
    max1=j[1]
if max1>=min1:
  print(max1-min1+1)
else:
  print(0)
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for i in range(m)]
max = n
min = 1

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

ans = max - min + 1

if ans > 0:
  print(ans)
else:
  print(0)
import sys
input=sys.stdin.readline

n,k=map(int,input().split())
L=[0]*(n+1)
und=0
top=10**5
for i in range(k):
    a,b=map(int,input().split())
    und=max(und,a)
    top=min(b,top)
print(top-und+1 if top>=und 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)
res=min(R)-max(L)+1
if res<0:
	print("0")
else:
	print(res)
n,m=map(int,input().split())
L=0
R=n+1
for i in range(m):
    l,r=map(int,input().split())
    L=max(L,l)
    R=min(R,r)
print(max(R-L+1,0))
n,m = map(int,input().split())
min_card,max_card = 0,n
for i in range(m):
    l,r = map(int,input().split())
    min_card = max(min_card,l)
    max_card = min(max_card,r)
#print(min_card,max_card)
if min_card<=max_card:
    print(max_card-min_card+1)
else:
    print(0)
N,M=map(int,input().split())
Lmax=1
Rmin=N
for i in range(M):
    L,R=map(int,input().split())
    Lmax=max(Lmax,L)
    Rmin=min(Rmin,R)
    
print(max(Rmin-Lmax+1,0))
n, m = list(map(int, input().split()))

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

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

ans = [1]*n

s = 0
m = n

for i in list:
    if i[0] > s:
        s = i[0]
    if i[1] < m:
        m = i[1]
if m>=s:
    print(m-s+1)
else:
    print(0)
n,m=map(int,input().split())
mn=0
mx=10**6
for i in range(m):
    a,b=map(int,input().split())
    mn=max(mn,a)
    mx=min(mx,b)
if mn>mx:
    print(0)
else:
    print(mx-mn+1)
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]

max_L = 0
min_R = float("inf")
for i in range(m):
    L, R = LR[i]
    max_L = max(max_L, L)
    min_R = min(min_R, R)
if min_R - max_L < 0:
    print(0)
else:
    print(min_R - max_L + 1)
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)
ans=rm-lm+1
print(ans if ans>=0 else 0)
import numpy as np
n, m = map(int, input().split())
ans = [0]*n

for _ in range(m):
    l, r = map(int, input().split())
    ans[l-1] += 1
    if r < n:
        ans[r] -= 1

c = list(np.cumsum(ans))
print(c.count(m))
N, M=map(int, input().split())

a=1
b=N
for i in range(M):
  L,R=map(int, input().split())
  a=max(a,L)
  b=min(b,R)
  
if b>=a:
  print(b-a+1)
else:
  print(0)
N,M=map(int,input().split())
left=1
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=1
r=N
for i in range(M):
  L,R = map(int,input().split())
  l = max(l,L)
  r = min(r,R)
ans = r-l+1
if ans >= 0:
  print(ans)
else:
  print(0)
N,M = map(int,input().split())
maL = 0
miR = N
for g in range(M):
  L,R = map(int,input().split())
  maL = max(maL,L)
  miR = min(miR,R)
if maL <= miR:
  print(miR-maL+1)
else:
  print(0)
def main():
    n,m = map(int,input().split())
    l = 0
    r = float('inf')
    for i in range(m):
        l_,r_ = map(int,input().split())
        if l<l_:
            l = l_
        if r_<r:
            r = r_
    if r-l>=0:
        print(r-l+1)
    else:
        print(0)

if __name__ == "__main__":
    main()
import itertools
import math
import fractions
import functools
import copy
n, m = map(int, input().split())
lr = []
for i in range(m):
    l, r = map(int, input().split())
    lr.append([l,r])

result = list(range(lr[0][0],lr[0][1]+1))


maximum = lr[0][1]
minimum = lr[0][0]
for i in range(len(lr)):
    maximum = min(maximum,lr[i][1])
    minimum = max(minimum,lr[i][0])

if maximum < minimum:
    print(0)
else:
    print(maximum-minimum+1)
f=lambda:map(int,input().split())
n,m=f()
L,R=0,n
for i in range(m):
  l,r=f()
  L=max(L,l)
  R=min(R,r)
print(max(0,R-L+1))
n, m = map(int, input().split())
x = [0] * (n+2)
for i in range(m):
    l, r = map(int, input().split())
    x[l] += 1
    x[r+1] -= 1

ans = 0
for i in range(1, n+1):
    x[i] += x[i-1]
    if x[i] == m:
        ans += 1
print(ans)
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

A = [0] * (N + 1)
for i in range(M):
    l, r = map(int, input().split())
    l -= 1
    A[l] += 1
    A[r] -= 1
for i in range(1, N):
    A[i] += A[i - 1]
ans = 0
for a in A:
    if a == M:
        ans += 1
print(ans)
N, M = map(int,input().split())
l = 1; r = N;
for i in range(M):
  a, b = map(int,input().split())
  l = max(l, a)
  r = min(r, b)
print(max(0, r-l+1))
r=input().split()
N=int(r[0])
M=int(r[1])
data=[[int(s) for s in input().split()] for i in range(M)]
left=[]
right=[]
for i in range(M):
    left.append(data[i][0])
    right.append(data[i][1])
if min(right)-max(left)+1<=0:
    print(0)
else:
    print(min(right)-max(left)+1)
#C - Prison
N,M = map(int, input().split())
L = [list(map(int, input().split())) for i in range(M)]
a_max = 0
b_min = 10**9
for i in range(M):
    if b_min >= L[i][1]:
        b_min = L[i][1]
    else:
        pass
    if a_max <= L[i][0]:
        a_max = L[i][0]
    else:
        pass

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

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


    if max <= min:
        print(min - max +1)
    else:
        print(0)
N,M = map(int,input().split(" "))
Min = 1
Max = N

for _ in range(M):
    L,R = map(int,input().split(" "))
    if L>Min:
        Min=L
    if R<Max:
        Max=R
if Max-Min>=0:
    print(Max-Min+1)
else:
    print(0)
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())

lmax=max(l)
rmin=min(r)
if rmin>=lmax:
  print(rmin-lmax+1)
else:
  print(0)
N, M = map(int, input().split())

left = 1
right = N

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

print(max(right - left + 1, 0))