Problem:
 Score : 300 points    Problem Statement We have N ID cards, and there are M gates.  We can pass the i -th gate if we have one of the following ID cards: the L_i -th, (L_i+1) -th, ..., and R_i -th ID cards.  How many of the ID cards allow us to pass all the gates alone?      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq M \leq 10^5  1 \leq L_i \leq R_i \leq N         Input Input is given from Standard Input in the following format:  N  M  L_1  R_1  L_2  R_2  \vdots  L_M  R_M       Output Print the number of ID cards that allow us to pass all the gates alone.        Sample Input 1 4 2
1 3
2 4      Sample Output 1 2  Two ID cards allow us to pass all the gates alone, as follows:   The first ID card does not allow us to pass the second gate.  The second ID card allows us to pass all the gates.  The third ID card allows us to pass all the gates.  The fourth ID card does not allow us to pass the first gate.        Sample Input 2 10 3
3 6
5 7
6 9      Sample Output 2 1       Sample Input 3 100000 1
1 100000      Sample Output 3 100000

Solution (Python):
N,M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
L1 = []
R1 = []
for i in range(M):
    L1.append(LR[i][0])
    R1.append(LR[i][1])
if min(R1)>=max(L1):
    print(min(R1)-max(L1)+1)
else:
    print(0)

Solution (Python):
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,ans_max=L[0],R[0]

def compare(min_,max_,min_i,max_i):
  if max_<min_i:
    ret_min,ret_max = -1,-1
  elif min_>max_i:
    ret_min,ret_max = -1,-1
  elif (min_<=min_i) and (min_i<=max_) and (max_<=max_i):
    ret_min,ret_max = min_i,max_
  elif (min_>=min_i) and (max_<=max_i):
    ret_min,ret_max = min_,max_
  elif (min_>=min_i) and (min_<=max_i) and (max_>=max_i):
    ret_min,ret_max = min_,max_i
  elif (min_<=min_i) and (max_i<=max_):
    ret_min,ret_max = min_i,max_i
  else:
    pass

  return ret_min,ret_max

for i in range(1,M):
  ans_min,ans_max = compare(ans_min,ans_max,L[i],R[i])
  if ans_min==-1 and ans_max==-1:
    ans_min=N+1
    break

if ans_min<=N:
  if ans_max<=N:
    print(ans_max-ans_min+1)
  else:
    print(N-ans_min+1)
else:
  print(0)

Solution (Python):
N,M = map(int, input().split())
min_set_cnt,max_set_cnt = map(int,input().split(" "))

for m in range(M-1):
  _first, _last = map(int,input().split(" "))
  min_set_cnt = max(min_set_cnt,_first)
  max_set_cnt = min(max_set_cnt,_last)


ans = max(max_set_cnt - min_set_cnt + 1, 0)

print(ans)

Solution (Python):
def main():
    N, M = map(int, input().split())
    
    left = 0
    right = float('inf')
    
    for i in range(M):
        L, R = map(int, input().split())
        left = max(left, L)
        right = min(right, R)
    
    if left <= right:
        print(right - left + 1)
    else:
        print(0)

main()

Solution (Python):
N, M = map(int, input().split())
L = []
R = []
for i in range(M):
    L_temp, R_temp = map(int,input().split())
    L.append(L_temp)
    R.append(R_temp)

print(max(min(R)+1-max(L),0))

Solution (Python):
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 = list(range(max(L),min(R)+1))
print(len(ans))

Solution (Python):
n, m = map(int, input().split())
lr = []
for i in range(m):
    lr.append(list(map(int, input().split())))

def ans():
    ans = lr[0]
    for i in range(1, m):
        if ans[0] > lr[i][-1] or ans[-1] < lr[i][0]:
            print(0)
            return
        ans = [max(ans[0], lr[i][0]), min(ans[-1], lr[i][-1])]

    print(ans[-1]- ans[0] + 1)
    return

ans()

Solution (Python):
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 = list(range(max(L),min(R)+1))
print(len(ans))

Solution (Python):
N, M = list(map(int, input().split()))
LR = [list(map(int,input().split())) for i in range(M)]
max = 1
min = 100000
for i in range(M):
    if max < LR[i][0]:
        max = LR[i][0]
    if min > LR[i][1]:
        min = LR[i][1]
if max <= min:
    print(min - max + 1)
