peterverebics/CodeNet_Python_118 huggingface dataset:
import sys
n,m=(int(i) for i in input().split())
l = [None]*m
r = [None]*m

for i in range(m):
    l[i],r[i]=(int(j) for j in input().split())

rangel=l[0]
ranger=r[0]

for i in range(1,m):
    if rangel < l[i]:
        rangel = l[i]

    if ranger > r[i]:
        ranger = r[i]

    if rangel > ranger:
        ans =0
        print(ans)
        sys.exit()

ans = ranger - rangel +1
print(ans)
n,m=map(int,input().split())
l=1;ma=n
te=set(range(1,n+1))
for i in range(m):
    x,y=map(int,input().split())
    l=max(x,l)
    ma=min(ma,y)
print(abs(l-ma)+1 if l<=ma else 0)
N, M = map(int,input().split())


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


    

L = [x[0] for x in LR]
R = [x[1] for x in LR]


if  min(R) - max(L)>=0:
  print(min(R)-max(L)+1)
else:
  print(0)
n,m=[int(x) for x in input().split()]
L=0
R=1000000
for i in range(m):
  l,r=[int(x) for x in input().split()]
  L=max(L,l)
  R=min(R,r)
ans=R-L+1
if ans<=0:
  print(0)
else:
  print(ans)
import numpy as np
n,m=map(int,input().split())
X=1
Y=n
for i in range(m):
    L,R=map(int,input().split())
    X=max(X,L)
    Y=min(Y,R)
print(max(0,Y-X+1))
a = input().split()
N = int(a[1])
mini = []
maxi = []

for i in range(N):
  b = input().split()
  mini.append(b[0])
  maxi.append(b[1])
  
c = [int(s) for s in mini]
d = [int(s) for s in maxi]

c = sorted(c)
d = sorted(d, reverse=True)
e = min(d) - max(c)

if e >= 0:
  print(e + 1)
else:
  print(0)
# -*- coding: utf-8 -*-
"""
Created on Sat May 16 19:11:36 2020

@author: Kanaru Sato
"""

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

currentl,currentr = list(map(int,input().split()))
flag = 1

for i in range(m-1):
    l,r = list(map(int,input().split()))
    if currentl <= l and l <= currentr:
        currentl = l
    if currentl <= r and r <= currentr:
        currentr = r
    if currentr < l or r < currentl:
        flag = 0
        print(0)
        break

if flag == 1:
    print(currentr-currentl+1)
N, M = map(int, input().split())
left = 1
right = N

for i in range(M):
	l, r = map(int, input().split())
	if l > left:
		left = l
	if r < right:
		right = r
		
if left > right:
	print(0)
else:
		print(right - left + 1)
n,m = map(int,input().split())
li=[list(map(int,input().split())) for i in range(m)]
max_l,min_r=0,10**9

for i in li:
    max_l = max(max_l,i[0])
    min_r = min(min_r,i[1])
if min_r >= max_l:
    print(min_r-max_l+1)
else:
  print(0)
n, m = map(int, input().strip().split())

max_left = 0
min_right = n
for _ in range(m):
    l_tmp, r_tmp = map(int, input().strip().split())
    if max_left < l_tmp:
        max_left = l_tmp
    if r_tmp < min_right:
        min_right = r_tmp

if min_right < max_left:
    print(0)
else:
    print(min_right - max_left + 1)
n,m = map(int,input().split())
a = 1
b = n
for i in range(m):
    l,r = map(int,input().split())
    if ( a < l and b < l ) or ( a > r and b > r):
        a = 1
        b = 0
        break
    a = max(a,l)
    b = min(b,r)
print(b-a+1)
import sys

input = sys.stdin.readline


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

    L_max = max(L)
    R_min = min(R)
    if L_max > R_min:
        print(0)
    else:
        print(R_min - L_max + 1)


if __name__ == "__main__":
    main()
