peterverebics/CodeNet_Python_118 huggingface dataset:
N,M = map(int,input().split())
LR = [[i for i in map(int,input().split())] for j in range(M)]
start = 1
end = N
for i in range(M):
  if start < LR[i][0]: start = LR[i][0]
  if end > LR[i][1]: end = LR[i][1]
    
ans = end-start+1
if ans < 0: ans = 0
print(ans)
n, m = map(int, input().split())
l, r = map(int, input().split())
maxL = l
minR = r
for i in range(m-1):
  l, r = map(int, input().split())
  if maxL < l:
    maxL = l
  if minR > r:
    minR = r
    
ans = min(minR, n)
if ans - maxL+1 > 0:
  print(ans - maxL+1)
else:
  print(0)
import bisect,collections,copy,itertools,math,numpy,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
N,M = LI()
LR = [LI() for _ in range(M)]
a = 0
b = float('INF')
for x in LR:
    a = max(x[0],a)
    b = min(x[1],b)
print(b-a+1 if b-a+1>=0 else 0)
N,M=map(int,input().split())
_min,_max=map(int,input().split())
for i in range(M-1):
   L,R=map(int,input().split())
   _min,_max=max(_min,L),min(_max,R)
print(max(_max+1-_min,0))
import sys
import copy
import math
import itertools
import numpy as np
l = [int(c) for c in input().split()]
N=l[0]
M=l[1]
LR=[list(map(int,input().split())) for c in range(M)]
small = 0
large = N
for i in range(M):
    if small<LR[i][0]:
        small=LR[i][0]
    
    if large>LR[i][1]:
        large=LR[i][1]

if  small <= large:
    print(large-small+1)
else:
    print(0)
n, m = map(int, input().split())
L = []
R = []
for i in range(m):
  t1, t2 = map(int, input().split())
  L.append(t1)
  R.append(t2)

maxl = max(L)
minr = min(R)
ans = minr - maxl + 1

if ans <= 0:
  print(0)
else:
  print(ans)
N, M = [int(x) for x in input().split()]
L, R = zip(*( [[int(x) for x in input().split()] for _ in range(M)]))

l = 0
r = 10 ** 5 + 1
for i in range(M):
    l = max(l, L[i])
    r = min(r, R[i])

print( max(r - l + 1, 0))
n,m = map(int,input().split())
lr_list = [list(map(int,input().split())) 
           for nesya in range(m)]
l = 0
r = n
for lr in lr_list:
  l = max(l,lr[0])
  r = min(r,lr[1])
if l > r:
  print(0)
else:
  print(r-l+1)
n,m=map(int,input().split())
l,r=[],[]

for i in range(m):
  ll,rr=map(int,input().split())
  l.append(ll)
  r.append(rr)
  
print(min(r) - max(l) + 1 if min(r)-max(l) >= 0 else 0)
n, m = map(int, input().split())
l = []
r = []
for i in range(m):
  li, ri = map(int, input().split())
  l.append(li)
  r.append(ri)
maxl = -10**5-1
minr = 10**5+1
for i in range(m):
  maxl = max(maxl, l[i])
  minr = min(minr, r[i])
print(max(minr - maxl + 1, 0))
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n,m=mp()
a=-float("inf")
b=float("inf")
for i in range(m):
    l,r=mp()
    a=max(a,l)
    b=min(b,r)
print(max(b-a+1,0))
N,M = map(int,input().split())
right = N
left = 1
for _ in range(M):
  a,b = map(int,input().split())
  right = min(right,b) 
  left = max(left,a)

ans = right-left+1
print(max(0,ans))
n,m=map(int,input().split())
a=[[int(i) for i in input().split()] for l in range(m)]
b=0
s=100000000000
for x in a:
    b=max(b,x[0])
    s=min(s,x[1])
print(max(0,s-b+1))
if __name__ == "__main__":
    n, m = map(int, input().split())
    lmax = 0
    rmin = n + 1
    for i in range(m):
        ltmp, rtmp = map(int, input().split())
        if ltmp > lmax:
            lmax = ltmp
        if rtmp < rmin:
            rmin = rtmp

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

lr = []

ming = 0
maxg = 10 **6

for i in range(m):
    a,b = 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]))
n, m = map(int, input().split())
L, R = 0, 100000
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))
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

