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 = [int(_) for _ in input().split()]
LR = [[int(_) for _ in input().split()] for _ in range(M)]
lmax = 1
rmin = N
for l, r in LR:
    lmax = max(lmax, l)
    rmin = min(rmin, r)
print(max([0, rmin - lmax + 1]))

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

gates = [[int(v) for v in input().split()] for i in range(m)]

left = max(gates, key=lambda x: x[0])[0]
right = min(gates, key=lambda x: x[1])[1]
if left <= right:
  print(right - left + 1)
else:
  print(0)

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

l = []
r = []

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

a = min(r) - max(l) + 1

if a < 0:
    a = 0

print(a)

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

for x in range(M):
    L, R = map(int,input().split())
    Low = max(Low, L)
    High = min(High, R)

if High >= Low:
    answer = High - Low + 1
else:
    answer = 0
    
print(answer)

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

Solution (Python):
N,M=map(int,input().split())
list1=[list(map(int,input().split())) for i in range(M)]
start=[]
end=[]
for i in range(M):
    start.append(list1[i][0])
    end.append(list1[i][1])
stmax=max(start)

enmax=min(end)

if enmax>=stmax:
    print(enmax-stmax+1)
else:
    print(0)

Solution (Python):

n,m = map(int, input().split())
maxlist = []
minlist = []

for i in range(m):
	
	l , r = list(map(int, input().split()))
	minlist.append(l)
	if r > 0:
		maxlist.append(r)

minid = max(minlist)
maxid = min(maxlist)
okgate = maxid - minid + 1

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

Solution (Python):
N,M = [int(x) for x in input().split(' ')]
id = [1,N]
ans = -1
for i in range(M):
    L,R = [int(x) for x in input().split(' ')]
    if id[1] < L:
        ans = 0
        break
    elif id[0] > R:
        ans = 0
        break
    if id[0] < L:
        id[0] = L
    if id[1] > R:
        id[1] = R

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

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

def prison(n,m,id1,id2):
    if min(id2) >= max(id1):
        print(min(id2)+1-max(id1))
    else:
        print(0)


def main():
    n,m = [int(i) for i in input().split()] 
    l = []
    r = []
    for _ in range(m):
        l1, r1 = [int(i) for i in input().split()] 
        l.append(l1)
        r.append(r1)
    prison(n,m,l,r)
    

if __name__ == "__main__":
    main()

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

cards = [x for x in range(N)]

tmp = cards
Ls = []
Rs = []
for i in range(M):
    L,R = map(int,input().split())
    Ls.append(L - 1)
    Rs.append(R - 1)