n,m=list(map(int,input().split()))
l_list=[]
r_list=[]
for i in range(m):
  l,r=list(map(int,input().split()))
  l_list.append(l)
  r_list.append(r)

ans_1=max(l_list)
ans_2=min(r_list)
print(max(ans_2-ans_1+1,0))
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 'None'

n,m = map(int,input().split())
l = 1
r = n
for i in range(m):
    a,b = map(int,input().split())
    if a > l:
        l = a
    if b < r:
        r = b
x = r-l+1
if x < 0:
    x = 0
print(x)
N,M = map(int,input().split())
L_max,R_min = 0,10000000
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(0)
else:
  print(R_min - L_max + 1)
from itertools import accumulate

n, m = [int(x) for x in input().split()]
a = [0 for i in range(n + 2)]
for i in range(m):
    l, r = [int(x) for x in input().split()]
    a[l] += 1
    a[r + 1] -= 1
a = list(accumulate(a))
print(a.count(m))
N,M=map(int,input().split())
gate=[list(map(int,input().split())) for _ in range(M)]

a=gate[0][0]
b=gate[0][1]
for i in range(1,M):
    a=max(a,gate[i][0])
    b=min(b,gate[i][1])

if b>=a:
    print(b-a+1)

else:
    print(0)
_, __, *d = map(int, open(0).read().split())
print(max(min(d[1::2]) - max(d[::2]) + 1, 0))
N,M=map(int,input().split())
l=list()
r=list()
for n 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())
lh=0
rh=10**9
for i in range(m):
  l,r=map(int,input().split())
  if lh<=l:
    lh=l
  if rh>=r:
    rh=r
if rh-lh<0:
  print(0)
else:
  print(rh-lh+1)
n, m = map(int, input().split())
max = float("INF")
min = 0
for i in range(m):
    l, r = map(int, input().split())
    if max > r:
        max = r
    if min < l:
        min = l
if max - min + 1 >= 0:
    print(max - min + 1)
else:
    print(0)
N, M = map(int, input().split())

LR = []
for _ in range(M):
  l, r = map(int, input().split())
  LR.append((l, r))

L_max = 1
R_min = N

for lr in LR:
  if (L_max < lr[0]):
    L_max = lr[0]

  if (R_min > lr[1]):
    R_min = lr[1]

result = R_min - L_max + 1
if(result <= 0):
  print(0)
else:
  print(result)
def myAnswer(N,M,L,R) -> int:
   total = 0
   MAXL=max(L)
   MINR =min(R)
   for i in range(1,N+1):
      if(MAXL<= i and MINR >= i):
         total += 1
   return total


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 =(int(a) for a in input().split())
num = []

for i in  range(M) :
    L,R =(int(a) for a in input().split())
    num.append((L , R))

a = 0
b = 10 ** 5
for i in range(M) :
    a = max(a , num[i][0])
    b = min(b , num[i][1])

x = b - a + 1
if x < 0 :
    print(0)
else : print(x)
n,m = map(int,input().split())
mi = 0
ma = 200000
for i in range(m):
    l,r = map(int,input().split())
    mi = max(mi,l)
    ma = min(ma,r)
if ma-mi>=0:
    print(ma-mi+1)
else:
    print(0)
n, m = map(int, input().split())

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

for i in range(n):
    gates[i + 1] += gates[i]

ans = 0
for i in range(n + 1):
    if gates[i] == m:
        ans += 1
print(ans)
import sys
input = lambda : sys.stdin.readline().rstrip()

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

left = -1
right = n + 1

for _ in range(m):
    l, r = map(int, input().split())
    if r < left or l > right:
        print(0)
        sys.exit()
    left = max(left, l)
    right = min(right, r)

print(right - left + 1)
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][1]<=r:
    r=LR[i][1]
  if l<=LR[i][0]:
    l=LR[i][0]
ans=r-l+1
if ans>0:
  print(ans)
else:
  print(0)