N, M = map(int, input().split())
maxL = 1
minR = N
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())
mn = 0
mx = N
for i in range(M):
    L,R = map(int,input().split())
    mn = max(L,mn)
    mx = min(R,mx)
print(max(0,mx-mn+1))
n,m=map(int,input().split())
L=[]
M=[]
for i in range(m):
  l,m=map(int,input().split())
  L.append(l)
  M.append(m)
  
a=max(L)
b=min(M)
if a<=b:
  print(b-a+1)
else:
  print(0)
N, M = map(int, input().split())
L = 0
R = N+1
for _ in range(M):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r)
print(R - L + 1 if R - L + 1 >= 0 else 0)
n,m = map(int, input().split())
a = [0,n]
for i in range(m):
  s,t = map(int, input().split())
  a[0] = max(a[0], s)
  a[1] = min(a[1], t)
print(max(a[1] - a[0] + 1, 0))
n , m = map(int , input().split())
l = -10
r = 100000
for i in range(m):
  j = list(map(int , input().split()))
  l = max(l , j[0])
  r = min(r , j[1])
print(max(min(r - l + 1 , n) , 0))
n,k = 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)
n, m = map(int, input().split())
l = 1
r = 10**5
for _ in range(m):
  l_, r_ = map(int, input().split())
  l = max(l, l_)
  r = min(r, r_)
  
if r>=l:
  print(r-l+1)
else:
  print(0)
import sys

n, m, *lr = map(int, sys.stdin.read().split())
lr = zip(*[iter(lr)] * 2)

def main():
    cnt = [0] * (n + 1)
    for l, r in lr:
        cnt[l-1] += 1
        cnt[r] -= 1
    
    for i in range(n):
        cnt[i+1] += cnt[i]
    
    res = 0
    for i in range(n):
        res += (cnt[i] == m) & 1
    
    return res

if __name__ == '__main__':
    ans = main()
    print(ans)
N, M = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(M)]
G = [0, N]
def overlap(a, b):
    if max(a[0], b[0]) > min(a[1], b[1]):
        return [0, 0]
    return [max(a[0], b[0]), min(a[1], b[1])]
for m in range(M):
    G = overlap(G, A[m])
if G == [0, 0]:
    print(0)
else:
    print(G[1] - G[0] + 1)
from itertools import accumulate

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

card = [0]*N
for _ in range(M):
    l, r = map(lambda x: int(x)-1, input().split())
    card[l] += 1
    if r < N-1:
        card[r+1] -= 1
card = accumulate(card)
# c = list(card)
ans = 0
for ci in card:
    if ci == M:
        ans += 1
print(ans)
n,m = map(int,input().split())
ans = [-1,10**6]

for i in range(m):
    tmp = list(map(int,input().split()))
    ans[0] = max(ans[0],min(tmp))
    ans[1] = min(ans[1],max(tmp))

print(max(ans[1]-ans[0]+1,0))
N,M = map(int, input().split())
li = [list(map(int, input().split())) for i in range(M)]

mi = 0
ma = 10**9

for index, n in enumerate(li):
  if mi < n[0]:
    mi = n[0]

  if ma > n[1]:
    ma = n[1]

ans = N - (mi - 1) - (N - ma)

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

left = 0
right = 100000

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

result = right - left + 1

print(result if result >= 0 else 0)
N,M=map(int,input().split())
import heapq
L=[]
R=[]
for i in range(M):
  l,r=map(int,input().split())
  heapq.heappush(L, -l)
  heapq.heappush(R, r)
l=heapq.heappop(L)
r=heapq.heappop(R)
if -l>r:
  print(0)
else:
  print(r+l+1)
N,M = map(int,input().split())
LR = [list(map(int,input().split())) for i in range(M)]

maxi = 0
mini = 10**6
for lr in LR:
    maxi = max(lr[0],maxi)
    mini = min(lr[1],mini)

if mini-maxi+1 >= N:
    print(N)
else:
    print(mini-maxi+1)
n, m = map(int, input().split())
l_r = [ list(map(int, input().split())) for _ in range(m) ]

ans = 0
l_ans = 0
r_ans = float('inf')
for l, r in l_r:
    l_ans = max(l_ans, l)
    r_ans = min(r_ans, r)
print(r_ans-l_ans+1)
n,m = map(int, raw_input().split(' '))
def intersection(A,B):
	a,b  = max(A[0],B[0]), min(A[1],B[1])
	return [a,b] if a <= b else []