print(max(min(Rs) - max(Ls) + 1,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())

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

Solution (Python):
def solve():
    maxCards, gates = map(int, input().split())
    gateRanges = [tuple(map(int, input().split())) for i in range(gates)]

    for i, permitRange in enumerate(gateRanges):
        if i == 0:
            activeBegin, activeEnd = permitRange
            continue
        begin, end = permitRange
        if not (begin <= activeEnd and end >= activeBegin):
            return print(0)
        else:
            activeBegin, activeEnd = max(activeBegin, begin), min(activeEnd, end)
    else:
        if (maxCards < activeBegin):
            return print(0)
        ans = min(activeEnd, maxCards) - activeBegin + 1
    print(ans)
solve()

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

max_l = 0
min_r = float('inf')

for l,r in LR:
    max_l = max(max_l,l)
    min_r = min(min_r,r)

if max_l <= min_r:
    print(min_r - max_l + 1)
else:
    print(0)

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

Solution (Python):


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

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

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

print(data.count(m))

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

Solution (Python):
N, M=map(int, input().split())
_min_list=[]
_max_list=[]
for i in range(M):
	_min,_max=map(int, input().split())
	_min_list.append(_min)
	_max_list.append(_max)

min_value=max(_min_list)
max_value=min(_max_list)

if min_value > max_value:
	print(0)
else:
	print(max_value-min_value+1)

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

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

id_l,id_r = max(L),min(R)
width = id_r - id_l + 1 if max(L)<=min(R) else 0

print(width)

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

listA=[]
while True:
    try:
        listA.append(list(map(int,input().split())))
    except:
        break;
L=[]
R=[]
for i in listA:
  L.append(i[0])
  R.append(i[1])
maxL = max(L)
minR = min(R)

count = 0
for i in range(1,N+1):
  if maxL <= i <= minR:
    count+=1
print(count)

Solution (Python):

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

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



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


print(card.count(m))

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

Solution (Python):
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
L=[]
R=[]
for i in range(m):
  L.append(l[i][0])
  R.append(l[i][1])
print(max(min(R)-max(L)+1,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())
mL = max(L)
mR = min(R)
ans = 0
for i in range(1,N+1):
    if ((mL <=i) and (i<=mR)):
        ans += 1
print(ans)

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

L, R = 1, N

for _ in range(M):
    a, b = list(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 = []
R = []
for i 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())
mis,mas=[],[]
for _ in range(m):
  tmp=list(map(int,input().split()))
  mis.append(tmp[0])
  mas.append(tmp[1])
ans=min(mas)-max(mis)+1
print(ans if ans>0 else 0)

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

maxtable1 = sorted(table, key = lambda x: x[0])
maxtable2 = sorted(table, key = lambda x: x[1])

print(max(0, maxtable2[0][1] - maxtable1[m-1][0] + 1))

Solution (Python):

N,M= map(int,input().split())
MIN=0
MAX=N
for i in range(M):
    L,R= map(int,input().split())
    MAX=min(MAX,R)
    MIN=max(MIN,L)
ans =MAX-MIN+1
print(max(0,ans))

Solution (Python):

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

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

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

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

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

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

L = 1
R = N
for LR in LR_list:
    if LR[0] > L:
        L = LR[0]
    if LR[1] < R:
        R = LR[1]
output = 0
if L <= R:
    output = R - L + 1
print (output)

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

Solution (Python):
N ,M = map(int , input().split())
max_L = []
min_R = []
gate_number = [i for i in range(1, M+1)]
for j in range(M):
    L , M = map(int,input().split())
    max_L.append(L)
    min_R.append(M)
print(len([s for s in range(max(max_L) , min(min_R) + 1)]))

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

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

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

Solution (Python):
N, M = [int(nm) for nm in input().split()]
L, R = [int(lr) for lr in input().split()]
LR = [L, R]
for m in range(M-1):
  L_2, R_2 = [int(lr) for lr in input().split()]
  if L_2 < LR[0]:
    if R_2 < LR[0]:
      print(0)
      break
    elif LR[0] <= R_2 <= LR[1]:
      LR = [LR[0], R_2]
    else: 
      continue
  elif LR[0] <= L_2 <= LR[1]:
    if LR[0] <= R_2 <= LR[1]:
      LR = [L_2, R_2]
    else: 
      LR = [L_2, LR[1]]
  elif LR[1] < L_2:
    print(0)
    break
else:
  card = LR[1] - LR[0] + 1
  print(card)

Solution (Python):
N, M = map(int, input().split())
gate_number = [i for i in range(1, M+1)]
all_L = []
all_R = []
all_gate_pass_IDs = []

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

print(len([i for i in range(max(all_L), min(all_R)+1)]))

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

l = 1
r = N
flag = False
for i in range(M):
    l_1, r_1 = list(map(int, input().split()))
    if l_1 > l:
        l = l_1
    if r_1 < r:
        r = r_1
    if l > r:
        flag = True
        break
    
if flag:
    print(0)
else:
    print(r - l + 1)

Solution (Python):

n,m=map(int,input().split())
L=[]
R=[]
for _ in range(m):
    a,b=map(int,input().split())
    L.append(a)
    R.append(b)
print(max(min(R)-max(L)+1,0))

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

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

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

Solution (Python):
N, M = map(int, input().split())
l = 0
r = N + 1
for _ in range(M):
  ll, rr = map(int, input().split())
  l = max(l, ll)
  r = min(r, rr)
print(max(0, r - l + 1))

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

l_max=max(l)
h_min=min(h)
if (h_min-l_max)<0:
    print(0)
else:
    print(h_min-l_max+1)

Solution (Python):


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

max_L, min_R = 0, N+1
for i in range(M):
    L, R = map(int, input().split())
    max_L = max(max_L, L)
    min_R = min(min_R, R)

ans = max(min_R - (max_L - 1), 0)

print(ans)

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

Solution (Python):
from itertools import accumulate
N,M=map(int,input().split())
L = [0]*(N+1)
for _ in range(M):
  l,r = map(lambda x:int(x)-1,input().split())
  L[l]+=1
  L[r+1]-=1

L = list(accumulate(L))
print(L.count(M))

Solution (Python):
n, m = map(int, input().split())
l = 0
r = n
for _ in range(m):
    nl, nr = map(int, input().split())
    l = max(nl, l)
    r = min(nr, r)

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

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

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


if __name__ == "__main__":
    main()

Solution (Python):
import sys
N,M = map(int,input().split())
L,R = sorted(map(int, input().split()))
range_min = L
range_max = R

def end_checker(L,R,range_min,range_max):
  if  L > range_max or R < range_min:
    return True
  return False

while M > 1:
  L,R = sorted(map(int, input().split()))
  if end_checker(L,R,range_min,range_max):
    print(0)
    sys.exit()
  
  range_min = L if L > range_min else range_min
  range_max = R if R < range_max else range_max
  M -= 1

ans = range_max - range_min + 1  
print(ans)

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

L = []
R = []

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

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

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

cards = np.zeros(n + 1)

for i in range(m):
    left, right = map(int, input().split())
    cards[left - 1] += 1
    cards[right] += -1
print(np.sum(cards.cumsum() >= m))

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

Solution (Python):
def solve(n, m, l, r):
    return max(0, min(r) - max(l) + 1)

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(solve(n, m, l, r))

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

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

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

lmax = -1
rmin = n * 100
for i in range(m):
    l, r = map(int, input().split())
    lmax = max(l, lmax)
    rmin = min(r, rmin)

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

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

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)
p = min(rr) - max(ll) + 1
if p < 0:
  print(0)
else:
  print(p)

Solution (Python):
n,m=map(int,input().split())
L=[0]*m
R=[0]*m
table=[0]*(n+2)
for i in range(m):
    L[i],R[i]=map(int,input().split())
    table[L[i]]+=1
    table[R[i]+1]-=1
s=[0]*(n+2)
for i in range(1,n+2):
    s[i]=s[i-1]+table[i]
print(s.count(m))

Solution (Python):
n,m=map(int,input().split())
L=[0]*m
R=[0]*m
table=[0]*(n+3)
for i in range(m):
    L[i],R[i]=map(int,input().split())
    table[L[i]]+=1
    table[R[i]+1]-=1
s=[0]*(n+3)
for i in range(1,n+3):
    s[i]=s[i-1]+table[i]
print(s.count(m))

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

Solution (Python):
def Technique_often_used():    
    
    
    
    pass
def naming_conventions():
    
    
    
    pass
def visual_studio_code_shortcut():    
    
    
    
    pass
from itertools import combinations, permutations 
from collections import deque, Counter 
from heapq import heapify, heappop, heappush, heappushpop, heapreplace 
from copy import deepcopy, copy 
from functools import reduce

from fractions import gcd 
def gcds(numbers):
    return reduce(gcd, numbers)
def lcm(x, y):
    return (x * y) // gcd(x, y)
def lcms(numbers): 
    return reduce(lcm, numbers, 1)


is_test = False
def debug_print(value,comments="",is_test=is_test):
    
    
    
    
    if not is_test:
        pass
    elif not comments and value is None:
        print("")
    elif not comments:
        print(value)
    else:
        print(comments,":",value)


if is_test:
    n,m = [10, 3]
    lr = [[3,6],[5,7],[6,9]]
else:        
    R = lambda: map(int, input().split())
    
    
    n,m = R()
    l,r = 0, 10**6
    for i in range(m):
        l_, r_ = R()
        l = max(l, l_)
        r = min(r, r_)

print(max(r-l+1,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],R[i]=l,r
ans=min(R)-max(L)+1
print(max(ans,0))

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

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

a_max = max(a)
b_min = min(b)

if b_min < a_max:
 print(0)
else:
 print(b_min - a_max + 1)

Solution (Python):

import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    diff = min(R) - max(L)
    if diff < 0:
        print(0)
    else:
        print(diff + 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())
l = []
r = []

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


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

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

for l,r in LR:
    left = max(left,l -1)
    right = min(right,r -1)

if left > right:
    print(0)
else:
    print( right - left +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)
 
Lmax = max(L_list)
Rmin = min(R_list)

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

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

Lmax = 0
Rmin = 10**5 + 1

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

    Lmax = max(Lmax,L)
    Rmin = min(Rmin,R)


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

Solution (Python):
n, m = map(int, input().split())
cnt = 0
id = []
for _ in range(m):
        l= list(map(int, input().split()))
        if len(id) == 0:
                id = l
        else:
                if id[0] < l[0]:
                        id[0] = l[0]
                elif id[1] > l[1]:
                        id[1] = l[1]
if id[1] < id[0]:
        cnt = 0
else:
        cnt = id[1]-id[0]+1
print(cnt)

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

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

Lmax, Rmin = 1, N
for i in range(M):
     L, R = [int(_) for _ in input().split()]
     Lmax = max(Lmax, L)
     Rmin = min(Rmin, R)

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

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

Solution (Python):
import sys

n,m = map(int, sys.stdin.readline().split())
less,more = 0,n

for i in range(m):
    l,r = map(int, sys.stdin.readline().split())
    less = max(less, l)
    more = min(more, r)

print max(0,1+more-less)

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

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

print(max(allR - allL + 1, 0))

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

N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
p = [0] * (N + 1)

for l, r in LR:
    p[l - 1] += 1
    p[r] -= 1
    
for i in range(N):
    p[i + 1] += p[i]
    
print(len([v for v in p if v == M]))

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


if __name__ == '__main__':
	main()

Solution (Python):
n, m = map(int, input().split())
gates = []
for i in range(m):
    gates.append(list(map(int, input().split())))
candidate = [1, n]
for gate in gates:
    if candidate[0] < gate[0]:
        candidate[0] = gate[0]
    if gate[1] < candidate[1]:
        candidate[1] = gate[1]
if (candidate[1] >= candidate[0]):
    print(candidate[1] - candidate[0] + 1)
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)

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

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

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

for m in range(M):
    L_in, R_in = list(map(int, input().split()))
    L.append(L_in)
    R.append(R_in)

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

Solution (Python):






import sys
input = sys.stdin.readline
N, M = map(int, input().split())





"""
4 2
1 3
2 4
"""





l_ = 0
r_ = N
for i in range(M):
    l, r = map(int, input().split())
    l_ = max(l_, l)
    r_ = min(r_, r)






ids = r_-l_+1
if ids < 0:
    print(0)
else:
    print(ids)

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

l = -1
r = n+1
for i in range(m):
  l = max(l,gate[i][0])
  r = min(r,gate[i][1])
print((r-l+1)*(l <= r))

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

Solution (Python):
card_gate = input().split()
card = int(card_gate[0])
gate = int(card_gate[1])
L_list = []
R_list = []

for i in range(0, gate):
    id_list = input().split()
    L_list.append(int(id_list[0]))
    R_list.append(int(id_list[1]))

print(len(range(max(L_list), min(R_list)+1)))

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

Solution (Python):
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(m)]
lr.sort()
r_min = 100000
l_max = 0
lr_min = 100000