N,M = map(int,input().split())
L_list = []
R_list = []
count = 0
min_mn=float('inf')
max_mn=0
for i in range(M):
    L,R=map(int,input().split())
    min_mn = min(min_mn,R)
    max_mn = max(max_mn,L)

for i in range(1,N+1):
    if(i>=max_mn and i<=min_mn):
        count += 1
print(count)
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)
x=max(l_list)
y=min(r_list)
z=y-x
if z>=0:
    print(z+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 L<l:
        L=l
    if r<R:
        R=r

print(max(0,R-L+1))
import numpy as np

i = list(map(int, input().split()))
N = i[0]
M = i[1]

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

limit1 = max(l[:, 0])
limit2 = min(l[:, 1])

print(max(limit2 - limit1 + 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())

L_SORT = sorted(L)
R_SORT = sorted(R)

#print(L_SORT)
#print(R_SORT)

L_MAX = L_SORT[M-1]
R_MIN = R_SORT[0]

#print(L_MAX)
#print(R_MIN)


if (R_MIN - L_MAX) < 0:
  print(0)
else:
  print(R_MIN-L_MAX + 1)
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]

INF = float('inf')
ans_l, ans_r = -INF, INF
for l, r in lr:
    ans_l = max(ans_l, l)
    ans_r = min(ans_r, r)
print(max(0, ans_r-ans_l+1))
N, M = map(int, input().split())
l, r = 1, N
for i in range(M):
  L, R = map(int, input().split())
  l, r = max(l, L), min(r, R)
if r - l >= 0:
  print(r - l + 1)
else:
  print(0)
# C - Prison

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

ansl = 1
ansr = n

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

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

min, max = map(int, input().split())

for i in range(1, M):
    L, R = map(int, input().split())
    if L > max or R < min:
        print(0)
        exit()
    elif min < L <= max:
        min = L
    elif min <= R < max:
        max = R
print(max-min+1)
n,m=map(int,input().split())
L,R=0,10**18
for i in range(m):
  l,r=map(int,input().split())
  L=max(L,l)
  R=min(R,r)
if R<L:
  print(0)
else:
  print(R-L+1)
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)
ansl,ansr=max(l),min(r)
print(max(0,ansr-ansl+1))
n,m=map(int,input().split())
mn,mx=-1,10**7
for _ in range(m):
  l,r= map(int,input().split())
  mn=max(mn,l)
  mx=min(mx,r)
ans=max(mx-mn+1,0)
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)
n,m = map(int,input().split())
#v = list(map(int,input().split()))

#a = [0 for _ in range(n+1)]
ml = -1
mr = 10**6
for i in range(m):
  l,r = map(int,input().split())
  ml = max(ml,l)
  mr = min(mr,r)
if mr < ml :
  res = 0
else:
  res = mr-ml + 1

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

print(b-a+1 if a <= b else 0)
N,M = map(int,input().split())
L= 0
R = 10 ** 5
for i in range(M):
  Lm,Rm = map(int,input().split())
  L = max(L,Lm)
  R = min(R,Rm)
if R - L < 0:
  print(0)
else:
  print(R - L + 1)
import sys
input = sys.stdin.readline

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

n,m=MI()
ls=[]
rs=[]
for i in range(m):
    line=input().rstrip().split()
    ls.append(int(line[0]))
    rs.append(int(line[1]))
mx=0
mn=10**6
for l in ls:
    if l>mx:
        mx=l
#print(mx)
for r in rs:
    if r<mn:
        mn=r
#print(mn)
ans=max(mn-mx+1,0)
print(ans)
n, m = map(int, input().split())
left = -1
right = n+1

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 = list(map(int, input().split()))
low =[]
high=[]

for i in range(m):
  temp = list(map(int, input().split()))
  low.append(temp[0])
  high.append(temp[1])
  
maxlow= max(low)
minhigh=min(high) 

if minhigh-maxlow >= 0:
  print(minhigh-maxlow+1)
else:
  print(0)