else:
    print(0)

Solution (Python):
import sys

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

overlap_l = 1
overlap_r = n
for i in range(m):
    l, r = list(map(int,input().split()))
    if overlap_r < l or r < overlap_l:
        print(0)
        sys.exit()
    else:
        overlap_l = max(l, overlap_l)
        overlap_r = min(r, overlap_r)

print(overlap_r - overlap_l + 1)

Solution (Python):
n, m = map(int, input().split())
gl=[]
for i in range(m):
	gl.append(list(map(int, input().split())))

gl.sort()
s = 1
e = n
for g in gl:
	if s < g[0]:
		s = g[0]
	if e > g[1]:
		e = g[1]
	

print(max(e - s + 1, 0))

Solution (Python):
N,M = map(int,input().split())

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

L0,R0 = 0,10**6

for i in IDS:
    L0 = max(L0,i[0])
    R0 = min(R0,i[1])

print(max(0,R0-L0+1))

Solution (Python):
N, M = map(int, input().split())
low = 1
high = 1e+5
for i in range(M):
    L, R = map(int, input().split())
    if low < L:
        low = L 
    if R < high:
        high = R
if low > high:
    print(0)
else:
    print(int(high-low+1))

Solution (Python):
N, M = map(int, input().split())
keys = []
for i in range(M):
  keys.append(list(map(int, input().split())))
  
passkeys = [1, N]
for i in range(M):
  passkeys[0] = max(passkeys[0], keys[i][0])
  passkeys[1] = min(passkeys[1], keys[i][1])
if passkeys[1] >= passkeys[0]:
  print(passkeys[1] - passkeys[0] + 1)
if passkeys[1] < passkeys[0]:
  print(0)

Solution (Python):
n,m=map(int,input().split())
lmax=0
rmin=float('inf')
for i in range(m):
  l,r=map(int,input().split())
  lmax=max(l,lmax)
  rmin=min(r,rmin)
if lmax>rmin:
  x=0
else:
  x=rmin-lmax+1
print(x)

Solution (Python):
import sys
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())
LL = L[0]
RR = R[0]
for i in range(1,M):
    if L[i] > RR or LL > R[i]:
        print(0)
        sys.exit()
    else:
        LL = max(LL, L[i])
        RR = min(RR, R[i])
print(RR - LL + 1)

Solution (Python):
import sys
N,M = map(int,input().split())
L = []
R = []
for i in range(M):
    a =list(map(int,input().split()))
    L.append(a[0])
    R.append(a[1])
LL = L[0]
RR = R[0]
for i in range(1,M):
    if L[i] > RR or LL > R[i]:
        print(0)
        sys.exit()
    else:
        LL = max(LL, L[i])
        RR = min(RR, R[i])
print(RR - LL + 1)

Solution (Python):
import sys
N,M = map(int,input().split())
L = []
R = []
for i in range(M):
    a =list(map(int,input().split()))
    L.append(a[0])
    R.append(a[1])
LL = L[0]
RR = R[0]
for i in range(1,M):
    if L[i]>RR or LL>R[i]:
        print(0)
        sys.exit()
    elif LL>=L[i] and RR>=R[i]:
        RR=R[i]
    elif LL>=L[i] and RR<R[i]:
        continue
    elif L[i]>=LL and R[i]>=RR:
        LL = L[i]
    elif L[i]>=LL and R[i]<RR:
        continue
print(RR-LL+1)

Solution (Python):
import heapq
n, m = map(int, input().split())
lr = []
heap1 = []
heap2 = []
for i in range(m):
    lr.append(list(map(int, input().split())))
for j in range(m):
    heapq.heappush(heap1, -lr[j][0])
    heapq.heappush(heap2, lr[j][1])
d = heapq.heappop(heap2) + heapq.heappop(heap1) +1 
if d < 0:
    print(0)
else:
    print(d)

Solution (Python):

import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    print(max(0, min(R)-max(L)+1))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

Solution (Python):
n,m=map(int,input().split())
l_list,r_list=[],[]
for _ in range(m):
  l,r=map(int,input().split())
  l_list.append(l)
  r_list.append(r)
ans=max(0,min(r_list)-max(l_list)+1)
print(ans)

Solution (Python):
def intersect(a, b, c, d):
    if b < c or d < a:
        return -2, -1
    else:
        li = sorted([a, b, c, d])
        return li[1], li[2]
        
