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 = input().split()
N,M = int(N),int(M)
L,R = [],[]

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

max_L = max(L)
min_R = min(R)
import numpy as np
L = np.array(L)
R = np.array(R)

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

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

Solution (Python):
N,M = map(int,input().split())
mat = [list(map(int,input().split())) for i in range(M)]
L = 0
R = 10**5+1
for i in range(M):
    if L < mat[i][0]:
        L = mat[i][0]
    if R > mat[i][1]:
        R = mat[i][1]
print(max([R-L+1,0]))

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    L=-INF; R=INF
    for _ in range(m):
        l,r=map(int,input().split())
        L=max(L,l)
        R=min(R,r)
    print(max(R-L+1,0))
resolve()

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

for i in range(1, N + 1):
    K[i] += K[i - 1]
print([i == M for i in K].count(True))

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

if x>=0:
  print(x)
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())
L_min = L[0]
R_max = R[0]
for i in range(1, M):
    if L_min < L[i]:
        L_min = L[i]
    if R[i] < R_max:
        R_max = R[i]
result = R_max-L_min+1
if result <= 0:
    print(0)
else:
    print(R_max-L_min+1)

Solution (Python):

import sys
input = lambda: sys.stdin.buffer.readline()[:-1].decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7


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





l, r = 0, 10**6
for i, j in data:
	l = max(l, i)
	r = min(r, j)
print(max(0, r-l+1))

Solution (Python):

import sys
input = lambda: sys.stdin.buffer.readline()[:-1]
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7


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





l, r = 0, 10**6
for i, j in data:
	l = max(l, i)
	r = min(r, j)
print(max(0, r-l+1))

Solution (Python):

import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7


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





l, r = 0, 10**6
for i, j in data:
	l = max(l, i)
	r = min(r, j)
print(max(0, r-l+1))

Solution (Python):
N,R = map(int,input().split())
min = 0
max = 10000000
ans = 0
for i in range(R):
  A,B = map(int,input().split())
  if A > min:
    min = A
  if B < max:
    max = B
if min < max:
  ans = max - min + 1
elif min == max:
  ans = 1
print(ans)

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

for j in range(m):
  num1 = max(num[j][0], num1)
  num2 = min(num[j][1], num2)

ans = num2 - num1 + 1

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

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

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 l > max_l:
    max_l = l
  if r < min_r:
    min_r = r
ans = min_r - max_l + 1
if ans < 0:
  ans = 0
print(ans)

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

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


def intersect(lhs, rhs):
    if lhs[1] < rhs[0] or rhs[1] < lhs[0]:
        return [0, 0]
    min_value = lhs[0] if lhs[0] > rhs[0] else rhs[0]
    max_value = lhs[1] if lhs[1] < rhs[1] else rhs[1]
    return [min_value, max_value]


ans = [-1, 2 * 10 ** 5]
for _ in range(m):
    ans = intersect(ans, list(map(int, input().split())))
    if 0 == ans[0] and 0 == ans[1]:
        print(0)
        exit(0)
print(ans[1] - ans[0] + 1)

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

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

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

max_L, min_R = (0, N)

for r in l:
    if r[0]>max_L:
        max_L = r[0]
    if r[1]<min_R:
        min_R = r[1]
if min_R >= max_L:
    print(min_R - max_L + 1)
else:
    print(0)

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):

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

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


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

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

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

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

_min = 1
_max = n

for d in data:
    if _min < d[0]:
        _min = d[0]
    if _max > d[1]:
        _max = d[1]

if _max >= _min:
    print(_max-_min+1)
else:
    print(0)

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

min_val_list = []
max_val_list = []
for i in range(M):
    min_id_range, max_id_range = map(int, input().split())
    min_val_list.append(min_id_range)
    max_val_list.append(max_id_range)

min_val_id = max(min_val_list)
max_val_id = min(max_val_list)


if min_val_id > max_val_id:
    print(0)
else:
    print(max_val_id - min_val_id + 1)

Solution (Python):

import sys
sys.setrecursionlimit(10000)
INF = 1<<32


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    
    lm = max(L)
    rm = min(R)

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



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

