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

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

Solution (Python):

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

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

Solution (Python):

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

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

Solution (Python):
def main():
	import itertools
	n,m = map(int,input().split())
	l = [list(map(int,input().split())) for i in range(m)]
	gaterange = []
	
	li = []
	ri = []
	for i in range(m):
		li.append(l[i][0])
		ri.append(l[i][1])
	print(len(list(range(max(li)-1,min(ri)))))
	




if __name__ == '__main__':
	main()

Solution (Python):
N, M= map(int, input().split())
L_list=list()
R_list=list()
for i in range(M):
	L, R = map(int, input().split())
	L_list.append(L)
	R_list.append(R)
	
result=min(R_list)-max(L_list)+1

if result < 0:
	print(0)
else:
	print(result)

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

Solution (Python):
import sys
from itertools import accumulate
n, m = [int(i) for i in sys.stdin.readline().split()]
ls = [0 for i in range(n+1)]
for i in range(m):
    l, r = [int(i) for i in sys.stdin.readline().split()]
    l, r = l - 1, r - 1
    ls[l] += 1
    ls[r + 1] -= 1
ls = [i for i in accumulate(ls)]
cnt = 0
for l in ls[:-1]:
    if l == m:
        cnt += 1
print(cnt)

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

st = 1
en = N
for _ in range(M):
    L, R = map(int, input().split())
    st = max(st, L)
    en = min(en, R)

ans = en - st
if ans < 0:
    print(0)
else:
    print(ans + 1)

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

x=[]
y=[]

for i in range(m):
    l,r=map(int,input().split())
    x.append(l)
    y.append(r)

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

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

lmax = 0
rmin = 10 ** 5
for i in range(m):
    lmax = max(lmax,l[i])
    rmin = min(rmin,r[i])

if lmax <= rmin:
    print(rmin - lmax + 1)
else:
    print("0")

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

lp = 1
rp = n

for i in range(m):
    if lp < lr[i][0]:
        lp = lr[i][0]
    if rp > lr[i][1]:
        rp = lr[i][1]
else:
    if rp - lp < 0:
        ans = 0
    else:
        ans = rp-lp+1

print(ans)

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

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


if __name__ == '__main__':
    solve()

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

ans = 0
for i in range(1,n+1) :
  if l <= i <= r :
    ans += 1
print(ans)

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

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

under = 1
top = 10**6+10
for i in range(m):
    if LRs[i][0] > under:
        under = LRs[i][0]
    if LRs[i][1] < top:
        top = LRs[i][1]

if not top >= under:
    print(0)
else:

    print(top - under + 1)

Solution (Python):

import sys
import itertools
import math

N, M=input().split()
N=int(N)
M=int(M)
i=0
ID_list=[]
ID_sublist=[]
gate_possible_list=[]

for i in range(N):
 ID_list.append(i+1)


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




















i=0
j=0
k=0
l=0
m=0
mini=gate_list[0][0]
maxi=gate_list[0][1]
for i in range(M):
  if mini <gate_list[i][0]:
      mini=gate_list[i][0]
i=0
for i in range(M):
  if maxi >gate_list[i][1]:
      maxi=gate_list[i][1]

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

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

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

Solution (Python):
n,m = map(int,input().split())
conditions = [ tuple(map(int,input().split())) for _ in range(m)]
max_l = 0
min_r = float("INF")
for l,r in conditions:
  max_l = max(max_l,l)
  min_r = min(min_r,r)
if min_r < max_l:
  print(0)
else:
  print(min_r - max_l + 1)

Solution (Python):
from sys import stdin
n,m = map(int,stdin.readline().split())









l=0
r=n
for _ in range(m):
  a,b = map(int,stdin.readline().split())
  l = l if l>a else a
  r = r if r<b else b
print(max([0,r-l+1]))

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









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

Solution (Python):
n,m = map(int,input().split())
ll = [list(map(int,input().split())) for _ in [0]*m]
l=[]
r=[]
for i in range(m):
  l.append(ll[i][0])
  r.append(ll[i][1])
l = max(l)
r = min(r)

  




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

