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(" "))
min_l = 1
max_r = N

for m in range(M):
    l, r = map(int, input().split(" "))
    if l > min_l:
        min_l = l
    if r < max_r:
        max_r = r

ans = (max_r-min_l)+1
if ans < 0:
    ans = 0

print(ans)

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

Solution (Python):

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

Lmax = 1
Rmin = N

for i in range(M):
    L, R = map(int, input().split())
    if(Lmax<L):
        Lmax = L
    if(R<Rmin):
        Rmin = R

if (Lmax<=Rmin):
    print(Rmin-Lmax+1)
else:
    print(0)

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

Solution (Python):

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

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(l,left)
    right=min(r,right)
print(max(0,right-left+1))

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

ll = 1
rr = N

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

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

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

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

C = CR-CL
if C < 0:
	print(0)
else:
	print(C + 1)

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

Solution (Python):
def main():
    idnum,gatenum = [int(element) for element in input().split(" ")]
    minid = 0
    maxid = idnum

    for gete in range(gatenum):
        condmin,condmax = [int(element) for element in input().split(" ")]
        if(minid<condmin):
            minid = condmin
        if(maxid>condmax):
            maxid = condmax

    if(maxid<minid):
        print(0)
    else:
        print(maxid-minid+1)

if __name__ == "__main__":
    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())
if M==1:
  print(R[0]-L[0]+1)
else:
  for i in range(M-1):
    L[i+1]=max(L[i],L[i+1])
    R[i+1]=min(R[i],R[i+1])
  if R[M-1]>=L[M-1]:
    print(R[M-1]-L[M-1]+1)
  else:
    print(0)

Solution (Python):
N, M = map(int,input().split())
p = [list(map(int,input().split())) for i in range(M)]
lmax, rmin = 0, 10**7
for l, r in p:
    lmax = max(lmax, l)
    rmin = min(rmin, r)
if lmax <= rmin:
    print(rmin-lmax+1)
else:
    print(0)

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

haba = list()
max_L = 0
min_R = 10 ** 5 + 1
for i in range(M):
    L, R = map(int, input().split())
    haba.append([L, R])
    max_L = max(max_L, L)
    min_R = min(min_R, R)

print(max(min_R + 1 - max_L, 0))

Solution (Python):



v = list(map(int, input().split()))
n, m = v[0], v[1]

l, r = [], []
for i in range(m):
	v = list(map(int, input().split()))
	l.append(v[0])
	r.append(v[1])

min = 1
max = n
for i in range(m):
	if l[i] > min:
		min = l[i]
	if max > r[i]:
		max = r[i]

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

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

Solution (Python):
n,m=map(int,input().split())
lm=0
rm=100000

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

if rm-lm<0:
    print(0)
else:
    if lm<=n:
        print(rm-lm+1)
    else:
        print(n-rm+1)

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

L_max = max(L)
R_min = min(R)
ok = 0
for i in range(N):
  if L_max <= i+1 <= R_min:
    ok += 1

print(ok)

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

through_list = []
for i in range(M):
    L,R = map(int,input().split())
    through_list.append([L,R])
through_list.sort()
ans_range = through_list[0]
for i in range(M-1):
    ans_range = [max(ans_range[0],through_list[i+1][0]),min(ans_range[1],through_list[i+1][1])]
ans = ans_range[1]-ans_range[0]+1 
ans = max(0,ans)
print(ans)

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

a_max = -1
b_min = 999999999999999

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

ans = max(b_min - a_max + 1, 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)

max_L = max(L)
min_R = min(R)

if max_L > min_R:
  print(0)
else:
  print(min_R - max_L + 1)

Solution (Python):
N,M = map(int, input().split())
L_max = 0
R_min = N
for i 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 < 0:
    print(0)
else:
    print(R_min-L_max+1)

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

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n,m =na()
ls = 1
rs = n
for i in range(m):
    l, r = na()
    ls = max(ls, l)
    rs = min(rs, r)