r = [1,n]
for _ in range(m):
	a,b = map(int, raw_input().split(' '))
	keys = intersection(r, [a,b])
	if len(keys) == 0: break
print (keys[1] - keys[0] + 1) if keys else 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)

if min(R) <= max(L):
  print(min(R)-max(L)+1)
else:
  print(0)
n,m = map(int,input().split())
ls = [i for i in range(m)]
rs = [i for i in range(m)]
cnt = 0
for _ in range(m):
    ls[_],rs[_] = map(int,input().split())

maxl = max(ls)
minr = min(rs)

for nn in range(1,n+1):
    if maxl <= nn or minr >= nn:
        cnt += 1

print(cnt)
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)
print(r-l+1)
n, m = map(int, input().split())
lr = [[int(i) for i in input().split()] for _ in range(m)]
mi, ma = 0, n
for l, r in lr:
    mi, ma = max(mi, l), min(ma, r)
print(ma - mi + 1)
#C
n, m = 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(rmin - lmax + 1)
N,M = map(int,input().split())
A = []
B = []
for _ in range(M):
    L,R = map(int,input().split())
    A.append(L)
    B.append(R)
print(min(B)-max(A)+1)
import itertools
n,m = map(int,input().split())
card = [list(map(int,input().split())) for c in range(m)]
ans = 0
tmp = []
if m == 1:
    print(max(card[0]))
else:
    for c in card:
      c = [i for i in range(c[0],c[-1]+1)]
      tmp.append(c)

    tmp = list(itertools.chain.from_iterable(tmp))
    tmp = [x for x in set(tmp) if tmp.count(x) >= m]

    print(len(tmp))
n, m = map(int, input().split())
cnt = 0
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)
print(r_min - l_max + 1)
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n,m = i2()
l = []
r = []

for i in range(m):
	L,R = i2()
	l.append(L)
	r.append(R)

print(min(r) - max(l) + 1)
N,M=map(int,input().split())
interval=[[0,0] for i in range(M)]
for i in range(M):
  interval[i][0],interval[i][1]=map(int,input().split())
L=[interval[i][0] for i in range(M)]
R=[interval[i][1] for i in range(M)]
L_max=max(L)
R_min=min(R)
print(R_min-L_max+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 max(l)<=min(r):
    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(R-L+1)
N, M = map(int, input().split())

for i in range(M):
  L, R = map(int, input().split())
  
  if i == 0:
    top = R
    bottom = L
  
  top = min(top, R)
  bottom = max(bottom, L)

print(top-bottom+1)
N, M = map(int, input().split())
L = list(list(map(int,input().split())) for i in range(M))
l = 0
r = N
for i in range(M):
    l = max(l,L[i][0])
    r = min(r,L[i][1])
print(l-r+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)
  
ans=min(R)-max(L)+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())
if max(l)<min(r):
    print(0)
else:
    print(min(r)-max(l)+1)
#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())
list_lr=[list(map(int, input().split())) for _ in range(M)]
a=list_lr[0][1]-list_lr[-1][0]+1
print(a if a<=N else N)
n,m = map(int,input().split())
l,r = 1,n
for i in range(m):
    l_tmp,r_tmp = map(int,input().split())
    if l_tmp > l:
        l = l_tmp
    if r_tmp < r:
        r = r_tmp
print(r-l+1)
# -*- coding: utf-8 -*-
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)
Rmax = min(R)
print(Rmax-Lmax+1)
N,M=map(int,input().split())
s=[list(map(int,input().split())) for i in range(M)]


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

for j in range(M):
  for k in range(len(t)):
    if s[j][0]>t[k]:
      t[k]=0
    if s[j][1]<t[k]:
      t[k]=0
t=set(t)
print(len(t)-1)
n,m = map(int,input().split())
lmax, rmin = 0, n

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

print((rmin-lmax)+1)
#import numpy as np
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with

#A = int(input())
N,M= map(int,input().split())
#A = list(map(int,input().split()))
#B = list(map(int,input().split()))
#S= str(input())
left=1
right=10**6
for m in range(M):
    l,r = map(int,input().split())
    if left<=l and l<=right:
        left=l
    if left<=r and r<=right:
        right=r

print(right-left+1)
N, M = map(int, input().split())
L_list = []
for i in range(M):
    A = list(map(int, input().split()))
    L_list.append(A)