N, M = map(int, input().split())
LR = []
for _ in range(M):
    LR.append(tuple(map(int, input().split())))
    
a, b = LR[0]
for lr in LR:
    a, b = intersect(a, b, *lr)
    
ans = b-a+1
print(ans) if a > 0 else print('0')

Solution (Python):
n,m=map(int,input().split())
l,r=1,n
for i in range(m):
    x,y=map(int,input().split())
    if l<x:
        l=x
    if y<r:
        r=y
    if l>r:
        print(0)
        break
else:
    print(r-l+1)

Solution (Python):
n, m = map(int, input().split())
ls, rs = [], []
for _ in range(m):
    l, r = map(int, input().split())
    ls.append(l)
    rs.append(r)
l_max = max(ls)
r_min = min(rs)
print(max(r_min - l_max + 1, 0))

Solution (Python):
N, M = map(int, input().split())

gate = [list(map(int, input().split())) for _ in range(M)]
pL = max(gate[0][0], 0)
pR = min(gate[0][1], N)
lst = range(pL, pR+1)

for i in range(1, M):
    L = max(gate[i][0], 0)
    R = min(gate[i][1], N)
    lst = range(max(L, pL), min(R, pR)+1)
    pL = max(L, pL)
    pR = min(R, pR)

print(len(list(lst)))

Solution (Python):
n, m = map(int, input().split())

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

max_l = max(L)
min_r = min(R)

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

Solution (Python):
n,m = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(m)]

l_max,r_min = 0,10**5
for l,r in src:
    l_max = max(l_max,l)
    
for l,r in src:    
    r_min = min(r_min,r)
else:
    if r_min-l_max < 0:
        print(0)
    else:
        print(r_min-l_max+1)

Solution (Python):
n,m = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(m)]

l_max,r_min = 0,10**5
for l,r in src:
    l_max = max(l_max,l)
    r_min = min(r_min,r)
else:
    if r_min-l_max < 0:
        print(0)
    else:
        print(r_min-l_max+1)

Solution (Python):
n,m=map(int,input().split())

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

print(max(0,(min(r)-max(l)+1)))

Solution (Python):
n,m=map(int,input().split())
D=[list(map(int,input().split())) for i in range(m)]
maxI=n
minI=1
for i in range(m):
  maxI=min(maxI,D[i][1])
  minI=max(minI,D[i][0])
print(max(maxI-minI+1,0))

Solution (Python):
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)


L.sort()
R.sort()

print(max(R[0]-L[-1]+1, 0))

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

min_i = 0
max_i = 100000

for m in range(M):
    min_i = max(min_i, LR[m][0])
    max_i = min(max_i, LR[m][1])

print(max_i - min_i + 1 if (max_i >= min_i) else 0)

Solution (Python):
N, M = map(int, input().split())
min_value = 0
max_value = N  
for i in range(M):
    l, r = list(map(int, input().split()))
    if min_value < l:
        min_value = l
    if max_value > r:
        max_value = r
print(max(0, max_value-min_value+1))

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

Solution (Python):
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(0, r-l+1))

Solution (Python):
n,m = map(int,input().split())
l1 = []
l2 =[]
for i in range(m):
    l,r = list(map(int,input().split()))
    l1.append(l)
    l2.append(r)
ans = min(l2) - max(l1) + 1
if ans >0:
    print(ans)
else:
    print(0)

Solution (Python):
N, M = map(int, input().split())
L=[]
R=[]
for i in range(M):
    l, r = list(map(int, input().split()))  
    L.append(l)
    R.append(r)
 
ans = max(0, min(R) - max(L) + 1)

print(ans)

Solution (Python):
import numpy as np

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

L=1
R=N

for i in range(M):
  temp=list(map(int, input().split()))
  
  if L<temp[0]:
    L=temp[0]
    
  if R>temp[1]:
    R=temp[1]
    
temp=int(R-L+1)
if temp>0:
  print(temp)
else:
  print(0)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m = LI()
L,R = 1,n
for _ in range(m):
    l,r = LI()
    if L < l:
        L = l
    if r < R:
        R = r
if R - L < 0:
    print(0)
else:
    print(R-L+1)

Solution (Python):
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)

if left > right:
    print(0)
else:
    print(right - left + 1)