if __name__ == '__main__':
    main()

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

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

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

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

l = [list(map(int, input().split())) for _ in range(M)]
l = np.array(l)
ans = list(range(l[:, 0].max(), l[:, 1].min() + 1))
print(len(ans))

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

Nlist=[0 for i in range(N+2)]

for i in range(M):
    p=lr[i][0]
    q=lr[i][1]+1
    Nlist[p]+=1
    Nlist[q]-=1

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

num=Nlist.count(M)
if Nlist[N+1]==M:
    num-=1
print(num)

Solution (Python):

import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    L = max(L)
    R = min(R)

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

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)


if __name__ == "__main__":
    main()

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

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

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

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

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

L_list = []
R_list = []

for i 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)


print(len(range(L_max,R_min+1)))

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

n, m = map(int, input().split())
left, right = [None]*m, [None]*m

for i in range(m):
    a, b = map(int, input().split())
    left[i] = a
    right[i] = b

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

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

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

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

Solution (Python):
N,M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
L=list(map(lambda x: x[0], LR))
R=list(map(lambda x: x[1], LR))

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

Solution (Python):
n, m = (int(x) for x in input().split())
max_l = float('-inf')
min_r = float('inf')
for _ in range(m):
  l, r = (int(x) for x in input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
print(max(0, min_r - max_l+1))

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

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

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

    if l>r:
        print(0)
        exit()
    
    

print(r-l+1)

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

keylist = [0,1000000]

for i in range(m):
	if keylist[0] < list1[i][0]:
		keylist[0] = list1[i][0]
	if keylist[1] > list1[i][1]:
		keylist[1] = list1[i][1]
if keylist[0] > keylist[1]:
	print(0)
else:
	print(keylist[1]-keylist[0]+1)

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

for i in range(1,N+1):
    if(L_max<=i and i<=R_min):
        count+=1

print(count)

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

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

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

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

Solution (Python):
N, M = map(int, input().split())
low = -float('INF')
high = float('INF')
for i in range(M):
    l, r = map(int, input().split())
    if l > low:
        low = l
    if r < high:
        high = r
print(max(0, high - low + 1))

Solution (Python):
N, M = map(int, input().split())
L, R = [], []
low = -float('INF')
high = float('INF')
for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)
    if l > low:
        low = l
    if r < high:
        high = r
print(max(0, high - low + 1))

Solution (Python):
N, M = map(int, input().split())
min_c = 0
max_c = N
for i in range(M):
    L, R = map(int, input().split())
    min_c = max(min_c, L)
    max_c = min(max_c, R)

print(max(0, max_c - min_c + 1))

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

if max(L) > min(R):
    print(0)
else:
    print(int(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)
    R.append(r)

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

result = R_min - L_max + 1

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

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

inf = []
sup = []

for i in range(0,M):
	L,R = map(int,input().split())
	inf.append(L)
	sup.append(R)
	
inf = max(inf)
sup = min(sup)

ran = sup - inf

if ran >= 0:
	print(ran + 1)
else:print(0)

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

mi = 0
ma = 100001

for i in range(m):
	if lr[i][0] > mi:
		mi = lr[i][0]
	if lr[i][1] < ma:
		ma = lr[i][1]

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

Solution (Python):
n,m=map(int,input().split())
 
l=-1
r=10**6
 
for _ in range(m):
  il, ir=map(int,input().split())
  l=max(l,il)
  r=min(r,ir)
  
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)]
mi,mx=0,n
for item in l:
  mi1=item[0]
  mx1=item[1]
  if mi1>mi:
    mi=mi1
  if mx1<mx:
    mx=mx1
if mx<mi:
  print(0)
else:
  print(mx-mi+1)

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

Solution (Python):
n , m = map(int,input().split())
a , b = 1 , 100000

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

Solution (Python):
from sys import stdin

n, m = [int(i) for i in stdin.readline().rstrip().split()]
max_l = -1
min_r = 100000

for _ in range(m):
  l, r = [int(i) for i in stdin.readline().rstrip().split()]
  if l > max_l:
    max_l = l
  if r < min_r:
    min_r = r
    