min_range = L_list[0][0]
max_range = L_list[0][1]
for i in range(i, M):
    if L_list[i][0] >= min_range:
        min_range = L_list[i][0]
    if L_list[i][1] <= max_range:
        max_range = L_list[i][1]
print(max_range-min_range+1)
# -*- 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())
lr = []

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

lmax = 0
mmin = 10**5

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

print(mmin - lmax + 1)
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(m)]
b=min(ab[i][1] for i in range(m))
a=max(ab[i][0] for i in range(m))
print(b-a+1)
n,m = map(int, input().split())
l,r = 0,n
for i in range(m):
    L,R = map(int, input().split())
    l = max(l,L)
    r = min(r,R)

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())
s = min(n,min(r)) - min(n,max(l))
if (n < min(r)):
    s = 0
else:
    s += 1
print(s)
n, m = (int(x) for x in input().split())
l, r = (int(x) for x in input().split())
sss = set(range(l, r + 1))
for i in range(m-1):
  l, r = (int(x) for x in input().split())
  sss &= set(range(l, r))
print(len(sss))
n,m = map(int,input().split())
i_now = []
r_now = []

for k in range(m):
  i,r = map(int,input().split())
  
  i_now.append(i)
  r_now.append(r)

i_max = max(i_now)
r_min = min(r_now)

ans = r_min - i_max + 1
  
print(ans)
N,M=map(int, input().split())
a=[[int(j) for j in input().split()] for i in range(M)]

ans = a[0][1]-a[-1][0]+1

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

id_range = range(N + 1)

for m in range(M):
  L, R = map(int, input().split())
  if (L not in id_range) and (R not in id_range):
    id_range = range(0)
    break
  if L in id_range:
    id_range = range(L, id_range[len(id_range) - 1] + 1, 1)
  if R in id_range:
    id_range = range(id_range[0], R + 1, 1)
    
id_set = set(id_range)
print(len(set(range(1, N + 1, 1)) & id_set))
n,m = map(int,input().split())
mn = 1
mx = n

for i in range(m):
    l,r = map(int,input().split())
    if l > mn:
        mn = l
    if r < mx:
        mx = r
        
if mx > mn:
    print(0)
else:
    print(mn - mx + 1)
N, M = map(int, input().split())
L = list(list(map(int,input().split())) for i in range(M))
l = 0
r = N
for i in range(M):
    l = max(l,L[i][0])
    r = min(r,L[i][1])
print(r-l+1)
#前のやつだとTLEする
#こんなことしなくても最小と最大を更新していけばおｋ
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(r_min-l_max+1)
N, M = map(int, input().split(' '))

_min = 1
_max = N

for _ in range(M):
    l, r = map(int, input().split(' '))
    _min = max(_min, l)
    _max = min(_max, r)

print(_max - _min + 1)
def mi():
    return map(int, input().split())
'''
3 4
*G*S
G**S
*G*S
'''
n,m = mi()
l = -1
r = 1e10
for i in range(m):
    a,b=mi()
    l = max(l,a)
    r = min(r,b)
print(r-l+1)
n,k = 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]))

print(len(tmp))
n, m = map(int, input().split())
mini, maxi = 0, 10**5+1

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

print(maxi - mini + 1)
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(right-left+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())
s=0
for j in range(L[0],R[0]+1):
  if all(L[k]<=j<=R[k] for k in range(j,M)):
    s+=1
print(s)
import sys
N, M = map(int, sys.stdin.readline().split())
LR = sorted([list(map(int,sys.stdin.readline().split())) for i in range(M)])
if LR[0][1] - LR[M-1][0] >= 0:
    print(LR[0][1] - LR[M-1][0] + 1)
else:
    print(0)
n, m = map(int, input().split())
l_max = 1
r_min = 10 ** 5
for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l, l_max)
    r_min = min(r, r_min)

print(r_min - l_max + 1)
n,m = map(int, input().split())
mini = 0
maxi = 10**9
for i in range(m):
    l,r = map(int, input().split())
    if mini < l:
        mini = l
    if r < maxi:
        maxi = r
print(maxi-mini+1)
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n, m = map(int, input().strip().split())
  ls, rs = [], []
  for _ in range(m):
    l, r = map(int, input().strip().split())
    ls.append(l)
    rs.append(r)
  l = max(ls)
  r = min(rs)
  print(r - l + 1)