Solution (Python):
n, m = map(int, input().split())
x, y = 1, n
for _ in range(m):
  a, b = map(int, input().split())
  x = max(a, x)
  y = min(b, y)
print(max(0, y-x+1))

Solution (Python):
n,m = map(int, input().split())
ls = []
rs = []
for i in range(m):
  l,r = map(int, input().split())
  ls.append(l)
  rs.append(r)
ans = min(rs)-max(ls)+1
print(max(ans,0))

Solution (Python):
n, m = map(int, input().split())
l_r = list(list(int(i) for i in input().split()) for i in range(m))

x = []
y = []

for i in range(m):
    x.append(l_r[i][0])
    y.append(l_r[i][1])

x1 = max(x)
y1 = min(y)

if x1 <= y1:
    print(y1 - x1 + 1)
else:
    print('0')

Solution (Python):
n, m = map(int, input().split())
lrs = [list(map(int, input().split()))
       for _ in range(m)]

ls = []
rs = []
for l, r in lrs:
    ls.append(l)
    rs.append(r)
l = max(ls)
r = min(rs)
if l <= r:
    print(r - l + 1)
else:
    print(0)

Solution (Python):
n,m = map(int, input().split())
l_lst, r_lst = [], []
for i in range(m):
  l,r = map(int, input().split())
  l_lst.append(l)
  r_lst.append(r)
res = min(r_lst)-max(l_lst)+1
if res > 0:
  print(res)
else:
  print(0)

Solution (Python):
import numpy as np
N, M = map(int, input().split())
LR = np.array([list(map(int, input().split())) for _ in range(M)])
lmax, rmin = LR.max(axis=0)[0], LR.min(axis=0)[1]
print(max(0, rmin-lmax+1))

Solution (Python):
n,m = map(int,input().split())
ans = []
for _ in range(m):
    l,r = map(int,input().split())
    if ans:
        ans = [max(ans[0],l),min(ans[1],r)]
    else:
        ans = [l,r]
print(ans[1]-ans[0]+1 if ans[1]-ans[0]+1>0 else 0)

Solution (Python):
import math
import copy
import fractions
import numpy as np



def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList



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

data = getSomeInputListInt(m)

lower = []
upper = []

for i in range(m):
    lower.append(data[i][0])
    upper.append(data[i][1])

lowerNum = max(lower)
upperNum = min(upper)

ans = max(0, upperNum - lowerNum + 1)


print(ans)

Solution (Python):
n,m=map(int,input().split())

lis=[1,n]
for i in range(m):
    l,r=map(int,input().split())
    if l>lis[0]:
        lis[0]=l
    if r<lis[1]:
        lis[1]=r

if lis[1]-lis[0]<0:
    print(0)
else:
    print(lis[1]-lis[0]+1)

Solution (Python):
n1, n2 = [int(i) for i in input().split()]

n3 = []
n4 = []

t1 = 0
t2 = 0
for i1 in range(n2):
    t1, t2 = ([int(i) for i in input().split()])
    n3.append(t1)
    n4.append(t2)

m1 = max(n3)
m2 = min(n4)

r1 = m2 - m1 + 1

if r1 < 0:
    r1 = 0

print(r1)

Solution (Python):
N, M = map(int, input().split())
L, R = [0] * M, [0] * M
Lmax, Rmin = 0, 1e10
for i in range(M):
    L, R = map(int, input().split())
    if L > Lmax:
        Lmax = L
    if R < Rmin:
        Rmin = R

print(max(Rmin - Lmax + 1, 0))

Solution (Python):
n, m = map(int, input().split())
min_num = 0
max_num = 10**5
           
for i in range(m):
  min_temp, max_temp = map(int, input().split())
  if min_temp > min_num:
    min_num = min_temp
  if max_temp < max_num:
    max_num = max_temp
print(max(max_num - min_num + 1, 0))

Solution (Python):
n = list(map(int,input().split()))
l = [list(map(int,input().split())) for i in range(n[1])]
minp = l[0][0]
maxp = l[0][1]
for t in range(n[1]):
    minp = max(minp,l[t][0])
    maxp = min(maxp,l[t][1])
print(max(0,maxp-minp+1))

Solution (Python):
import numpy as np
N, M = [int(x) for x in input().split()]
ok = np.zeros(N+2, np.int64)

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

cum = np.cumsum(ok)
print(np.sum(cum==M))

