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):
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4 2
1 3
2 4"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """10 3
3 6
5 7
6 9"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """100000 1
1 100000"""
        output = """100000"""
        self.assertIO(input, output)
def resolve():
    (N,M)=(int(i) for i in input().split())
    ll=[]
    rr=[]
    for i in range(M):
        (l,r)=(int(i) for i in input().split())
        ll.append(l)
        rr.append(r)
    lm=max(ll)
    rm=min(rr)
    if lm>rm:
        print(0)
        return
    if N>=rm:
        print(1+rm-lm)
    elif N>=lm:
        print(1+N-lm)
    else:
        print(0)
if __name__ == "__main__":
    resolve()

Solution (Python):
N,M = map(int,input().split())
L_max = 0
R_min = N
for i in range(0,M):
  L_tmp, R_tmp = map(int,input().split())
  if L_max < L_tmp:
    L_max = L_tmp
  if R_min > R_tmp:
    R_min = R_tmp
  
print(R_min - L_max + 1) if R_min >= L_max else print(0)

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

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

Solution (Python):
LL=[]
RR=[]
N,M=map(int,input().split())
for i in range(M):
    L,R=map(int,input().split())
    LL.append(L)
    RR.append(R)
    
L1=max(LL)
R1=min(RR)
if L1>R1:
    print(0)
else:
    print(R1-L1+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())

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

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

prev_l, prev_r = 1, 100000
for _ in range(m):
    l, r = map(int, input().split()) 
    prev_l, prev_r = max(l, prev_l), min(r, prev_r)

if prev_r < prev_l:
    print(0)
else:
    print(prev_r - prev_l + 1)

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

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


def main():
    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)
    
    print(max(R-L+1, 0))


if __name__ == '__main__':
    main()

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)
Lm=max(L)
Rm=min(R)
print(Rm-Lm+1 if Rm-Lm>=0 else 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 = 0
A = [0] * N
L_max = max(L)
R_min = min(R)
if R_min >= L_max:
    ans = R_min - L_max + 1
else:
    ans = 0
print(ans)

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

Solution (Python):

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

cs = [0] * N
for _ in range(M):
    L, R = map(int, input().split())
    cs[L - 1] += 1
    if R != N:
        cs[R] -= 1

for i in range(1, N):
    cs[i] += cs[i - 1]

print(cs.count(M))

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

cs = [0] * N
for _ in range(M):
  L, R = map(int, input().split())
  cs[L - 1] += 1
  if R != N:
    cs[R] -= 1

for i in range(1, N):
  cs[i] += cs[i - 1]

print(sum(1 for i in range(N) if cs[i] == M))

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

L_largest = 0
R_min = N
for m in range(M):
    L, R = map(int, input().split())
    if L >= L_largest:
        L_largest = L
    if R <= R_min:
        R_min = R

print(max(0, R_min - L_largest + 1))

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    ls = [0] * (n+2)
    for i in range(m):
        l, r = list(map(int, input().rstrip('\n').split()))
        ls[l] += 1
        ls[r+1] -= 1
    t = 0
    cnt = 0
    for i in range(len(ls)):
        t += ls[i]
        if t == m:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

Solution (Python):
N,M,*D = map(int, open(0).read().split())
print(max(min(D[1::2])-max(D[::2])+1, 0))

Solution (Python):
import sys

N,M = map(int, input().split())
D = sys.stdin
l = 0
r = N
for d in D:
    L,R = map(int, d.split())
    if L > l:
        l = L
    if R < r:
        r = R
print(max(r-l+1,0))

Solution (Python):
import sys
N,M = map(int, input().split())
D = sys.stdin
L = []
R = []
for d in D:
    l,r = map(int, d.split())
    L.append(l)
    R.append(r)
print(max(min(R)-max(L)+1,0))

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)
print(max(min(R)-max(L)+1,0))

Solution (Python):
N,M = map(int, input().split())
l = 0
r = N
D = [map(int, input().split()) for _ in range(M)]
for d in D:
    L,R = d
    if L > l:
        l = L
    if R < r:
        r = R
print(max(r-l+1,0))

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

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)

left = 0
right = N
for l, r in zip(L, R):
    left = max(left, l)
    right = min(right, r)
    
print(max(0, right - left + 1))

Solution (Python):
N,M=map(int,input().split())
G=[[] for _ in range(M)]
for i in range (M):
  L,R=map(int,input().split())
  G[i].append([L,R])
small=[]
for i in range(M):
  small.append(G[i][0][0])
s=max(small)
large=[]
for i in range(M):
  large.append(G[i][0][1])
l=min(large)
if s<=l:
  ans=l-s+1
else:
    ans=0
print(ans)

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

def main():
    N, M = [int(x) for x in input().split()]

    A = []
    for i in range(M):
        A.append([int(x) for x in input().split()])

    answer = A[0]
    for i in range(1, M):
        if max(answer) < min(A[i]) or max(A[i]) < min(answer):
            print(0)
            exit()

        total = []
        total.extend(answer)
        total.extend(A[i])
        s_total = sorted(total)
        answer = s_total[1:3]

    print(answer[1] - answer[0] + 1)




if __name__ == '__main__':
    main()

Solution (Python):

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

ll = 1
rr = 100000

for i in range(M):
    l_next, r_next = map(int, input().split())
    if l_next >= ll:
        ll = l_next
    if r_next <= rr:
        rr = r_next

print(max(rr - ll + 1, 0))

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

Solution (Python):
N, M = map(int,input().split(' '))
maxL=0
minR=N+1
for i in range(M):
    L, R = map(int,input().split(' '))
    if L >= maxL:
        maxL = L
    if R <= minR:
        minR = R
print(max(0,minR-maxL+1))

Solution (Python):
n, m = list(map(int, input().split()))
left_max = -1
right_min = 10**10
for i in range(m):
  l, r = list(map(int, input().split()))
  left_max = max([left_max, l])
  right_min = min([right_min, r])
print(len([i for i in range(1, n+1) if left_max <= i and i <= right_min]))

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

Solution (Python):
n,m = map(int,input().split())
numSet = set(range(1,n+1))
L,R = [],[]
for i 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)

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

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

ans = 0
L, R = 0, 114514

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

print(R - L + 1 if R - L + 1 >= 1 else 0)

Solution (Python):
C,G=map(int,input().split())
Lmax=0
Rmin=C+1
for i in range(G):
    L,R=map(int,input().split())
    if L > Lmax :
        Lmax = L
    else:
        pass
    
    if R < Rmin :
        Rmin = R
    else:
        pass
ans=Rmin-Lmax+1
if ans >= 0 :
    print(ans)
else :
    print(0)

Solution (Python):

n,m = map(int, input().split())
L = [0]*m
R = [0]*m

ans = 0

infi = 1000000000000000

l_max =0
r_min =infi

for i in range(m):
    L[i],R[i] = map(int, input().split())
    if(l_max<L[i]):
        l_max = L[i]
    if(r_min>R[i]):
        r_min = R[i]

ans = (r_min - l_max)+1
if(ans<=0):
    ans =0
        
print(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)
L.sort()
R.sort()
if R[0]>=L[m-1]:
    print(R[0]-L[m-1]+1)
else:
    print(0)

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

l = 0
r = 100001

for i in range(0,m):
    l = max(g[i][0],l)
    r = min(g[i][1],r)
    if l > r:
        break

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

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

Solution (Python):
N,M=list(map(int,input().split()))
L=[];R=[]
for i in range(M):
    a,b=list(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())
mi, ma = 1, n
for _ in range(m):
    l, r = map(int, input().split())
    mi = max(l, mi)
    ma = min(r, ma)
print(max(0, ma - mi + 1))

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

for i in range(m-1):
  l, r = map(int, input().split())
  if mini < l:
    mini = l
  if maxi > r:
    maxi = r
    
if maxi-mini+1 < 0:
  print(0)
else:
  print(maxi-mini+1)

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

L = 1
R = N

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

if R >= L:
    print(R-L+1)
else:
    print(0)

Solution (Python):
n,m= map(int,input().split())
maxl = 0
minr = n
for i in range(m):
    templ,tempr = map(int,input().split())
    maxl = max(maxl,templ)
    minr = min(minr,tempr)
if minr - maxl >= 0:
    print(minr - maxl + 1)
else:
    print(0)

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

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(min(R)-max(L)+1,0))

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

l_M = 0
r_m = 10**5 +3


for i in range(m):
    l,r = map(int,input().split())
    l_M = max(l_M,l)
    r_m = min(r_m ,r)
   
ans = r_m - l_M + 1
if ans >= 0:
    print(ans)
else:
    print(0)

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

max_left = 1
min_right = n

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

print(max(min_right - max_left + 1, 0))

Solution (Python):
n,m=list(map(int, input().split() ))
Firstlis=[]
Secondlis=[]
for i in range(m):
  s=list(map(int,input().split()))
  S1=s[0]
  S2=s[1]
  Firstlis.append(S1)
  Secondlis.append(S2)
Min=max(Firstlis)
Max=min(Secondlis)
if Max>=Min:
    print(Max-Min+1)
else:
    print(0)

Solution (Python):
n,m=map(int,input().split())
maxl=1
minr=n
for i in range(m):
  l,r=map(int,input().split())
  maxl=max(maxl,l)
  minr=min(minr,r)

print(max(minr-maxl+1,0))

Solution (Python):
n, m = map(int, input().split())
L_list = [0]*m
R_list = [0]*m
for i in range(m):
    L, R = map(int, input().split())
    L_list[i] = L
    R_list[i] = R
LR_intersection = min(R_list) - max(L_list)
if LR_intersection >= 0:
    print(LR_intersection+1)
else:
    print(0)

Solution (Python):
n, M, *X = map(int, open(0).read().split())
print(max(0, min(X[1::2]) - max(X[::2]) + 1))

Solution (Python):

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

current_L = 1
current_R = N
for L,R in LRs:
    if current_L < L:
        current_L = L
    if current_R > R:
        current_R = R


if current_R < current_L:
    print(0)
else:
	print(current_R-current_L+1)

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

Solution (Python):
import math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

n,m = MAP()
l,r = [], []
for i in range(m):
    a,b = MAP()
    l.append(a)
    r.append(b)

max_l = max(l)
min_r = min(r)
if min_r<max_l:
    print(0)
    exit()
print(min_r-max_l+1)

Solution (Python):
def main():
    n, m = map(int, input().split())
    lr = [list(map(int, input().split())) for i in range(m)]
    l = 0
    r = 10 ** 5 + 1
    for i in range(m):
        if lr[i][0] > l:
            l = lr[i][0]

        if lr[i][1] < r:
            r = lr[i][1]

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


if __name__ == '__main__':
    main()

Solution (Python):

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

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())

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

Solution (Python):
n,m = list(map(int,input().split()))
left = []
right = []
for i in range(m):
  l,r = list(map(int,input().split()))
  left.append(l)
  right.append(r)
start = max(left)
end = min(right)
cnt = 0 
for i in range(1, n+1):
  if(start <=i and i <= end):
    cnt += 1
print(cnt)

Solution (Python):
n,m = map(int,input().split())
l = [0]*m
r = [0]*m
lmax = 1
rmin = n

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

if lmax>rmin:
    print(0)
else:
    print(rmin-lmax+1)

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

l = []
for i in range(M):
    l.append(lr[i][0])

r = []
for i in range(M):
    r.append(lr[i][1])

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

Solution (Python):
n, m = list(map(int, input().split()))
maxl = 0
minr = 1000000
for i in range(m):
    l, r = list(map(int, input().split()))
    maxl = max(maxl, l)
    minr = min(minr, r)
if minr < maxl:
    print(0)
else:
    print(minr - maxl + 1)

Solution (Python):
def main(): 


    card_cnt, gate_cnt = map(int, input().split())

    min_v, max_v = map(int, input().split())
    for _ in range(1, gate_cnt):
        L,R = map(int, input().split())
        min_v = max(min_v, L)
        max_v = min(max_v, R)
        if min_v > max_v:
            print(0)
            break
    else:
        print(len(range(min_v, max_v+1)))

if __name__ == "__main__":
    main()

Solution (Python):
N,M = (int(i) for i in input().split())
min = 1
max = N

for i in range(M):
    L = list(map(int,input().split()))
    if L[0] > min:
        min = L[0]
    if L[1] < max:
        max = L[1]

if min > max :
    print(0)
else:
    print(max-min+1)

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

maxl = 0
minr = 10 ** 6
for i in range(m):
    l, r = map(int, input().split())
    maxl = max(maxl,l)
    minr = min(minr,r)

if minr < maxl:
    print(0)
else:
    print(minr-maxl+1)

Solution (Python):
nm=list(map(int,input().split()))
low=1
high=nm[0]
for i in range(nm[1]):
    lr=list(map(int,input().split()))
    low=max([low,lr[0]])
    high=min([high,lr[1]])
k=high-low+1
if k<0:
    k=0
print(k)

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

Solution (Python):
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(rMin - lMax + 1 if rMin >= lMax else 0)

Solution (Python):
N, M=map(int, input().split())
lst=[list(map(int, input().split())) for _ in range(M)]
maxL, minR=-1, N
for L, R in lst:
  L, R=L-1, R-1
  maxL=max(maxL, L)
  minR=min(minR, R)
if minR>=maxL:
  print(minR-maxL+1)
else:
  print(0)

Solution (Python):
n,m = map(int,input().split())
ans_l = None
ans_r = None
for i in range(m):
    
    l,r = map(int,input().split())

    
    if i == 0:
        ans_l = l
        ans_r = r

    else:
        ans_r = min(r,ans_r)
        ans_l = max(l,ans_l)
        
    if ans_r - ans_l < 0:
        print(0)
        exit()


print(ans_r-ans_l+1)

Solution (Python):
n,m=map(int,input().split())
li=[list(map(int,input().split())) for i in range(m)]
maxl=0
minr=1000000
for i in range(m):
  maxl=max(li[i][0],maxl)
  minr=min(minr, li[i][1])
if maxl<=minr:
  print(minr-maxl+1)
else:
  print(0)

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

Solution (Python):


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


N, M = map(int, input().split())
L = [0] * M
R = [0] * M
for i in range(M):
    l, r = map(int, input().split())
    L[i] = l
    R[i] = r

left = max(L)
right = min(R)

if left <= right:
    print(right-left+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())

print(0 if min(R)<max(L) else min(R)-max(L)+1)

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)
L = sorted(L)
R = sorted(R)
Lmax = L[-1]
Rmin = R[0]
if Rmin<Lmax:
    print(0)
else:
    print(Rmin-Lmax+1)

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

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

if L <= R:
  print(R - L + 1)
else:
  print(0)

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

Solution (Python):
N, M = map(int,input().split())
lr = [tuple(map(int,input().split())) for _ in range(M)]
ls = []
rs = []
for i in range(M):
  ls.append(lr[i][0])
  rs.append(lr[i][1])

print(max(0,min(rs) - max(ls) + 1))

Solution (Python):

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

Solution (Python):

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

Solution (Python):
N,M = map(int,input().split())
Max_l = 0
Min_r = float('inf')

for i in range(M):
    l,r = map(int,input().split())
    Max_l = max(Max_l,l)
    Min_r = min(Min_r,r)
ans = 0
if Max_l<=Min_r:
    ans = Min_r-Max_l+1
    

   
print(ans)

Solution (Python):

N, M = map(int, input().split())
lr = []
for i in range(M):
    lr.append(list(map(int, input().split())))
l = lr[0][0]
r = lr[0][1]
for i in range(1, M):
    if lr[i][0] > l:
        l = lr[i][0]
    if lr[i][1] < r:
        r = lr[i][1]
    
if r >= l:
    print(r - l + 1)
else:
    print(0)

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    x = [0] * (n + 2)
    for i in range(m):
        l, r = list(map(int, input().rstrip('\n').split()))
        x[l] += 1
        x[r+1] -= 1
    t = 0
    cnt = 0
    for i in range(n + 2):
        t += x[i]
        if t == m:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

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


for i in range(M):
    l,r=map(int,input().split())
    cards_l=max(cards_l,l)
    cards_r=min(cards_r,r)
    
print(max(0,cards_r-cards_l+1))

Solution (Python):
while True:
    try:
        n, m = map(int, input().split())
        start = 1
        end = n
        for i in range(m):
            a, b = map(int, input().split())
            if a > end or b < start:
                print(0)
                for j in range(m-i-1):
                    input()
                break
            start = max(start, a)
            end = min(end, b)   
        else:
            print(end-start+1)
    except:
        break

Solution (Python):
import itertools

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

imos = [0] * (N + 2)
for lr in LR:
    l, r = lr[0], lr[1]
    imos[l] += 1
    imos[r + 1] -= 1
acc = itertools.accumulate(imos)

res = 0
for a in acc:
    if a == M:
        res += 1

print(res)

Solution (Python):
N, M = map(int, input().split())
maxL = 1
minR = N
for i in range(M):
    L, R = map(int, input().split())
    if maxL < L:
        maxL = L
    if minR > R:
        minR = R

if minR - maxL >= 0:
    print(minR - maxL + 1)
else:
    print(0)

Solution (Python):
n, m = map(int, input().split())
tl, tr = 1, n
for _ in range(m):
    l, r = map(int, input().split())
    if tr<l or tl>r:
        tl, tr = 1, 0; break
    else:
        tl = l if tl<l else tl
        tr = r if tr>r else tr
print(tr-tl+1)

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

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

leftest = 1
rightest = N
for gate in gates:
    L = gate[0]
    R = gate[1]
    if L > leftest:
        if L > rightest:
            print(0)
            break
        leftest = L
    if R < rightest:
        if R < leftest:
            print(0)
            break
        rightest = R
else:
    print(rightest - leftest + 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)

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

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

s, e = 1, n
for i in range(m):
    l, r = map(int, input().split(" "))
    if s < l: s = l
    if e > r: e = r

print(e-s+1 if s <= e else 0)

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

L=[i[0] for i in LR]
R=[i[1] for i in LR]
ans=min(R)-max(L)
if ans>=0:
  print(ans+1)
else:
  print(0)

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

min_temp = L[0]
max_temp = R[0]
ans = R[0] - L[0] + 1

for i in range(1, M):
    
    if min_temp < L[i]:
        ans -= (L[i] - min_temp)
        min_temp = L[i]
    
    if R[i] < max_temp:
        ans -= (max_temp - R[i])
        max_temp = R[i]
        
if ans < 0:
    ans = 0
    
print(ans)

Solution (Python):
n,m=[int(i) for i in input().split()]
arr = [0]*(n+2)
for I in range(m):
    l,r=[int(i) for i in input().split()]
    arr[l]+=1
    arr[r+1]-=1
for i in range(1,n+2):
    arr[i]+=arr[i-1]
print(arr.count(m))

Solution (Python):
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n,m = inpl()
cnt = [0]*(n+1)
for i in range(m):
    l,r = inpl()
    cnt[l-1] += 1
    cnt[r] -= 1
cntsum = list(itertools.accumulate(cnt))
print(cntsum.count(m))

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

for i in range(M):
  a,b = map(int,input().split())
  if(i == 0): a_Max,b_Min = a,b
  else: 
    a_Max = max(a_Max,a)
    b_Min = min(b_Min,b)
    
ans = b_Min - a_Max + 1 
if(ans <= 0): ans = 0 
print(ans)

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

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

Solution (Python):
N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]
G = [0] * (N + 1)
for L, R in X:
    G[L - 1] += 1
    G[R] -= 1
for i in range(N):
    G[i + 1] += G[i]
print(sum([1 if g == M else 0 for g in G]))

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 = max(0, min(R) - max(L) + 1)

print(ans)

Solution (Python):
n,m = map(int, input().split())
l = [0] * m
r = [0] * m
for i in range(m):
  a,b = map(int, input().split())
  l[i] = a
  r[i] = b

if min(r) - max(l) < 0:
  print(0)
else:
  print(min(r) - max(l) + 1)

Solution (Python):
n, m = map(int, input().split())
low = 0
high = n+1

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

print(high-low+1 if low <= high else 0)

Solution (Python):
a,b= map(int,input().split())
x, y= zip(*[list(map(int,input().split())) for i in range(b)])
f=  min(y) - max(x) 
if f< 0 :
  print(0)
else :
  print(min(y) - max(x) +1)

Solution (Python):
a,b= map(int,input().split())
x, y= zip(*[list(map(int,input().split())) for i in range(b)])
f=  min(y) - max(x) 
if f< 0 :
  print(0)
else :
  print(min(y) - max(x) +1)

Solution (Python):
n, m = map(int, input().split())
max_value = 10 ** 5
min_value = 1
for i in range(m):
    l, r = map(int, input().split())
    min_value = max(min_value, l)
    max_value = min(max_value, r)

ans = max_value - min_value + 1
if ans < 0:
    ans = 0
print(ans)

Solution (Python):
N ,M  = map(int,input().split())
L = 0
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))

Solution (Python):
n, m = map(int, input().split())
lr = [tuple(map(int, input().split())) for _ in range(m)]
maxl = max(_[0] for _ in lr)
minr = min(_[1] for _ in lr)
if minr - maxl >= 0:
    answer = minr - maxl + 1
else:
    answer = 0
print(answer)

Solution (Python):
while True:
    try:
        N, M = map(int, input().split())
        
        dp = [0]*(10**5+5)
        for i in range(M):
            L, R = map(int, input().split())
            dp[L-1] += 1
            dp[R] -= 1
        
        cnt = 0
        ans = 0
        for i in range(N):
            cnt += dp[i]
            if cnt == M:
                ans += 1
            
        print(ans)
    except:
        break

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

Solution (Python):
n,m=map(int,input().split())
data=[list(map(int,input().split())) for i in range(m)]
import sys
input = lambda: sys.stdin.readline()[:-1]
l,r=0,10**6
for i,j in data:
	l=max(l,i)
	r=min(r,j)
print(max(0,r-l+1))

Solution (Python):

N, M = map(int, input().split())
L = [list(map(int, input().split())) for i in range(M)]
new_L = []
new_R = []
for i in range(M):
    new_L.append(L[i][0])
    new_R.append(L[i][1])
if min(new_R) - max(new_L) >= 0:
    ans = min(new_R) - max(new_L) + 1
else:
    ans = 0
print(ans)

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    lr = [list(map(int, input().rstrip('\n').split())) for _ in range(m)]
    a = [0] * (n + 2)
    for l, r in lr:
        a[l] += 1
        a[r + 1] -= 1
    t = 0
    cnt = 0
    for i in range(1, n + 1):
        t += a[i]
        if t == m:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

Solution (Python):
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
max_L = 1
min_R = 100000
for _ in range(M):
	L, R = map(int, input().split())
	if max_L < L:
		max_L = L
	if R < min_R:
		min_R = R
if min_R - max_L + 1 < 0:
	print(0)
else:
	print(min_R - max_L + 1)

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

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

if y-x >= 0:
    print(y-x+1)
else:
    print(0)

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