if(min_r >= max_l):
  print(min_r - max_l + 1)
else:
  print(0)

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

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

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

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

print(card.count(m))

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

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

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

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N,M = map(int,readline().split())
LR = np.array(read().split(),np.int32)

L = LR[::2]; R = LR[1::2]

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

Solution (Python):


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

min = 1
max = N

for i in range(M):
    if L_R_list[i][0] >= min:
        min = L_R_list[i][0]
    if L_R_list[i][1] <= max:
        max = L_R_list[i][1]

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

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

l_max = max(l)
r_min = min(r)

for k in range(n+1):
    if l_max <= k <= r_min:
        cnt += 1
print(cnt)

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

l = 0
r = N+1
for i in range(M):
    l = max(l, L[i][0])
    r = min(r, L[i][1])
    
if r-l >= 0:
    print(r-l+1)
else:
    print(0)

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

l_L,r_L = [],[]

for val in range(m):
    l,r = map(int,input().split())

    l_L.append(l)
    r_L.append(r)

r = min(r_L)
l = max(l_L)

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

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

for llrr in lr:
  min2 = max(min2, llrr[0])
  max2 = min(max2, llrr[1])

if max2-min2 >= 0:
  print(max2-min2+1)
else:
  print(0)

Solution (Python):
import numpy as np
[n,m]=[int(x) for x in input().split()]
LR_T=np.array([[int(x)-1 for x in input().split()] for _ in range(m)]).T

print(max(np.min(LR_T[1])-np.max(LR_T[0])+1,0))

Solution (Python):
import sys

N, M = map(int,sys.stdin.readline().split())
L = 1
R = N

for _ in range(M):
  l, r = map(int, sys.stdin.readline().split())

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

Solution (Python):
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
min_n = 0
max_n = 10**5

for i in range(M):
    min_n = max(min_n, LR[i][0])
    max_n = min(max_n, LR[i][1])

print(max(max_n-min_n+1, 0))

Solution (Python):
N, M = map(int, input().split())
max_L = 0
min_R = 1000000
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 = min_R - max_L + 1
if ans >= 0:
    print(ans)
else:
    print(0)

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

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

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


main()

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


main()

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

L = []
R = []

for i in range(M):
    inp = input().split()
    L.append(int(inp[0]))
    R.append(int(inp[1]))
    
if max(L) <= min(R):
    print(min(R)-max(L)+1)
else:
    print(0)

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

Solution (Python):
from sys import stdin

n,m = [int(x) for x in stdin.readline().rstrip().split()]

li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(m)]


max_n = 10**7
min_n = -1*10**7

for i in range(m):
    if li[i][0] > max_n or li[i][1] < min_n:
        print(0)
        exit()
    else:
        max_n = min(max_n,li[i][1])
        min_n = max(min_n,li[i][0])

print(max_n - min_n + 1)

Solution (Python):
N, M = map(int, input().split())
L = []
R = []
for _ in range(M):
    inp = input().split()
    L.append(int(inp[0]))
    R.append(int(inp[1]))
L_max = max(L)
R_min = min(R)
print(max(0,min(R_min,N) + 1 - L_max))

Solution (Python):
N,M = map(int,input().split())
L,R = map(int,input().split())
for i in range(M-1):
  L_ans,R_ans = map(int,input().split())
  if L < L_ans:
    L = L_ans
  if R > R_ans:
    R = R_ans
print(len(list(range(L,R+1))))

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

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([min(r) - max(l) + 1, 0][min(r) - max(l) < 0])

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

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


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

print(ans)

Solution (Python):

n,m = map(int,input().split())
right = n
left = 0

for i in range(m):
    l,r = map(int,input().split())
    left = max(left,l)
    right = min(right,r)
if right< left:
    print(0)
else:
    print(right-left+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())
L=[-1]*M
R=[-1]*M
for i in range(M):
  L[i],R[i] =map(int,input().split())
if min(R) >= max(L):
  print(min(R)-max(L)+1)
else:
  print(0)

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

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

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