Solution (Python):
n, m = map(int, input().split())
l_list = []
r_list = []
cnt = 0
for _ in range(m):
    l, r = map(int, input().split())
    l_list.append(l)
    r_list.append(r)

print(min(r_list) - max(l_list) + 1) if min(r_list) >= max(l_list) else print(0)

Solution (Python):
n,m = map(int,input().split())

s,e = map(int,input().split())
for i in range(m-1):
  l,r = map(int,input().split())
  s = max(s,l)
  e = min(e,r)

print(len(range(s,e+1)))

Solution (Python):
n, m = map(int, input().split())
maxi = 10**5
mini = 0
for _ in range(m):
    s,t = map(int,input().split())
    maxi = min(maxi,t)
    mini = max(mini,s)
print(maxi - mini +1) if maxi - mini +1 > 0 else print(0)

Solution (Python):
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)
print(b-a+1 if b-a+1 > 0 else 0)

Solution (Python):
N,M = map(int, input().split())
mNum=0
MNum=10**10
for i in range(M):
    tm,tM = map(int, input().split())
    if(mNum<tm):
        mNum=tm
    if(MNum>tM):
        MNum=tM
print(max(MNum-mNum+1,0))

Solution (Python):
N, M = list(map(int, input().split()))
mn = 0
mx = N
f = False
for i in range(M):
    tmn, tmx = list(map(int, input().split()))
    if mn < tmn:
        mn = tmn
    if tmx < mx:
        mx = tmx
    if mx < mn:
        f = True
        break
if f:
    print(0)
else:
    print(mx - mn + 1)

Solution (Python):
import sys
input = sys.stdin.readline

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)

ans = max(R-L+1, 0)
print(ans)

Solution (Python):
n, m = map(int, input().split())
S = [None]*m
L = 0
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:
    ans = 0
print(ans)

Solution (Python):
import numpy as np
input1 = input()

door_num = int(input1.split()[1])
lefts = []
rights = []
for i in range(door_num):
    a = input()
    new_inputs = [int(i) for i in a.split()]
    lefts.append(new_inputs[0])
    rights.append(new_inputs[1])
    
    
    
    
min_right = np.min(rights)
max_left = np.max(lefts)

if min_right >= max_left:
    print(min_right - max_left + 1)
else:
    print(0)

Solution (Python):
import sys
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 = max(l)
r_min = min(r)

if l_max > r_min:
    print(0)
else:
    if n < l_max:
        print(0)
    elif n < r_min:
        print(r_min - n + 1)
    else:
        print(r_min - l_max + 1)

Solution (Python):
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())
LL = max(L)
RR = min(R)
ans = 0
for i in range(N):
    if LL <= i + 1 <= RR:
        ans += 1
    else:
        pass
print(ans)

Solution (Python):
n, m = map(int, input().split())
l_max = 1
r_min = 10 ** 5

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

if r_min < l_max:
    ans = 0
else:
    ans = r_min - l_max + 1

print(ans)

Solution (Python):
n, m = map(int, input().split())

l_tmp = 0
r_tmp = n+1
for i in range(m):
  l, r = map(int, input().split())
  l_tmp = l if l > l_tmp else l_tmp
  r_tmp = r if r < r_tmp else r_tmp

if r_tmp-l_tmp >= 0:
  print(r_tmp-l_tmp+1)
else:
  print(0)

Solution (Python):
N, M = list(map(int, input().split()))
lr = []
for i in range(M):
    l, r = list(map(int, input().split()))
    lr.append([l, r])

mi = -1
ma = 10**5 + 1
for i in range(M):
    l = lr[i][0]
    r = lr[i][1]
    if mi <= l:
        mi = l
    if r <= ma:
        ma = r

ans = ma - mi + 1
print(ans)if ans >= 0 else print(0)

Solution (Python):
n,m = map(int,input().split())
card = [ 0 for i in range(n+5) ]
for i in range(m):
    l , r = map(int,input().split())
    card[l] += 1
    card[r+1] -= 1

for i in range(1,n+1):
    card[i] += card[i-1]
ans = 0
for i in card:
    if i == m:
        ans += 1
print(ans)

Solution (Python):
n,m=map(int,input().split())
s=0
t=10**5+1
for i in range(m):
	l,r=map(int,input().split())
	s=max(s,l)
	t=min(t,r)
print(max(t-s+1,0))