N, M = map(int, input().split())
Lmax = 0
Rmin = 10**5 + 1
for n in range(M):
  L, R = map(int, input().split())
  if L > Lmax:
    Lmax = L
  if R < Rmin:
    Rmin = R

gate = Rmin - Lmax + 1
if gate < 0:
  print("0")
else:
  print(gate)
N,M = map(int,input().split())
L1 = 0
R1 = N

for m in range(M):
  L2,R2 = map(int,input().split())
  L1 = max(L1,L2)
  R1 = min(R1,R2)
  
print(max(0,R1-L1+1))
N, M = map(int, input().split())
LR = [tuple(map(int, input().split())) for _ in range(M)]

L = 1
R = 10**5

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

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

print(len(range(L, R+1)))
n,m=map(int,input().split())
ansl = 1
ansr = n
for i in range(m):
  l,r=map(int,input().split())
  ansl = max(ansl,l)
  ansr = min(ansr,r)
print(max(0,ansr-ansl+1))
N,M = map(int, input().split())

n = 0
m = 999999

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

print(m-n+1 if (m >= n) else 0)
n, m = map(int, input().split())
lmin = 0
lmax = 1e5+10
for _ in range(m):
  l, r = map(int, input().split())
  lmin = max(lmin, l)
  lmax = min(lmax, r)
print(max(0, lmax-lmin+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(max(0, min(r) - max(l) + 1))
n,m = map(int,input().split())
a = [0] *m
b = [0] *m
for i in range(m):
    a[i], b[i] = map(int, input().split())

ans = [a[0],b[0]]

for i in range(m-1):
    if ans[0] < a[i+1]:
        ans[0] = a[i+1]
    if ans[1] > b[i+1]:
        ans[1] = b[i+1]

if (ans[1]-ans[0] +1)<= 0:
    print(0)
    exit()

print(ans[1]-ans[0]+1)
n,m = map(int,input().split(' '))
i = 0
lmax = 0
rmin = n
while i < m:
    l,r = map(int,input().split(' '))
    if l > lmax:
        lmax = l
    if r < rmin:
        rmin = r
    i += 1
if lmax <= rmin:
    print(rmin-lmax+1)
else:
    print(0)
n, m = map(int, input().split())
cards = [0]*(n+1)
for _ in range(m):
    l, r = map(int, input().split())
    cards[l] += 1
    if r == n:
        continue
    cards[r+1] -= 1

for i in range(n):
    cards[i+1] += cards[i]
print(cards.count(m))
N, M = map(int, input().split())
L, R = 1, N
for _ in range(M):
    l, r = map(int, input().split())
    L = max(l, L)
    R = min(r, R)
if R<L:
    print(0)
else:
    print(R-L+1)
N, M, *I = map(int, open(0).read().split())
l = 0
r = N

for i in range(M):
    x, y = I[i*2], I[i*2+1]
    l = max(l, x)
    r = min(r, y)

print(max(r-l+1, 0))
# imos法
n, m = map(int, input().split())
l = []
r = []
imos = [0]*(n+1)
for i in range(m):
    L, R = map(int, input().split())
    l.append(L)
    r.append(R)

for i in range(m):
    imos[l[i]-1] += 1
    imos[r[i]] -= 1

cum_sum = [imos[0]]
for i in range(1, len(imos)):
    cum_sum.append(cum_sum[i - 1] + imos[i])

ans = 0
for count in cum_sum:
    if count == m: ans += 1
print(ans)
N, M = map(int, input().split())
maxL = 0
minR = float('inf')
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())
 
left, right = 1, N
 
for _ in range(M):
  L, R = map(int, input().split())
  
  left = max(L, left)
  right = min(R, right)

ans = right - left + 1 if left <= right else 0 
print(ans)
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(max(maxn-minn+1, 0))
n,m=[int(x) for x in input().rstrip().split()]

l=-float('inf')
r=float('inf')
for i in range(m):
  L,R=[int(x) for x in input().rstrip().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())

Lm = -1
Rm = float('inf')

for _ in range(M):
    L,R = map(int, input().split())
    Lm = max(L, Lm)
    Rm = min(R, Rm)

print(max(Rm - Lm + 1, 0))
n, m = map(int, input().split())
l_r = [ list(map(int, input().split())) for _ in range(m) ]

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)
if r_ans < l_ans:
    ans = 0