Solution (Python):
import heapq
n, m = map(int,input().split())
l = []
r = []
for i in range(m):
    line = list(map(int,input().split()))
    l.append(-line[0])
    r.append(line[1])
heapq.heapify(l)
heapq.heapify(r)
l_max = - heapq.heappop(l)
r_min = heapq.heappop(r)
if l_max > r_min:
    print(0)
else:
    print(r_min-l_max+1)

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

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

left = []
right = []
for i in range(m):
    a, b = map(int, input().split())
    left.append(a)
    right.append(b)

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

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

Solution (Python):
n, m  = (int(x) for x in input().split())
max_l = 0
min_r = 10000000
for i in range(0, m):
  a, b = (int(x) for x in input().split())
  if(max_l < a):
    max_l = a
  if(b < min_r):
    min_r = b

ret = min_r - max_l + 1
if ret < 0:
  ret = 0

print(ret)

Solution (Python):
N , M = map(int ,input().split())
left = 1
right = N
for i in range(M):
    tmp_left , tmp_right = map(int ,input().split())
    if left < tmp_left:
        left = tmp_left
    if right > tmp_right:
        right = tmp_right
ans = right - left + 1
if ans < 0:
    print("0")
else:
    print(right - left + 1)

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


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

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

for i in range(m):
	l=listg[i][0]
	r=listg[i][1]


	if l>l_max:
		l_max=l
	if r<r_min:
		r_min=r	

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

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

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

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

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

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

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

Solution (Python):
from collections import Counter

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

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

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

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

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

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


l = []
r = []
for i in range(m):
    l.append(lr[i][0])
    r.append(lr[i][1])


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

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

Solution (Python):
N, M = map(int, input().split())
List_LR = [list(map(int,input().split())) for i in range(M)]
L = [List_LR[i][0] for i in range(len(List_LR))]
R = [List_LR[i][1] for i in range(len(List_LR))]
 
L_max = max(L)
R_min = min(R)

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

Solution (Python):
N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
L = {li[i][0] for i in range(len(li)) }
R = {li[i][1] for i in range(len(li)) }


mL = max(L)
mR = min(R)

ans = mR - mL + 1
if ans <= 0: ans = 0
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_)

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

Solution (Python):
N,M=map(int,input().split())
a,b=1,N
for i in range(M):
    L,R=map(int,input().split())
    a,b=max(a,L),min(b,R)
print(max(b-a+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 max(L) > min(R):
  print(0)
else:
  print(min(R) - max(L) + 1)

Solution (Python):
from itertools import accumulate

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

A=list(accumulate(A))

ans=0
for a in A:
  if a==M:
    ans+=1
print(ans)

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

Solution (Python):
N,M=map(int,input().split())
lst=[[] for _ in range(M)]
dif=10**19
v1,v2=map(int,input().split())
for i in range(M-1):
  v_1,v_2=map(int,input().split())
  v1,v2=max(v1,v_1),min(v2,v_2)
if v2-v1+1<=0:
  print(0)
else:
  print(v2-v1+1)

Solution (Python):
N,M = map(int,input().split())
L =[]
R =[]
for _ in range(M):
    L_,R_ = map(int,input().split())
    L.append(L_)
    R.append(R_)
Lmax = 0
Rmin = 10**6
for i in range(M):
    Lmax = max(Lmax,L[i])
    Rmin = min(Rmin,R[i])
if Rmin-Lmax < 0:
    print(0)
    exit()
print(Rmin-Lmax+1)

Solution (Python):
import sys
from bisect import *
from collections import *
from copy import deepcopy
from heapq import *
from itertools import *
from math import *
from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N,M = map(int, input().split())
    left = 0
    right = N-1
    for _ in range(M):
        L,R = map(int, input().split())
        left = max(left,L-1)
        right = min(right,R-1)
        if right < left:
            print(0)
            return 0
    print(right-left+1)


if __name__ == '__main__':
    main()

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

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

Solution (Python):
n,m = (int(x) for x in input().split())
l,r = (int(x) for x in input().split())

for i in range(m - 1):
	a,b = (int (x) for x in input().split())
	if l > r:
		continue
	if l < a:
		l = a
	if r > b:
		r = b

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