if ls > rs:
    print(0)
else:
    print(rs - ls + 1)

Solution (Python):
n,m = map(int, input().split())
head, tail = map(int, input().split())
flag=True
for i in range(m-1):
    l,r = map(int, input().split())
    if head<l and l<=tail:
        head=l
    elif l>tail:
        flag=False
        break
    if tail>r and head<=r:
        tail = r
    elif r<head:
        flag=False
        break
print(tail-head+1 if flag==True 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())
    lmax = max(L, lmax)
    rmin = min(R, rmin)

ans = max(rmin - lmax + 1, 0)
print(ans)

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

L = 1
R = N

for _ in range(M):
    l, r = list(map(int, input().split(' ')))
    if l > L:
        L = l
    if r < R:
        R = r

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

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

Solution (Python):
n, m = map(int, input().split())
t = [tuple(map(int, input().split())) for i in range(m)]
t1 = [tt[0] for tt in t]
t2 = [tt[1] for tt in t]
r = min(t2) - max(t1) + 1
r = r if r >= 0 else 0
print(r)

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

Solution (Python):
L, N = map(int, input().strip().split())
s1, e1 = 0, 10**5
for i in range(N):
    s, e = map(int, input().strip().split())
    s1, e1 = max(s, s1), min(e, e1)
    
print(max(0, e1 - s1+1))

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

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

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

ans = 0
tmp = 0

for i in range(N):
    tmp += count[i]
    if tmp == M:
        ans += 1

print(ans)

Solution (Python):
N, M = map(int, input().split())
min = 0
max = N
for i in range(M):
    L, R = map(int, input().split())
    if L > min:
        min = L
    if R < max:
        max = R
ans = max - min + 1
if ans < 0:
    ans = 0
print("{}".format(ans))

Solution (Python):
x=lambda:map(int,input().split())
N,M=x()
l,r=zip(*[tuple(x()) for _ in range(M)])
print(max(0, 1+min(r)-max(l)))

Solution (Python):
n, m = list(map(int, input().split()))
max_l = 1
min_r = 100000
for _ in range(m):
    l, r = list(map(int, input().split()))
    if l > max_l:
        max_l = l
    if r < min_r:
        min_r = r
ans = min_r - max_l + 1
if ans < 0:
    print(0)
else:
    print(ans)

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

left = 1
right = n
for i in range(m):
  if left < lst[i][0]:
    left = lst[i][0]
  if right > lst[i][1]:
    right = lst[i][1]
if left <= right:
  print(right-left+1)
else:
  print(0)

Solution (Python):


import math

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

s = 0
t = 10**5+1
for i in range(m):
    a,b = list(map(int,input().split(" ")))
    if s < a:
        s = a
    if b < t:
        t = b
if t < s:
    print(0)
else:
    print(t-s+1)

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

Solution (Python):


''' 
------------------------
author : iiou16
------------------------
'''

def main():
    N, M = list(map(int, input().split()))

    L = []
    R = []
    id = list(range(N+1))
    for _ in range(M):
        l, r = list(map(int, input().split()))
        L.append(l)
        R.append(r)

    max_l = max(L)
    min_r = min(R)
    if min_r < max_l:
        print("0")
    else:
        print(min_r - max_l + 1)

if __name__ == '__main__':
    main()

Solution (Python):
from __future__ import print_function


import sys
import math
import string
import fractions
import re
import array
import copy
import functools
import operator

import collections
import itertools
import bisect
import heapq


from heapq import heappush
from heapq import heappop



import random




def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


def main():
    n,m = map(int, input().split())
    
    
    
    left=-100000
    right=sys.maxsize
    for i in range(m):
        t1,t2 = map(int, input().split())
        
        
        if left<t1:
            
            
            left =t1
        if t2<right:
            right = t2
        

    if right<left or n<left:
        print(0)
    else :                      
        if n<right:
            print(n+1 - left)
        else:                    
            print(right+1 - left)
        
            
        
            
        
        