for i in range(m):
    l_max = max(lr[i][0], l_max)
    r_min = min(lr[i][1], r_min)

lr_min = [r_min - l_max + 1 if l_max <= r_min else 0]
print(lr_min[0])

Solution (Python):
n, m = map(int,input().split())
arr = [1, n]
check = True
for i in range(m):
    l, r = map(int, input().split())
    if l <= arr[0] and r>= arr[1]:
        continue
    elif l > arr[1] or r < arr[0]:
        check = False
    if arr[0] <= l and l <= arr[1]:
        arr[0] = l
    if arr[1] >= r and r >= arr[0]:
        arr[1] = r
if check:
    print (arr[1] - arr[0] + 1)
else:
    print (0)

Solution (Python):





from sys import stdin
import itertools






temp = stdin.readline().split()
N = int(temp[0])
M = int(temp[1])

data = []
for i in range(M):
    write = stdin.readline().split()
    data.append(write)

mina = 0
maxa = 10e7
for num in data:
    if int(num[0]) > mina:
        mina = int(num[0])
    if int(num[1]) < maxa:
        maxa = int(num[1]) 

count = maxa-mina+1

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

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

a = np.max(LR_mat[:,0],axis=0)
b = np.min(LR_mat[:,1],axis=0)
num_ID = int(b-a+1)
if num_ID >= 0:
  print(num_ID)