Solution (Python):
n,m = map(int,input().split())
l=0
r=n
for _ in range(m):
  a,b = map(int,input().split())
  l = max([a,l])
  r = min([b,r])
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)]
l_max=1
r_min=n
for i in range(m):
    l_now=l[i][0]
    r_now=l[i][1]
    if l_now>l_max:
        l_max=l_now
    if r_now<r_min:
        r_min=r_now
    
print(max(r_min-l_max+1,0))

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

Solution (Python):

si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n,m=nm()
l,r=0,10000000

for i in range(m):
    cl,cr=nm()
    l=max(cl,l)
    r=min(cr,r)

if r-l>=0:
    print(r-l+1)
elif r-l<0:
    print(0)

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

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

l_max = 1
r_min = N

for l, r in lr:
    l_max = max(l, l_max)
    r_min = min(r, r_min)

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

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

Solution (Python):
from sys import stdin


def fetch_one_line():
    return stdin.readline().rstrip()


def fetch_int_input():
    return [int(s) for s in fetch_one_line().split()]


def fetch_inputs(times):
    return [fetch_one_line() for _ in range(times)]


def fetch_int_inputs(times):
    return [fetch_int_input() for _ in range(times)]


N, M = fetch_int_input()
gate_len = fetch_int_inputs(M)

l, r = 0, N
for gl in gate_len:
    if l < gl[0]:
        l = gl[0]
    if r > gl[1]:
        r = gl[1]



result = r - l + 1
if result < 0: result = 0

print(result)

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

Solution (Python):
N, M = map(int, input().split())
l_max = -float("inf")
r_min = float("inf")

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>N:
    r_min = N