if __name__ == '__main__':
    main()

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

Solution (Python):


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

result = [1, n]

for card in gates:
    if result[0] < card[0]:
        result[0] = card[0]
    if result[1] > card[1]:
        result[1] = card[1]

tmp = result[1] - result[0] + 1 
if tmp >= 0:
    print(tmp)
else:
    print(0)

Solution (Python):

import sys;
sys.setrecursionlimit(20000) 






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

















def doit():
  ma = 0
  mi = 100000 + 1
  for i in range(m):
    l, r = map(int, input().split())
    print(l, r, file=sys.stderr)
    ma = max(ma, l)
    mi = min(mi, r)
    print(ma, mi, file=sys.stderr)
  if ma > mi:
    print("0")
  else:
    print(mi - ma + 1)
  
try:
  doit()
except Exception as e:
  print(e, file=sys.stderr)

Solution (Python):


n,m=map(int,input().split(" "))
mae=0
usiro=100000
for i in range(m):
    s,t=map(int,input().split(" "))
    if(mae<=s):
        mae=s
    if(usiro>=t):
        usiro=t

ans=usiro-mae+1
if(ans<0):
    ans=0
print(ans)

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

L_list = []
R_list = []

for i in range(M):
    L_i,R_i = map(int, input().split() )
    L_list.append(L_i)
    R_list.append(R_i)

result = max([min(R_list)- max(L_list) + 1, 0])

print(str(result))

Solution (Python):
import numpy as np
n, m = map(int, input().split())

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

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)

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

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

Solution (Python):

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

head = 1
tail = n

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

if head > tail:
    print(0)
else:
    print(tail - head + 1)

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

listL = []
listR = []
card = N
for i in range(M):
    l, r = input().split()
    L = int(l)
    R = int(r)

    listL.append(L)
    listR.append(R)

maxL = max(listL)
minR = min(listR)

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

Solution (Python):
N, M = map(int, input().split())
min, max = 0, N
ans = 0

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

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

Solution (Python):
N, M = map(int, input().split())
t = [0] * (N+1)
for i in range(M):
    l, r = map(int, input().split())
    t[l-1] += 1
    t[r] -= 1

for i in range(N):
    if (0 < i):
        t[i] += t[i-1]

t = t[:-1]
m = max(t)
if m == M:
    print(t.count(m))
else:
    print(0)

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

Solution (Python):


inputs = input().split(' ')
N = int(inputs[0])
M = int(inputs[1])

maxL = 1
minR = 100000
for _ in range(M):
    inputs = input().split(' ')
    L = int(inputs[0])
    R = int(inputs[1])
    if L > maxL:
        maxL = L
    if R < minR:
        minR = R

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

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

gate = []

l, r = map(int, input().split())


for i in range(M-1):
    tmpl, tmpr = map(int, input().split())
    if l < tmpl:
        l = tmpl
    if tmpr < r:
        r = tmpr


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):
    Li, Ri = map(int, input().split(' '))
    L.append(Li)
    R.append(Ri)

Lmax = 1
Rmin = N
for i in range(M):
    if Lmax < L[i]:
        Lmax = L[i]
    if Rmin > R[i]:
        Rmin = R[i]

num = Rmin - Lmax + 1
if num < 0:
    num = 0
print(num)

Solution (Python):
if __name__ == '__main__':
    N, M = map(int, input().split())

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

    LR1 = LR[0]
    maxL = LR1[0]
    minR = LR1[1]

    for tmp_LR in LR[1::]:
        tmp_L = tmp_LR[0]
        tmp_R = tmp_LR[1]

        if tmp_L >= maxL:
            maxL = tmp_L

        if tmp_R <= minR:
            minR = tmp_R

    my_list = range(maxL, minR + 1)

    answer = len(my_list)
    print(answer)

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

