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())
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):
def solve():
    n, m = map(int, input().split())
    lrs = list(list(map(int, input().split())) for _ in range(m))
    max_l, min_r = max([l for l, r in lrs]), min([r for l, r in lrs])
    ans = min_r - max_l + 1
    return ans if ans > 0 else 0


if __name__ == "__main__":
    print(solve())

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

Solution (Python):

n, m = [int(el) for el in input().split(' ')]
max_l = 0
min_r = n
for i in range(m):
    l, r = [int(el) for el in input().split(' ')]
    max_l = max(max_l, l)
    min_r = min(min_r, r)

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

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

x = []
y = []

for i in range(m):
    array = list(map(int, input().split()))
    x.append(array[0])
    y.append(array[1])

a = max(x[:])
b = min(y[:])

print(len(list(range(a, b+1))))

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

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

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

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

print(max(0,rm-lm+1))

Solution (Python):
n,m=map(int, input().split()) 
s=[list(map(int, input().split())) for i in range(m)]
R=0
L=0
s=sorted(s)
L=s[m-1][0]
s=sorted(s, key=lambda x:x[1])
R=s[0][1]
if R-L>=0:
    print(R-L+1)
else:
    print(0)

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

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

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

lower = 1
upper = N
for l, u in lst:
    if l > lower:
        lower = l

    if u < upper:
        upper = u

    if upper < lower:
        print(0)
        exit()

print(upper - lower + 1)

Solution (Python):

n,m=map(int,input().split())
lr=[list(map(int,input().split())) for _ in range(m)]
x=[0 for _ in range(n+1)]
for l,r in lr:
	x[l-1]+=1
	x[r]-=1
for i in range(1,n+1):
	x[i]+=x[i-1]
res=0
for t in x:
	if t==m:
		res+=1
print(res)

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

lma, rmi = max(l), min(r)

if lma > rmi:
    print(0)
else:
    if rmi <= n:
        print(rmi - lma + 1)
    elif lma <= n:
        print(n - lma + 1)
    else:
        print(0)

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

ans = minR - maxL + 1
print(max(ans, 0))

Solution (Python):
n, m = map(int,input().split())
maxL, minR = 1, n
L = []
R = []
for i in range(m):
  l, r = map(int,input().split())
  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
R = 10**5 +2
for i in range(M):
    l,r = map(int, input().split())
    if L < l:
        L = l
    if R > r:
        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())
L = [0]*m
R = [0]*m
for i in range(m): 
    *I, = map(int, input().split())
    L[i] = I[0]
    R[i] = I[1]
print(max(min(R)-max(L)+1, 0))

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)

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

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)

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

Solution (Python):

import sys
count = 0
n, m = list(map(int, input().split()))
temp_l, temp_r = list(map(int, input().split()))
for i in range(m-1):
  l,r = list(map(int, input().split()))
  if temp_r < l or r < temp_l:
    print(0)
    sys.exit()
  else:
    temp_l = max(temp_l, l)
    temp_r = min(temp_r, r)
print(temp_r - temp_l +1)

Solution (Python):
def resolve():
    N,M = map(int,input().split())
    LL,RR = [],[]
    for i in range(M):
        L,R = map(int,input().split())
        LL.append(L)
        RR.append(R)

    bool_id = [1]*N
    lmin = -1
    rmax = 10**10
    for i in range(M):
        lmin = max(lmin,LL[i])
        rmax = min(rmax,RR[i])
    print(max(rmax-lmin+1,0))

resolve()

Solution (Python):
N,M = map(int, input().split())
L = [0] * M
R = [0] * M
maxL = 1
minR = N

for i in range(M):
    L[i], R[i] = map(int, input().split())
    maxL = max([maxL, L[i]])
    minR = min([minR, R[i]])

ans = minR - maxL + 1
print(max([ans,0]))

Solution (Python):
n,m = map(int,input().split())
L = []
R = []

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

ans = min(R) - max(L) + 1

if ans >0:
	print(ans)