print(len(range(l_max, r_min+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()
R.sort()
print(max(R[0]-L[-1]+1,0))

Solution (Python):

n, m = map(int, input().split())
max_value, min_value = -1, -1
for i in range(m):
  tmp_min_value, tmp_max_value = map(int, input().split())
  min_value = max([min_value, tmp_min_value]) if min_value >= 0 else tmp_min_value
  max_value = min([max_value, tmp_max_value]) if max_value >= 0 else tmp_max_value

min_value = min_value if min_value <= n else n
max_value = max_value if max_value <= n else n

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

Solution (Python):
n,m=map(int,input().split())
llis=[]
rlis=[]
for i in range(m):
  l,r=map(int,input().split())
  llis.append(l)
  rlis.append(r)
llis.sort()
ok=True
for i in range(m):
  if llis[m-1]>rlis[i]:
    ok=False
if ok:
  print(min(rlis)-llis[m-1]+1)
else:
  print(0)

Solution (Python):
n,m=map(int,input().split())
ml,mr=0,float('inf')
for i in range(m):
      l,r=map(int,input().split())
      ml=max(l,ml)
      mr=min(r,mr)
print(max(mr-ml+1,0))

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

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

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

Solution (Python):
n, m = map(int, input().split())
left = 1
right = n
for _ in range(m):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)
print(max(right-left+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)

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

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


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

Solution (Python):
def main():
    N, M = map(int, input().split())
    tl, tr = map(int, input().split())
    for i in range(M-1):
        L, R = map(int, input().split())
        if tl < L:
            tl = L
        if tr > R:
            tr = R
    if tr >= tl:
        print(tr - tl + 1)
    else:
        print(0)
if __name__ == '__main__':
    main()

Solution (Python):
import sys

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

tmp1 = 0
tmp2 = float('inf')
val = [[] for i in range(M)]
ans = 0
for i in range(M):
    L,R = map(int, input().split())
    val[i].append(L)
    val[i].append(R)
    if R < tmp1:
        print(ans)
        sys.exit()
    if L > tmp1:
        tmp1 = L
    if R < tmp2:
        tmp2 = R
if tmp2-tmp1+1 < 0:
    print(0)
else:
    print(tmp2-tmp1+1)

Solution (Python):
N,M = map(int,input().split())
array = []
for i in range(M):
    a = list(map(int,input().split()))
    array.append(a)
maxa = max([i[1] for i in array])
array2 = [0 for i in range(maxa+2)]
for i in array:
    array2[i[0]] += 1
    array2[i[1]+1] -= 1
for i in range(len(array2)-1):
    array2[i+1] += array2[i]
print(array2.count(M))

Solution (Python):
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def MAP(): return [int(s) for s in input().split()]

N,M = MAP()
G = [MAP() for _ in range(M)]

l = 0
r = 10**5+1
for a,b in G:
	if a>l: l=a
	if b<r: r=b


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

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

a = {i for i in range(1, N+1)}
L_list = []
R_list = []
for i in range(M):
    L, R = map(int, input().split())
    L_list.append(L)
    R_list.append(R)
    
    
    

print(max(min(R_list) - max(L_list) + 1, 0))

Solution (Python):
def main():
    LR = open(0).read().split()[2:]
    MAX_L = max(map(int, LR[::2]))
    MIN_R = min(map(int, LR[1::2]))
    print(MIN_R - MAX_L + 1 if MAX_L <= MIN_R else 0)
    return


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

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

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

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

Solution (Python):
N,M = map(int,input().split())
mnr = []
mxr = []
for i in range(1,M+1):
  mn , mx = map(int,input().split())
  mnr.append(mn)
  mxr.append(mx)
x = max(min(mxr)-max(mnr) +1,0)
print(x)

Solution (Python):

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

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

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

import sys
n, m = map(int, sys.stdin.readline().split())

L = 0
R = n
for _ in range(m):
    Li, Ri = map(int, sys.stdin.readline().split())
    if L < Li:
        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())
t=[list(map(int,input().split())) for _ in range(m)]
tmp=[1,n]
for i in range(m):
  l,r=t[i]
  if tmp[0]<l:
    tmp[0]=l
  if r<tmp[1]:
    tmp[1]=r
print(max(0,tmp[1]-tmp[0]+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(min(r) - max(l) + 1, 0))

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

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

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

ans_l = 1
ans_r = n

for i in range(m):
  l, r = (int(i) for i in input().split())
  if l > ans_l:
    ans_l = l
    
  if r < ans_r:
    ans_r = r

print(max(0, ans_r - ans_l + 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())

min = L[0]
max = R[0]

for j in range(1,M):
    if min < L[j]:
        min = L[j]
    if max > R[j]:
        max = R[j]
if max-min+1 >= 0:
	print(max-min+1)
else:
  print(0)

Solution (Python):
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(
            think(4, [[1, 3], [2, 4]]),
            2
        )

    def test_2(self):
        self.assertEqual(
            think(10, [[3, 6], [5, 7], [6, 9]]),
            1
        )

    def test_3(self):
        self.assertEqual(
            think(100000, [[1, 100000]]),
            100000
        )


def solve():
    n, list_of_l_and_r = read()
    result = think(n, list_of_l_and_r)
    write(result)


def read():
    n, m = read_int(2)
    list_of_l_and_r = []
    for _ in range(m):
        list_of_l_and_r.append(read_int(2))
    return n, list_of_l_and_r


def read_int(n):
    return read_type(int, n)


def read_float(n):
    return read_type(float, n)


def read_type(t, n):
    return list(map(lambda x: t(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n, list_of_l_and_r):
    left = 1
    right = n
    for l, r in list_of_l_and_r:
        if right < l or r < left:
            return 0
        left = max(l, left)
        right = min(r, right)
    return right - left + 1


def write(result):
    print(result)


if __name__ == '__main__':
    
    solve()

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

Solution (Python):
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
l=0
r=10**5+1
for i in range(m):
    a,b=map(int,input().split())
    l=max(a,l)
    r=min(b,r)
res=r-l+1
if res>=0:
    print(res)
else:
    print(0)

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

Solution (Python):
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
left = lr[0][0]
right = lr[0][-1]
cnt = 0

for i in lr:
  if i[0] > left:
    left = i[0]
  
  if i[-1] < right:
    right = i[-1]

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

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n, m = rli()
    bl, br = 1, n
    for i in range(m):
        l, r = rli()
        bl = max(bl, l)
        br = min(br, r)
    print(max(br-bl+1, 0))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
  n, m = list(map(int, input().split()))
  l = 0
  r = 100001
  for i in range(m):
    a, b = map(int, input().split())
    if l < a:
      l = a
    if b < r:
      r = b

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

main()

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

Solution (Python):
N,M =map(int,input().split())
left = 0
right = 10**5 + 1
for i in range(M):
    l,r = map(int,input().split())
    left = max(l,left)
    right = min(r,right)
ans = right - left + 1
if(right - left < 0):
    ans = 0
print(ans)

Solution (Python):
N,M=map(int,input().split())
a=[]
b=[]
for i in range(M):
  L,R=map(int,input().split())
  a.append(L)
  b.append(R)
c=sorted(a)
d=sorted(b)
if d[0]-c[M-1]>=0:
  print(d[0]-c[M-1]+1)
else:
  print(0)

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

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

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

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

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

print (sum(1 for x in l if x == M))

Solution (Python):
import heapq

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

left = heapq.nlargest(1, head)[0]
right = heapq.nsmallest(1, tail)[0]


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

Solution (Python):
N,M = list(map(int,input().split()))
A = []
B = []
for _ in range(M):
    temp = list(map(int,input().split()))
    A.append(temp[0])
    B.append(temp[1])
cnt = 0
minA = max(A)
maxA = min(B)
if maxA - minA + 1 > 0:
    print(maxA - minA + 1)
else:
    print(0)

Solution (Python):
n,m=map(int,input().split())
L=[list(map(int,input().split())) for _ in range(m)]
 
lm=0
rm=float('inf')
for i in range(m):
      lm=max(L[i][0],lm)
      rm=min(L[i][1],rm)
print(max(0,rm-lm+1))

Solution (Python):
from collections import defaultdict,deque, Counter
import sys,heapq,bisect,math,itertools,string,queue,copy,time
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-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())
ans = 0
maxL = 0
minR = INF 
[n,m] = inpl()
for i in range(m):
    [L,R] = inpl()
    maxL = max(maxL,L)
    minR = min(minR,R)

ans = minR - maxL +1





print(max(ans,0))

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

Min = max(l)
Max = min(r)
for i in range(n):
    if Min<=i+1<=Max:
        ans += 1
print(ans)

Solution (Python):
N, M = map(int, input().split())
l_max, r_min = 0, 100000000

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

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

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

Solution (Python):
import numpy as np
N,M=map(int,input().split(' '))
LR=[list(map(int,input().split(' '))) for i in range(M)]
LR=np.array(LR)

if min(LR[:,1])-max(LR[:,0])+1>0:
  print(min(LR[:,1])-max(LR[:,0])+1)
else:
  print(0)

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

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

Solution (Python):
import numpy as np
N, M = [int(i) for i in input().split()]
L = [0]*M
R = [0]*M
for i in range(M):
    L[i], R[i] = [int(i) for i in input().split()]

Rmin = np.min(R)
Lmax = np.max(L)
if Rmin >= Lmax:
    num = np.min(R) - np.max(L) + 1
else:
    num = 0
print(num)

Solution (Python):

n,m = map(int, input().split())
G=[list(map(int,input().split())) for i in range(m)]

MIN=0
MAX=10**9
 
for a,b in G:
    MIN=max(MIN,a)
    MAX=min(MAX,b)
 
print(max(0,MAX-MIN+1))

Solution (Python):
from itertools import accumulate

n, m = map(int, input().split())
card = [0] * (n + 1)

for _ in range(m):
    l, r = map(int, input().split())
    card[l-1] += 1
    card[r] -= 1

ans = 0
for i in accumulate(card):
    if i == m:
        ans += 1
print(ans)

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

MOD = 10 ** 9 + 7


def main():
    N, M = map(int, input().split())
    ans = 0
    min_r = N
    max_l = 0

    for i in range(M):
        L, R = map(int, input().split())
        min_r = min(min_r, R)
        max_l = max(max_l, L)

    print(max(min_r - max_l + 1, 0))


if __name__ == '__main__':
    main()

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_max < L:
        L_max = L
    if R_min > R:
        R_min = R

if R_min < L_max:
    print(0)
else:
    print(R_min - L_max + 1)

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

Solution (Python):
n,m = map(int,input().split())
l = []
for i in range(m):
    l.append(list(map(int,input().split())))
ans = 0
lmax = 0
rmin = float('inf')
for i in l:
    if i[0]>lmax:
        lmax = i[0]
    if i[1]<rmin:
        rmin = i[1]
if lmax <= rmin:
    ans = rmin - lmax + 1
print(ans)

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

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

Solution (Python):
n, m = list(map(int, input().split()))
lr = [[0] * 2] * m

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

min = 0
max = n

for i in range(m):
    if (min < lr[i][0]):
        min = lr[i][0]

    if (lr[i][1] < max):
        max = lr[i][1]

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

Solution (Python):
N, M = [int(x) for x in input().split()]
L = [0] * (N + 1)
for m in range(M):
  l, r = [int(x) for x in input().split()]
  L[l-1] += 1
  L[r] -= 1
  
for i, l in enumerate(L[1:], start=1):
  L[i] = L[i-1] + l
 
print(L.count(M))

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,M=map(int,input().split())
    L,R=[0]*M,[0]*M
    for i in range(M):
        L[i],R[i]=map(int,input().split())
    print(max(0,min(R)-max(L)+1))

if __name__ == '__main__':
    main()

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

sup = []
inf = []
  
for i in range(len(L)):
  sup.append(L[i][1])
  inf.append(L[i][0])
  
sup = min(sup)
inf = max(inf)

if sup - inf < 0:
  print(0)
else:
  print(sup - inf + 1)

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

c_min = -1
c_max = n+1

for i in range(m):
    tmp_min, tmp_max = map(int, input().split())
    if tmp_min > c_min:
        c_min = tmp_min
    if tmp_max < c_max:
        c_max = tmp_max

if c_max >= c_min:
    print(c_max - c_min +1)
else:
    print(0)

Solution (Python):

import sys










def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]

    l, r = [], []
    for _ in range(m):
        li, ri = [int(x) for x in sys.stdin.readline().split()]
        l.append(li)
        r.append(ri)
    
    print(min(r) - max(l) + 1 if min(r) >= max(l) else 0)



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

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

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

a=0
b= float('inf')
for i in range(m):
      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())
p,q = 1,n
for i in range(m):
    l,r = map(int,input().split())
    p = max(p,l)
    q = min(q,r)
if q>=p:
    print(q-p+1)
else:
    print(0)

Solution (Python):
n,m = map(int,input().split())
key = []
for i in range(1,n+1):
    key.append(i)
    
ls = [0,100000]
for i in range(m):
    L,R = map(int,input().split())
    if L > ls[0]:
        ls[0] = L
    if R < ls[1]:
        ls[1] = R
    
ans = []    
for i in range(ls[0],ls[1]+1):
    ans.append(i)
    
print(len(set(ans)&set(key)))

Solution (Python):
N, M = list(map(int, input().split()))
f = 0
for i in range(M):
    if i==0:
        L1, R1 = list(map(int, input().split()))
    else:
        L2, R2 = list(map(int, input().split()))
        if R1 < L2 or L1 > R2:
            print(0)
            f+=1
            break
        elif L2<L1 and R1<R2:
            continue
        elif L1<L2 and R2<R1:
            L1 = L2
            R1 = R2
        elif L2<L1 and R2<R1:
            R1 = R2
        elif L1<L2 and R1<R2:
            L1 = L2
        else:
            continue
s = set(range(L1,R1+1))
if f!=1:
    print(len(s))

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

min=1
max=n


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

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

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

lmin = 1
rmax = n
for _ in range(m):
    l,r = map(int,input().split())
    if l > lmin:
        lmin = l
    if rmax > r:
        rmax = r
    
print(max(rmax - lmin + 1, 0))

Solution (Python):
N, M = map(int, input().split())
LR = sorted([list(map(int, input().split())) for i in range(M)], key=lambda x: (x[0], -x[1]))

minv = 0
maxv = N
for l, r in LR:
    if maxv < l:
        print(0)
        exit()
    minv = max(minv, l)
    maxv = min(maxv, r)

print(maxv-minv+1)

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

p_c()

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

min_id = 1
max_id = float('INF')
na_flag = False
for m in range(M):
    left = LR[m][0]
    right = LR[m][1]
    if right < min_id or max_id < left:
        na_flag = True
        break

    if min_id < left and left <= max_id:
        min_id = left
    if right < max_id and min_id <= right:
        max_id = right

if na_flag:
    print(0)
else:
    print(max_id - min_id + 1)