l0 = L[0][0]
l1 = L[0][1]
for i in L:
    if l0 < i[0]:
        l0 = i[0]

    if l1 > i[1]:
        l1 = i[1]

if l1 < l0:
    print(0)
else:
    print(l1 - l0 + 1)

Solution (Python):
ss=input().split(" ")
N=int(ss[0])
M=int(ss[1])
x=1
y=N
for i in range(M):
  tt=input().split(" ")
  if int(tt[0])>x:
    x=int(tt[0])
  if int(tt[1])<y:
    y=int(tt[1])
last=int(y-x+1)
if last>0:
  print(last)
else:
  print(0)

Solution (Python):
n, m = map(int,input().split())
min_max = 0
max_min = 1000000

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

    if r < max_min:
        max_min = r

print(len(range(min_max, max_min + 1)))

Solution (Python):



def main():
    from itertools import accumulate

    n, m = map(int, input().split())
    imos = [0 for _ in range(n + 1)]

    for i in range(m):
        li, ri = map(int, input().split())
        imos[li - 1] += 1
        imos[ri] += -1

    count = 0

    for i in list(accumulate(imos)):
        if i >= m:
            count += 1

    print(count)


if __name__ == '__main__':
    main()

Solution (Python):
_, m = map(int, input().split())
segs = [tuple(map(int, input().split())) for _ in range(m)]
left, right = segs[0]
for l, r in segs[1:]:
    left = max(left, l)
    right = min(right, r)
print(max(0, right - left + 1))

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

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

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

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

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

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

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


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

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

hoge = []
for i in range(0,M):
    a = [int(x) for x in input().split()]
    hoge.append(a)
    
s = min([x[1] for x in hoge]) - max([x[0] for x in hoge]) +1
if s <= 0:
	print(0)
else:
	print(s)

Solution (Python):
import os, sys, re, math

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

min_idx = 1
max_idx = 1e+5

for _ in range(M):
    l,r = [int(s) for s in input().split(' ')]
    min_idx = max(min_idx, l)
    max_idx = min(max_idx, r)
if max_idx >= min_idx:
    print(min(N, int(max_idx - min_idx + 1)))
else:
    print(0)

Solution (Python):
nm = list(map(int, input().split()))
n = nm[0]
m = nm[1]

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

first_max = 0
second_min = 100001
for gate in gates:
  first_max = max(first_max, gate[0])
  second_min = min(second_min, gate[1])

answer = second_min - first_max + 1
if (answer <= 0):
  print(0)
else:
  print(answer)

Solution (Python):
n,m = map(int,input().split())
l=[0]*m
r=[0]*m
for x in range(m):
    l[x],r[x] = map(int,input().split())
lm=max(l)
rm=min(r)


if (rm-lm)<0:
    print("0")
else:
    print(rm-lm+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())

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

Solution (Python):
d = input().split()
N = int(d[0])
M = int(d[1])

def main():
    dic = {}
    for i in range(1, N+1):
        dic[i] = 0
    Ls = []
    Rs = []
    for i in range(M):
        d = input().split()
        l = int(d[0])
        r = int(d[1])
        Ls.append(l)
        Rs.append(r)
    lm = max(Ls)
    rm = min(Rs)
    if rm - lm >= 0:
        print(rm - lm + 1)
    else:
        print(0)



if __name__ == '__main__':
    main()

Solution (Python):

import numpy as np
n, m = map(int, input().split())

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

gate = np.array(gate)

l = gate[:, 0].max()
r = gate[:, 1].min()

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

Solution (Python):


from sys import stdin

input = stdin.readline

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

L = []
R = []

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

maxL = max(L)
minR = min(R)


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

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

l_list = [LR[i][0] for i in range(M)]
r_list = [LR[i][1] for i in range(M)]

l = max(l_list)
r = min(r_list)

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