else:
    ans = r_ans-l_ans+1
print(ans)
N, M = map(int, input().split())
left = 0
right = 10 ** 5
for i in range(M):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)
print(right - left + 1 if right - left >= 0 else 0)
#!/usr/bin/env python3
# -*- 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)
max_l = 0
min_r = N
for i in range(M):
    if max_l < L[i]:
        max_l = L[i]
    if min_r > R[i]:
        min_r = R[i]
if max_l > min_r:
    print(0)
else:
    print(min_r - max_l + 1)
n, m = map(int, input().split())
ans = [i for i in range(1, n + 1)]

l_max = 0
r_min = n

for _ in range(m):
    x, y = map(int, input().split())
    l_max = max(l_max, x)
    r_min = min(r_min, y)

print(len([i for i in range(l_max, r_min + 1)]))
n,m = map(int,input().split())
l,r = map(int,input().split())

for i in range(m-1):
    l_new,r_new = map(int,input().split())
    if l_new > l:
        l = l_new
    if r_new < r:
        r = r_new

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

for i in range(1, M):
    if left < cards[i][0]:
        left = cards[i][0]
    if right > cards[i][1]:
        right = cards[i][1]

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

P = [0]*(N+1)

for i in range (0, M):
	A, B = map(int, input().split())
	P[A-1]+=1
	P[B]-=1
    
for i in range (1, N+1):
	P[i]+=P[i-1]
    
P = sorted(P, reverse = True)

count = 0

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

if __name__ == '__main__':
  main()
def main():
    n, m = map(int, input().split())

    card_list = [0] * n

    l_list = []
    r_list = []

    for _ in range(m):
        l, r = map(int, input().split())
        l_list.append(l)
        r_list.append(r)

    max_l = max(l_list)
    min_r = min(r_list)

    print(min_r-max_l+1 if min_r >= max_l else 0)

if __name__ == '__main__':
    main()
n,m=map(int,input().split())
card =[list(map(int,input().split())) for i in range(m)]
r = card[0][0]
R = card[0][1]
for i in range(m):
    if  r < card[i][0]:
        r = card[i][0]
for i in range(m):
    if card[i][1] < R:
        R = card[i][1]
ans = max(0,R - r + 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)
ans=(r+1-l)

if ans<=0:
    print(0)
else:
    print(ans)
import sys
input = sys.stdin.readline
f = lambda:map(int,input().split())

n, m = f()

lm = 0
rm = n

for i in range(m):
  l, r = f()
  lm = max(lm, l)
  rm = min(rm, r)

print(max(rm-lm+1, 0))
n,m=map(int,input().split())
##かーどについての初期配列
head =0
tail = n
for i in range(m):
    l,r = map(int,input().split())
    head = max(head,l)
    tail = min(r,tail)
    
if tail < head:
    print(0)
else:
    print(tail-head+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)
tmp = min(R)-max(L)+1

if tmp >= 1:
    print(tmp)
else:
    print(0)
n,m = map(int,input().split())
ansl = 0
ansr = n+1
for i in range(m):
    l,r = map(int,input().split())
    ansl = max(ansl,l)
    ansr = min(ansr,r)
print(0 if ansl > ansr else ansr-ansl+1)
n,m=map(int, input().split( ))
l=0
r = n+1
for _ 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())


l = 0
r = 10e5 + 5
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())

max_L = []
min_R = []

cnt = 0
for i in range(m):
    L,R = map(int,input().split())
    max_L.append(L)
    min_R.append(R)
if max(max_L) <= min(min_R):
    print(min(min_R)-max(max_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])

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

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