else:
  print(0)

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

s=[]
l= 0
r = n
for i in range(m):
    ln,rn=map(int,input().split())
    if ln>l:
        l=ln
    if rn<r:
        r=rn
if r-l+1>0:
    print(r-l+1)
else:
    print(0)

Solution (Python):
import sys
n=0
m=0

n, m  = map(int, input().split())
_min, _max  = map(int, input().split())
for i in range(1, m):
  gate_min, gate_max = map(int, input().split())
  if (_max > gate_max):
    _max = gate_max
   
  if _min < gate_min:
    _min = gate_min
 
result_list = list(filter(lambda x : (_min <= x) and (x <= _max), range(1, n+1)))
result = len(result_list)

print(result)
sys.exit(0)

Solution (Python):
def larger(x, y):
	if x > y: return x
	return y
def smaller(x, y):
	if x < y: return x
	return y

N, M = map(int, raw_input().split())
L = 1
R = N
flag = True
for i in range(0, M):
	lft, rgt = map(int, raw_input().split())
	if R < lft or rgt < L:
		flag = False
		break
	L = larger(L, lft)
	R = smaller(R, rgt)
if flag: print R-L+1
else: print 0

Solution (Python):
nm = [int(i) for i in input().split()]
lr = [[int(i) for i in input().split()] for i in range(nm[1])]