Solution (Python):
n,m=map(int,input().split())
l=[]
ans0=0
ans1=n
for i in range(m):
        l.append(list(map(int,input().split())))
for j in range(m):
        ans0=max(ans0,l[j][0])
        ans1=min(ans1,l[j][1])
if ans1-ans0+1<=0:
        print("0")
else:
        print(ans1-ans0+1)

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)

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

Solution (Python):
N, M = map(int, input().split())
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
low = array[0][0]
up = array[0][1]

flag = True
for i in range(1,M):
    if array[i][0] > up:
        flag = False
        break
    if array[i][0] > low:
        low = array[i][0]
    if array[i][1] < up:
        up = array[i][1]
    
if flag:
    print(up - low + 1)
else:
    print(0)

Solution (Python):
a, b = map(int, input().split())

maxf = 1
minf = a

for i in range(b):
    c, d = map(int, input().split())
    if(maxf < c):
        maxf = c
    if(minf > d):
        minf = d

if(minf-maxf < 0):
    print(0)
else:
    print(minf -maxf + 1)

Solution (Python):
a = input().split()
low = 0
high = int(a[0])
for i in range(int(a[1])):
    b = input().split()
    if int(b[0]) > low:
        low = int(b[0])
    if int(b[1]) < high:
        high = int(b[1])

r = high - low
if r < 0:
    print("0")
    exit()
print(r + 1)

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

N,M=map(int,input().split())
LRs = []
Min=-1
Max=10**5
for _ in range(M):
  L,R=map(int,input().split())
  Min = L if L > Min else Min
  Max = R if Max > R else Max
if Max-Min+1 > 0:
  print(Max-Min+1)
else:
  print(0)

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

print(ans)

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

for i in range(m):
    low,high =[int(i) for i in input().split()]
    if(under<low):
        under=low
    if(top>high):
        top=high

if(top<under):
    print(0)
else:
    print(top-under+1)

Solution (Python):
from sys import stdin

def main():
    N, M = map(int, input().split())
    LR = [tuple(map(int, input().split())) for _ in [0] * M]

    l, r = list(zip(*LR))
    lmax = max(l)
    rmin = min(r)
    ans = 0
    for i in range(lmax, rmin + 1):
        if i <= N:
            ans += 1
    print(ans)

input = lambda: stdin.readline()
main()

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

l, r = 1, N

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

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

Solution (Python):


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

l, r = 0, N
for l_r in L_R:
    if l < l_r[0]:
        l = l_r[0]
    if l_r[1] < r:
        r = l_r[1]

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

Solution (Python):
import sys

def solve(n, m, l, r):
    l_max = max(l)
    r_min = min(r)
    if l_max > r_min:
        return 0
    else:
        return r_min - l_max + 1


if __name__ == '__main__':

    n, m = map(int, sys.stdin.readline().strip().split(" "))
    l = []
    r = []
    for i in range(m):
        l_tmp, r_tmp = map(int, sys.stdin.readline().strip().split(" "))
        l.append(l_tmp)
        r.append(r_tmp)
    print solve(n, m, l, r)

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

Solution (Python):


import sys
input = sys.stdin.readline



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


ok = [0] * (N + 2)
for i in range(M):
    L, R = map(int, input().split())
    ok[L] += 1
    ok[R + 1] -= 1

ans = 0
tmp = 0
for i in range(1, N + 1):
    tmp += ok[i]
    
    if tmp == M:
        ans += 1

print(ans)

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

maxl = max(lr[j][0] for j in range(len(lr)))
minr = min(lr[j][1] for j in range(len(lr)))
if n > minr:
    print(max(minr - maxl + 1,0))