if __name__=='__main__':
  main()
n,m = map(int,input().split())
l_max=0
r_min=n 
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)
n,m = map(int, input().split())
minn, maxn = 1, n
for i in range(m):
	l, r = map(int,input().split())
	minn, maxn = max(minn, l), min(maxn, r)
print(maxn-minn+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
        
print(max_num- min_num + 1)
n, m = map(int, input().split())
left, right = 1, n
for _ in range(m):
  l, r = map(int, input().split())
  if l > left:
    left = l
  if r < right:
    right = r
  if l > r:
    print(0)
    quit()
print(right - left + 1)
L,M=map(int,input().split())
a=[0]*L
x=[]
y=[]
for i in range(M):
    b,c=map(int,input().split())
    x.append(b)
    y.append(c)
print(-max(x)+min(y)+1)
n,m = map(int,input().split())
l_max=0
r_min=10**5 
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)
n, m = map(int, input().split())
l, r = 0, n
for _ in range(m):
    li, ri = map(int, input().split())
    l = max(l, li)
    r = min(r, ri)
print(r - l + 1)
n,m=map(int,input().split())
LR=[list(map(int,input().split())) for _ in range(m)]
l,r=LR[0]
ans=r-l+1
for i in range(1,m):
  if l < LR[i][0]:
    l=LR[i][0]
  if r > LR[i][1]:
    r=LR[i][1]
  ans=r-l+1
print(ans)
N,T = map(int,input().split())
X = [list(map(int,input().split())) for n in range(T)]
M = N
m = 0

for a,b in X:
  M = min(M,b)
  m = max(m,a)

print(M-m+1)
N,M=map(int,input().split())
l=[list(map(int,input().split()))for i in range(M)]
min=0
max=1000000
for i in l:
    if i[0]>=min:
        min=i[0]
    if i[1]<=max:
        max=i[1]
print(max-min+1)
import numpy as np

N, M = [int(i) for i in input().split()]

LR = np.ones([M,2])

for i in range(M):
    LR[i][0],LR[i][1] = [int(i) for i in input().split()]
    
max_L = max(LR[:,0])
min_R = min(LR[:,1])
if max_L < min_R:
    ans = int(min_R - max_L)
else:
    ans =0
    
print(ans)
N,M=map(int,input().split())
LR=[]
for i in range(M):
  LR.append(list(map(int,input().split())))
l,r=LR[0][0],LR[0][1]
for i in range(M):
  if LR[i][0]<l and LR[i][1]<r:
    r=LR[i][1]
  elif l<LR[i][0] and LR[i][1]<r:
    l,r=LR[i][0],LR[i][1]
  elif l<LR[i][0] and r<LR[i][1]:
    l=LR[i][0]
print(r-l+1)
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(right-left+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(1+R-L)
n, m = map(int, input().split())
a = []
flag = True
for i in range(m):
    l, r = map(int, input().split())
    if i == 0: 
        l1 = l
        r1 = r
    else:
        if l1 < l:
            l1 = l
        elif r1 > r:
            r1 = r
if flag: print(r1-l1+1)
else: print(0)
N, M = map(int, input().split())

l = 0
r = N

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

else:
    print(ans)
from itertools import accumulate
N, M = map(int, input().split())

left = 1
right = M

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)
n,m = map(int, input().split())
fn = 1
ln = n
for i in range(m):
    l,r = map(int, input().split())
    fn = max(fn,l)
    ln = min(ln,r)    
print(ln-fn+1 if ln-fn>0 else 0)
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))
n, m = map(int, input().split())
l = 0
r = n
for i in range(m):
    lt, rt = map(int, input().split())
    l = max(l, lt)
    r=min(r,rt)
print(r-l+1)
n,m = map(int,input().split())
MIN = 0
MAX = 10**5
for i in range(m):
    l,r = map(int,input().split())
    MIN = MIN if MIN > l else l
    MAX = MAX if MAX < r else r
if MIN > n:
    print(0)
else:
    MAX = MAX if MAX <= n else n
    print(MAX-MIN+1)
N, M = map(int, input().split(' '))
l_val, r_val = map(int, input().split(' '))
for i in range(M-1):
    l, r = map(int, input().split(' '))
    l_val = max(l_val, l)
    r_val = min(r_val, r)

print(r_val - l_val + 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(r[0] - l[m - 1] + 1)
N, M=map(int, input().split())

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