else:
	print(0)

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

l_max = lr[0][0]
r_min = lr[0][1]

for i in range(1,m):
    if l_max < lr[i][0]:
        l_max = lr[i][0]
    if r_min > lr[i][1]:
        r_min = lr[i][1]

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

Solution (Python):

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

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

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

an = min(rr)-max(ll)+1
print(max(0,an))

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

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

an = min(rr)-max(ll)+1
if an >0:
	print(an)
else:
	print(0)

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

ans = min(R)-max(L)+1
print(ans if ans >0 else "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)

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

Solution (Python):
N, M = [int(v) for v in input().split()]

cards = [0] * (N + 2)

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

for i in range(1, N + 1):
    cards[i] += cards[i - 1]
print(len([i for i in range(1, N + 1) if cards[i] == M]))

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 = 1
r_min = 100000
for i in range(m):
    if l_max < l[i]:
        l_max = l[i]
    if r_min > r[i]:
        r_min = r[i]

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

Solution (Python):
from collections import defaultdict
n, m = map(int, input().split())
left = []
right = []
for i in range(m):
    l, r = map(int, input().split())
    left.append(l)
    right.append(r)

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

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

for i in range(1, n+1):
    s[i] = s[i] + s[i-1]

print(s.count(m))

Solution (Python):
n, k = list(map(int, input().split()))
min_list = []
max_list = []
for i in range(k):
    j, l = list(map(int, input().split()))
    min_list.append(j)
    max_list.append(l)


print(max(0, min(max_list) - max(min_list) + 1))

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

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

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

Solution (Python):

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

Solution (Python):
N,M=map(int,input().split())
C=[list(map(int,input().split())) for _ in range(M)]
G=[0]*(N+1)
for l,r in C:
  G[l-1]+=1
  G[r]-=1
gg=0
li=[]
for g in G:
  gg=gg+g
  li.append(gg)
print(li.count(M))

Solution (Python):
temp=input().split()
N=int(temp[0])
M=int(temp[1])
Lmax=int(0)
Rmin=int(N-1)

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

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

a=1
b=10**5
for i in range(m):
    if l[i][0]>=a:
        a=l[i][0]
    if l[i][1]<=b:
        b=l[i][1]

print(max(0,b-a+1))

Solution (Python):
import numpy as np

N, M= (int(i) for i in input().split())  
lr = [[int(i) for i in input().split()] for i in range(M)] 	
lr = np.array(lr)
l = list(lr[:, 0])
r = list(lr[:, 1])
d = min(r)-max(l)+1
if d>0:
	print(d)
else:
	print(0)

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

bottom=1
top=N

for i in range(M):
    if L[i] > bottom:
        bottom = L[i]
    if R[i] < top:
        top = R[i]

if top >= bottom:
    ans = top-bottom+1
else:
    ans = 0
print(ans)

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

maxL = float("-inf")
minR = float("inf")

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

print(max(0, minR - maxL + 1))

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

Lmax = 1
Rmin = N
for line in sys.stdin:  
    L, R = map(int, line.split())
    Lmax = max(L, Lmax)
    Rmin = min(R, Rmin)

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

Solution (Python):
import numpy as np

n,m = map(int,input().split())
count = 0
Li = []
Ri = []
for i in range(m):
  l,r = map(int,input().split())
  Li.append(l)
  Ri.append(r)
  
LM = max(Li)
RM = min(Ri)

print(np.maximum(RM - LM +1, 0))

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

Limax = max(Li)
Rimin = min(Ri)

for j in range(1,n+1):
    if Limax<=j<=Rimin:
        count+=1
    else:
        pass
print(count)

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

Solution (Python):
def main():
    n, m = map(int, input().split())
    cards = [0] * (n + 1)
    for _ in range(m):
        l, r = map(int, input().split())
        cards[l - 1] += 1
        cards[r] -= 1
    x = 0
    res = 0
    for c in cards[:-1]:
        x += c
        if x == m:
            res += 1
    print(res)


if __name__ == "__main__":
    main()

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

keymax = 10**6
keymin = 0
for i in range(M):
	if X[i][0] > keymin:
		keymin = X[i][0]
	if X[i][1] < keymax:
		keymax = X[i][1]

if keymax >= keymin:
	print(keymax-keymin+1)
else:
	print("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)
z = min(y)-max(x)
if z < 0:
    print(0)
elif z+1 >= N:
    print(N)
else:
    print(z+1)

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

lower, upper = 1, n

for _ in range(m):
    a,b=map(int,input().split())
    lower=max(lower, a)
    upper=min(upper, b)

print(max(upper-lower+1, 0))

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

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

ll = l[0]
rr = r[0]

for i in range(1,m):
    if ll < l[i]:
        ll = l[i]
    if rr > r[i]:
        rr = r[i]

cnt = rr-ll+1
if cnt >= 1:
    print(cnt)
else:
    print(0)

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

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

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

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

Solution (Python):
n, m = [int(x) for x in input().split()]
values = [list(map(lambda x:int(x), input().split())) for x in range(m)]
max = -1
min = values[0][1]

for (left, right) in values:
    if(right < min):
        min = right
    if(left > max):
        max = left

if(max > min):
    print(0)
else:
    print(min - max + 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 = [int(x) for x in input().split(" ")]
L, R = [], []

for i in range(M):
    l, r = [int(x) for x in input().split(" ")]
    L.append(l - 1)
    R.append(r - 1)

L_max = max(L)
R_min = min(R)

count = R_min - L_max + 1 if R_min - L_max >= 0 else 0
print(count)

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

for i in range(m):
    r,l = map(int,input().split())
    if i==0:
        x = r
        y = l
    else:
        if r>x:
            x = r
        if l<y:
            y = l

if y>=x:
    print(y-x+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())

a = max(L)
b = min(R)

ans = max(0, b-a+1)
print(ans)

Solution (Python):
a, b = map(int, input().split())
ma = 0
mi = 10 ** 9
for i in range(b):
    c, d = map(int, input().split())
    ma = max(ma, c)
    mi = min(mi, d)
if ma > mi:
    print(0)
else:
    print(abs(ma - mi) + 1)

Solution (Python):
n, m = map(int, input().split())
lll = []
rrr = []
for _ in range(m):
  lr = list(map(int, input().split()))
  lll.append(lr[0])
  rrr.append(lr[1])
maxl = max(lll)
minr = min(rrr)
count = 0
for i in range(1,n+1):
  if maxl <= i <= minr:
    count += 1
print(count)

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

ls = []
rs = []
for i in range(m):
    lr = [int(_) for _ in input().split()]
    ls.append(lr[0])
    rs.append(lr[1])

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

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

Solution (Python):


import sys

hoge = input().split()
n=int(hoge[0])
m=int(hoge[1])

l=[]
r=[]

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

maxl=l[0]
minr=r[0]
for i in range(1,m):
	maxl=max(maxl,l[i])
	minr=min(minr,r[i])

minr=min(minr,n)
if(minr<maxl):
	print(0)
else:
	print(minr-maxl+1)

Solution (Python):


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

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

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

count = 0
for ai in arr:
  if ai == m:
    count += 1

print(count)

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

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

card2canopen_dict = {i: True for i in range(1, n+1)}


kukan_min, kukan_max = 1, n

for i, lr in enumerate(lr_list):
    l, r = lr[0], lr[1]
    if l >= kukan_min:
        kukan_min = l
    if r <= kukan_max:
        kukan_max = r

if kukan_max < kukan_min:
    ans = 0
else:
    ans = len(range(kukan_min, kukan_max+1))

print(ans)

Solution (Python):
from sys import stdin

N,M = map(int,stdin.readline().rstrip().split())
lList = []
rList = []
for i in range(M):
    l,r = map(int,stdin.readline().rstrip().split())
    lList.append(l)
    rList.append(r)
lMax = max(lList)
rMin = min(rList)
ans = rMin - lMax +1
if ans < 0:
    ans = 0
print(ans)

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

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

if L0 <= R0:
    print(R0-L0+1)
else:
    print(0)

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

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

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

x = min(R) - max(L)
if x>=0:
    print(x)
else:
    print(0)

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

LMAX = 1
RMIN = n

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

if LMAX <= RMIN:
    print(RMIN - LMAX + 1)
else:
    print(0)

Solution (Python):
def main():

    N,M=map(int,input().split())
    
    undivList=[]
    for _ in range(M):
        l=map(int,input().split())
        undivList +=l
    atmax=max(undivList[0::2])
    atmin=min(undivList[1::2])
    
    if atmax>atmin:
        print(0)
    else:
        print(abs(atmax-atmin)+1)



if __name__== "__main__":
    main()

Solution (Python):
N,M= map(int,input().split())
LRlis = [list(map(int, input().split())) for i in range(M)]
maxL = 0
minR = 10**5
for i in range(M):
  maxL = max(LRlis[i][0],maxL)
  minR = min(LRlis[i][1],minR)

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

Solution (Python):
N,M = [int(s) for s in input().split()]
l=1
r=N
for i in range(M):
  x,y = [int(s) for s in input().split()]
  l=max([x,l])
  r=min([y,r])
print(max([r-l+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)
if min(R) - max(L) + 1 >= 0:
    print(min(R) - max(L) + 1)
else:
    print(0)

Solution (Python):
a, b = map(int, input().split())
c = 0
d = 10 ** 9
for i in range(b):
    e, f = map(int, input().split())
    c = max(c, e)
    d = min(d, f)
print(d-c+1 if c <= d else 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()))

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

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

Solution (Python):
from copy import *
from sys import *
import math
import queue
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)


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

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Solution (Python):
n,m = map(int,input().split())
min_ = 0
max_ = 100001
for i in range(m):
  li = list(map(int,input().split()))
  
  if min(li) >= min_ :
    min_ = min(li)
  if max(li) <= max_:
    max_ = max(li)
if min_ > max_:
    print(0)
else:
    print(max_ - min_ + 1)

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

i=0
mini=0
maxi=0
while(i<m):
	if(i==0):
		mini,maxi=map(int,input().split())
			
	else:
		mint,maxt=map(int,input().split())
		mini=max(mini,mint)
		maxi=min(maxi,maxt)

	i+=1
t=maxi-mini+1
if(t<0):
	print(0)
else:
	print(t)

Solution (Python):
N,M = map(int,input().split())
l,r = map(int,input().split())
k = min(r,N)
for i in range(M-1):
    ll,rr = map(int,input().split())
    if ll>k or rr<l:
        print(0)
        exit()
    if ll >l : l = ll
    if rr < k : k = rr

print(k-l+1)

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

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

for _ in range(m-1):
  templ, tempr = map(int, input().split())
  l = max(l, templ)
  r = min(r, tempr)

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

Solution (Python):
import sys
from itertools import product

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

Lmax = 1
Rmin = N

for i in range(M):
    L,R = list(map(int, input().split()))
    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())
max_l = 0
min_r = 10 ** 9
for _ in range(M):
    l, r = map(int,input().split())
    if l > max_l:
        max_l = l
    if r < min_r:
        min_r = r
print(max(min_r-max_l+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)

left = -1
right = 1e9

for i in range(M):
    left = max(left, L[i])
    right = min(right, R[i])

ans = right - left + 1
ans = max(ans, 0)
print(ans)

Solution (Python):
n,m=map(int,input().split())
info=[]
for i in range(m):
    info.append(list(map(int,input().split())))
start,goal=info[0][0],info[0][1]
c=0

for i in range(1,m):
    start=max(start,info[i][0])
    goal=min(goal,info[i][1])
    if start>goal:
        c=1
        break
    
if c==0:
    print(goal-start+1)
else:
    print(0)

Solution (Python):
def main22():
    data = input().split()
    num = []
    for i in range(2):
        num.append(int(data[i]))
    gate = []
    for i in range(num[1]):
        karin = []
        karid = input().split()
        for j in range(2):
            karin.append(int(karid[j]))
        gate.append(karin)
    und = 1
    upp = num[0]
    for i in range(num[1]):
        if und < gate[i][0]:
            und = gate[i][0]
        if upp > gate[i][1]:
            upp = gate[i][1]
    result = upp - und + 1
    if result < 0:
        print(0)
    else:
        print(result)
        
if __name__ == "__main__":
    main22()

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

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

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 L <= R:
    ans = R - L + 1
else:
    ans = 0



print(ans)

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

max_L = L[0]
min_R = R[0]
for i in range(1,M):
    max_L = max(max_L, L[i])
    min_R = min(min_R, R[i])

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

Solution (Python):
N, M = map(int, input().split())
LL = []
RR = []
ans = 0

for i in range(M):
    L, R = list(map(int, input().split()))
    LL.append(L)
    RR.append(R)

ans = max(min(RR) - max(LL) + 1, 0)
print(ans)

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

Solution (Python):
N, M = map(int, input().split())
l = []
r = []
for _ in range(M):
  s, t = map(int, input().split())
  l.append(s)
  r.append(t)
  
lm = max(l)
rm = min(r)
print(max([rm - lm + 1, 0]))

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

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

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

Solution (Python):
N, M = map(int, input().split())
L = []
R = []
ans = 0

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

for i in range(N):
    if L_max <= i+1 <= L_min:
        ans += 1
print(ans)

Solution (Python):
n,m = map(int,input().split())
id = sorted([list(map(int,input().split())) for _ in range(m)])
maxl = id[0][0]
minr = id[0][1]
for i in range(1,len(id)):
    maxl = max([maxl, id[i][0]])
    minr = min([minr, id[i][1]])

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

Solution (Python):
n,m = map(int,input().split())
id = sorted([list(map(int,input().split())) for _ in range(m)])
maxl = id[0][0]
minr = id[0][1]
for i in range(1,len(id)):
    maxl = max([maxl, id[i][0]])
    minr = min([minr, id[i][1]])

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

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

gates = [list(map(int, input().split())) for _ in range(m)]
up = min([i[1] for i in gates])
down = max([i[0] for i in gates])


if up >= down:
    print(up-down+1)
else:
    print(0)

Solution (Python):
N, M = map(int, input().split())
(start, end) = (0, 100000)
for _ in range(M):
    s, e = map(int, input().split())
    start = max((start, s))
    end = min((end, e))
print(end - start + 1 if start <= end else 0)

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

Solution (Python):
n, m = map(int, input().split())
l, r = 1, n
for i in range(m):
    li, ri = map(int, input().split())
    if ri<l or r<li:
        print(0)
        exit()
    if l<=li and ri<=r:
        l, r = li, ri
    elif l<=ri<=r:
        r = ri
    elif l<=li<=r:
        l = li
    else:
        continue

print(r-l+1)

Solution (Python):
n, m = (int(i) for i in input().split())  
gate = []
for i in range(m):
  line =  [int(i) for i in input().split()] 
  gate.append(line)
L = []
R = []
for i in range(m):
  L.append(gate[i][0])
  R.append(gate[i][1])
R_min = min(R)
L_max = max(L)
n = R_min - L_max
if n < 0:
  print(0)
else:
  print(n+1)

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


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

Solution (Python):
import sys
import numpy as np


def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    seq = np.zeros(N, dtype=int)
    for _ in range(M):
        l, r = map(int, input().split())
        seq[l-1] += 1
        if r < N:
            seq[r] -= 1
    csum = np.cumsum(seq)
    ans = np.add.reduce(np.where(csum == M, 1, 0))
    print(ans)


if __name__ == '__main__':
    main()

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

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

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

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

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

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

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

Solution (Python):
N, M = list(map(int, input().split()))
l, r = 0, 100000
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:
    ans = 0
print(ans)

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

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

Solution (Python):






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