else:
    print(max(n - maxl + 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)

masterkey = min(R) - max(L) + 1
if masterkey <= 0:
    print(0)
if masterkey > 0:
    print(masterkey)

Solution (Python):
def prison(N: int, M: int, queries: list)->list:
    l, r = 1, N

    for L, R in queries:
        l = max(l, L)
        r = min(r, R)

    return max(0, r-l+1)


if __name__ == "__main__":
    M = 0
    N, M = map(int, input().split())
    queries = [tuple(int(s) for s in input().split()) for _ in range(M)]

    ans = prison(N, M, queries)
    print(ans)

Solution (Python):
N, M = map(int, input().split())        
                                        
x = []                                  
for i in range(M):                      
    l,r = map(int, input().split())     
    x.append([l, r])                    
                                        
                                        
cand = list(range(1, N+1))              
                                        
for i, p in enumerate(x):               
    l,r = p                             
    if (r < cand[0]) or (l > cand[-1]): 
        del cand[0:]                    
        break                           
                                        
    if cand[0] < l:                     
        del cand[:(l - cand[0])]        
    if cand[-1] > r:                    
        del cand[(r-cand[0]+1):]        
                                        
print(len(cand))

Solution (Python):
import fractions
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)
ans = 0
M = max(l)
m = min(r)
for i in range(1,n+1):
    if M <= i and i <= m:
        ans += 1
print(ans)

Solution (Python):
n,m= input().split()
n = int(n)
m = int(m)
g=[input().split() for i in range(m)]
av =[-1,1000000]
for i in range(m):
  av[0] = max(int(g[i][0]),av[0])
  av[1] = min(int(g[i][1]),av[1])
print(max(av[1] -av[0]+1,0))

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

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



ans = -1
l_min , r_max = list(map(int, input().split()))
for i in range(M-1):
    l, r = list(map(int, input().split()))
    if r_max < l or r < l_min:
        ans = 0
        break

    if l_min <= l and l <= r_max:
        l_min = l

    if l_min <= r and r <= r_max:
        r_max = r


if ans == 0:
    print(ans)
else:
    print(r_max - l_min + 1)

Solution (Python):
N,M = [int(i) for i in input().split()]
L_max ,R_min = [int(i) for i in input().split()]
for _ in range(M-1):
    temp = [int(i) for i in input().split()]
    if L_max < temp[0]:
        L_max = temp[0]
    if R_min > temp[1]:
        R_min = temp[1]
print(max(R_min - L_max + 1,0))

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

for i in range(M):
    L, R = map(int, input().split())
    L_l.append(L)
    R_l.append(R)

cont = min(R_l) - max(L_l) + 1
if cont <= 0:
    print(0)
else:
    print(cont)

Solution (Python):
def ando(x, y):
    return (max(x[0], y[0]), min(x[1], y[1]))

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

r = (0, N)
for _ in range(M):
    lr = tuple(map(int, input().split()))
    r = ando(r, lr)

if r[1] >= r[0]:
    print(r[1] - r[0] + 1)
else:
    print(0)

Solution (Python):
N, M = [int(i) for i in raw_input ().split()]
X, Y = [int(i) for i in raw_input ().split()]
for i in range(M - 1):
    x, y = [int(i) for i in raw_input ().split()]
    X = max(X, x)
    Y = min(Y, y)
print max(0, Y - X + 1)

Solution (Python):
import numpy as np
from collections import defaultdict
from functools import reduce




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

l = 0
r = n+1

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

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

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

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


ans=0
Gates=[0]*(N+2)

for i in range(M):
    L,R=map(int,input().split())
    Gates[L]+=1
    Gates[R+1]-=1

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

for i in range(1,N+1):
    if Gates[i]==M:
        ans+=1

print(ans)

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

arr = [0, n+1]

for i in range(m):
    a , b = map(int, input().split())
    if arr[0] < a:
        arr[0] = a
    if arr[1] > b:
        arr[1] = b

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

if ans >= 0:
    print(arr[1] - arr[0] + 1)
else:
    print(0)

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)	

num = min(Rs) - max(Ls) + 1

if num < 0:
	num = 0

print(num)