Solution (Python):
import sys
input = sys.stdin.readline

def main(): 
    N,M = map(int,input().split())
    LR=[list(map(int,input().split())) for _ in range(M)]
    max = 0
    min = N
    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(0)
    else:
        print(min-max+1)
if __name__ == "__main__":
    main()

Solution (Python):
N,M = map(int, input().split())
S = [list(map(int, input().split())) for i in range(M)]
 
Mini = 0
Max = N
 
for i in range(M):
    if Mini <= S[i][0]:
        Mini = S[i][0]
    if Max >= S[i][1]:
        Max = S[i][1]
print(max(Max-Mini+1,0))

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

Solution (Python):
n, m = [int(x) for x in input().split(' ')]
lr = [(int(l), int(r)) for l, r in [input().split(' ') for _ in range(m)]]

l = max([x[0] for x in lr])
r = min([x[1] for x in lr])

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

Solution (Python):
def prison(n,m,security):
    l = 0
    r = 1
    through = [False]*(n+1)
    gate = [1,n]
    for i in range(m):
        if security[i][r] < gate[r]:
            gate[r] = security[i][r]

        if  gate[l] < security[i][l]:
            gate[l] = security[i][l]


    for i in range(1,n+1):
        if gate[l] <= i and i <= gate[r]:
            through[i] = True
        else:
            through[i] = False




    sucess = filter(lambda x:x == True, through)
    return len(sucess)

if __name__ == '__main__':
    n,m = map(int,raw_input().split())

    security = [list(map(int,raw_input().split())) for _ in range(m)]

    print prison(n,m,security)

Solution (Python):
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))

Solution (Python):
n,m=map(int,input().split())
L=0
R=100001
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))

Solution (Python):
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)

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

    left, right = LR[0]
    for i in range(1, M):
        l, r = LR[i]
        if left > r or right < l:
            return 0
        elif left <= r:
            left = max(left, l)
            right = min(right, r)
    return right - left + 1

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

Solution (Python):
n,m = map(int,input().split())
a_min = 1
a_max = n
for _ in range(m):
  l,r = map(int,input().split())
  a_min = max(a_min,l)
  a_max = min(a_max,r)
if a_min>a_max:
  print(0)
else:
  print(a_max-a_min+1)

Solution (Python):
n,m=map(int,input().split())
ll=[]
rl=[]
for i in range(m):
    l,r=map(int, input().split())
    ll.append(l)
    rl.append(r)


max_l=max(ll)
min_r=min(rl)
ans=min_r-max_l+1
if ans<=0:
    print(0)
else:
    print(ans)

Solution (Python):
N,M = map(int,input().split())
L_list = []
R_list = []

for i in range(M):
  L,R = map(int,input().split())
  L_list.append(L)
  R_list.append(R)
  
if min(R_list) >= max(L_list):
  print(min(R_list)  - max(L_list)+1)
else:
  print(0)

Solution (Python):
N,M = map(int,input().split())
maxn,minn = map(int,input().split())
for i in range(M-1):
    L,R = map(int,input().split())
    if L > maxn:
        maxn = L
    if R < minn:
        minn = R
if minn < maxn:
    ans = 0
else:
    ans = minn - maxn + 1
print(ans)

Solution (Python):
n, m = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(m)]

start = 1
end = n
for pair in lst:
    start = max(start, pair[0])
    end = min(end, pair[1])
print(end - start + 1) if end - start + 1 >= 0 else print(0)

Solution (Python):
import sys
input=sys.stdin.readline

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

Solution (Python):
import sys
input=sys.stdin.readline

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

l,r = 1,N
for i in range(M):
    l1,r1 = map(int, input().split())
    if l1>l:
        l = l1
    if r1<r:
        r = r1
if r-l<0:
    print(0)
else:
    print(r-l+1)

Solution (Python):
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)
L.sort(reverse=True)
R.sort()
ans =0
for i in range(n+1):
    if i >=L[0] and i <=R[0]:
        ans +=1
print(ans)

Solution (Python):
n,m = map(int,input().split())
l = [0]*m
r = [0]*m
for i in range(m):
  l[i], r[i] = map(int,input().split())
print(max(0,min(r)-max(l)+1))

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

Solution (Python):
n,m=[int(i) for i in input().split()]
lista=[]
listb=[]