ans = 0
Lmax = max(L)
Rmin = min(R)

for a in range(n):
    if Lmax <= a+1 and a+1 <= Rmin:
        ans += 1

print(ans)
n,m=map(int,input().split())
l=[]
r=[]
for j in range(m):
    templ,tempr=map(int,input().split())
    l.append(templ)
    r.append(tempr)
ans=min(r)-max(l)+1
if ans<0:
    ans=0
print(ans)
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 if ans >= 0 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)

m = max(L)
M = min(R)
if m <= M: print(M-m+1)
else: print(0)
n,m = map(int, input().split())
left = set()
right = set()
for i in range(m):
  l,r = map(int, input().split())
  left.add(l)
  right.add(r)
mini = max(left)
maxi = min(right)
print(len(range(mini,maxi+1)))
#n=int(input())
#s,t=input().split()
n,m = map(int, input().split())
#l=list(map(int, input().split()))
l=[0]*100005
r=[1000005]*100005
z=1000010
for i in range(m):
   l[i],r[i]=map(int,input().split()) 
mi=max(l)
ma=min(r)



if mi>ma:
    print(0)
else:
    print(ma-mi+1)
N, M = map(int, input().split())
imos = [0]*(N+1)
for _ in range(M):
    # ゲートの情報が入ってくる
    l, r = map(lambda x: int(x)-1, input().split())
    imos[l] += 1
    imos[r+1] -= 1  # rまでは有効であり、r+1以降が無向になる仕様であるのでr+1をデクリメントする

cumulative = [imos[0]]
for value in imos[1:]:
    cumulative.append(cumulative[-1]+value)
ans = 0
for c in cumulative:
    if c == M:
        ans += 1
print(ans)
n, m = map(int, input().split())
l = 0
r = n
for i in range(m):
    lt, rt = map(int, input().split())
    if (lt > l):
        l = lt
    if (rt < r):
        r = rt
if (r <l):
    print(0)
else:
    print(r-l+1)
N, M = map(int, input().split())
L = sorted([list(map(int, input().split())) for _ in range(M)])

ml = 0
mm = 10**7
for v in L:
    ml = max(ml, v[0])
    mm = min(mm, v[1])

if (mm - ml + 1 <= 0):
    print(0)
    exit()
print(mm - ml + 1)
n, m = map(int, input().split())
lr = [tuple(map(int, input().split())) for _ in range(m)]
l = 1
r = n
for i, j in lr:
    l = max(i, l)
    r = min(j, r)
print(max(r-l+1, 0))
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 = [0] * (n + 1)
for i in range(m):
    l, r = map(int, input().split())
    s[l - 1] += 1
    s[r] -= 1

for i in range(n):
    s[i + 1] += s[i]
print(s.count(m))
n,m=map(int,input().split())

a=0
b=n
for i in range(m):
 l,r=map(int,input().split())
 if a<l:
  a=l
 if r<b:
  b=r
print(max(b-a+1,0))
n, m = map(int, input().split())
lmax = 0
rmin = n
for i 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())
input_lines = [[int(j) for j in input().split()]for i in range(m)]

start=1
end=n
for line in input_lines:
    if line[0]>start:
        start=line[0]
    if line[1]<end:
        end=line[1]
print(len([i for i in range(start,end+1)]))
N, M = map(int, input().split())
gate_num = [0 for i in range(N)]
for i in range(M):
    L, R = map(int, input().split())
    gate_num[L - 1] += 1
    if R < N:
        gate_num[R] -= 1
for i in range(1, N):
    gate_num[i] = gate_num[i] + gate_num[i - 1]
    
ans = 0
for i in range(N):
    if(gate_num[i] == M):
        ans += 1
print(ans)
n,m=list(map(int,input().split()))
l=0
r=10**5+1
for _ in range(m):
  ll,rr=list(map(int,input().split()))
  l=max(l,ll)
  r=min(r,rr)
print(r-l+1 if r-l+1>=0 else 0)