l = [i[0] for i in lr]
r = [i[1] for i in lr]
print(len(range(max(l), min(r)+1)))

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


lmax = max(l)
rmin = min(r)

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

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

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

l, r = 0, 10 ** 6
for i in range(m):
    tl, tr = map(int, input().split())
    l = max(tl, l)
    r = min(tr, r)
print(len(range(l, r+1)))

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

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

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

lmin = 1
rmax = n
finish = False
for i in range(m):
    l, r = map(int, input().split())

    if l > lmin:
        lmin = l

    if r < rmax:
        rmax = r
    
    if rmax < lmin:
        finish = True
        

if finish:
    print(0)
else:
    print(rmax - lmin + 1)

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

def check(n, LR):
    return 1 if all(l <= n <=r for l, r in LR) else 0

L = 0
R = N
for _ in range(M):
    l, r = [int(x) for x in input().split()]
    if L < l:
        L = l
    if r < R:
        R = r

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

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

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

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

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    L = 0
    R = 10**5 + 1
    for _ in range(M):
        left, right = map(int, input().split())
        L = max(L, left)
        R = min(R, right)

    return max(R - L + 1, 0)


if __name__ == '__main__':
    print(main())

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

Solution (Python):
a=input().split()
N,M=int(a[0]),int(a[1])
L,R=[],[]
ans=0
o,p,q,O=[],[],[],[]
for i in range(0,M):
    a=input().split()
    L.append(int(a[0]))
    R.append(int(a[1]))
    a.clear()
num=len(L)

l=max(L)
r=min(R)+1

for i in range(l,r):
    ans=ans+1
print(ans)

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

print(max(res[1]-res[0]+1,0))

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

for i in LR:
    if i[0] > L:
        L = i[0]
    
    if i[1] < R:
        R = i[1]

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