for i in range(m):
  a,b = [int(i) for i in input().split()]
  lista.append(a)
  listb.append(b)
  
maxa = max(lista)
minb = min(listb)

if minb<maxa:
  print(0)
else:
  print(minb-maxa+1)

Solution (Python):


def main():
    N, M = map(int, input().split())
    INF = float('inf')

    LR = [-INF, INF]
    for _ in range(M):
        L, R = map(int, input().split())
        if LR[0] < L:
            LR[0] = L
        if LR[1] > R:
            LR[1] = R
    if LR[1] < LR[0]:
        return 0
    else:
        return int(LR[1] - LR[0] + 1)

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

Solution (Python):
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 = 1
e = N
for i in range(M):
	if s < L[i]:
		s = L[i]
	if e > R[i]:
		e = R[i]
if s > e:
	print(0)
else:
	print(e-s+1)

Solution (Python):
n,m=map(int,input().split())
c=[0]*(n+1)
for i in range(m):
    l,r=map(int,input().split())
    c[l-1]+=1
    c[r]-=1
for i in range(n):
    c[i+1]+=c[i]
ans=sum([1 for i in c if i==m])
print(ans)

Solution (Python):
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)

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

Solution (Python):
n,m = map(int,input().split())
l_li = []
r_li = []
for i in range(m):
  li = list(map(int,input().split()))
  l_li.append(li[0])
  r_li.append(li[1])
l_m = max(l_li)
r_m = min(r_li)
if r_m - l_m + 1> 0:
  print( r_m - l_m + 1)
else:
  print("0")

Solution (Python):
N, M = map(int, input().split())
start = []
end = []
for i in range(M):
    L, R = map(int, input().split())
    start.append(L)
    end.append(R)
start_max = max(start)
end_min = min(end)
print(max(0, end_min - start_max + 1))

Solution (Python):
N,M = list(map(int,input().split()))
list = []
for i in range(M):
    a,b=input().split()
    list.append((int(a),int(b)))
 
max = 0
min = 10000000000
count = 0
 
for i in range(M):
    if max < list[i][0] :
        count = 0
        max = list[i][0]
    elif max == list[i][0] :
        count += 1
 
for i in range(M):
    if min > list[i][1] :
        min = list[i][1]
   
 
if min >= max:
    print(min - max + 1)
else:
    print(0)

Solution (Python):
n, m = map(int, input().split())

floor = 0
ceiling = 999999

for i in range(m):
  a, b = map(int, input().split())
  if a > floor:
    floor = a
  if b < ceiling:
    ceiling = b

print(max(0, ceiling - floor + 1))

Solution (Python):
n,m=[int(i) for i in input().split()]
datalist=[[int(j) for j in input().split()] for i in range(m)]




head=1
tail=n


for data in datalist:
    
    if data[0]>head:
        head=data[0]
    if data[1]<tail:
        tail=data[1]
        
ans=tail-head+1
if ans<0:
    print(0)
else:
    print(ans)

Solution (Python):
c,g=map(int,input().split())

st=[]
en=[]

for i in range(g):
	s,e=map(int,input().split())
	st.append(s) 
	en.append(e)

res=min(en)-max(st)+1
print(max(res,0))

Solution (Python):

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

Ld = []
Rd = []

for i in range(M):
	Ld.append(LR[i][0])
	Rd.append(LR[i][1])

if max(Ld) <= min(Rd):
	print(min(Rd) - max(Ld) + 1)
else:
	print(0)

Solution (Python):
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())
    

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

Solution (Python):
n, m = map(int, input().split())

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

Solution (Python):
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)
maxl = max(L)
minr = min(R)
ans = minr-maxl + 1
if ans < 0:
    ans = 0
print(ans)

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

print(max(ans,0))

Solution (Python):
n,m=map(int, input().split())
l,r=[],[]
for _ in range(m):
    l0,r0=map(int, input().split())
    l.append(l0)
    r.append(r0)
keyl=max(l)
keyr=min(r)
if keyr-keyl>=0:
    print(keyr-keyl+1)
else: print(0)

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

Solution (Python):
n,m=map(int,input().split())
s=[0]*n
max_=1
min_=n
for _ in range(m):
    b,c=map(int,input().split())
    if b>max_:
        max_=b
    if c<min_:
        min_=c
if min_<max_:
  print('0')
else:
  print(min_-max_+1)