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

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

count = 0

max_l = max(l)
min_r = min(r)

for i in range(1,n+1):
	
	if max_l <= i <= min_r:
		count += 1
			
print(count)

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

maxNum = 0
minNum = 0

for i in range(m):
    a, b = map(int, input().split())
    if maxNum == 0:
        maxNum = b
    else:
        if maxNum > b:
            maxNum = b

    if minNum == 0:
        minNum = a
    else:
        if minNum < a:
            minNum = a

if maxNum - minNum >= 0:
    print(maxNum - minNum + 1)
else:
    print(0)

Solution (Python):
N,M = list(map(int,input().split()))
L = 1
R = N+1
ans = N*2

for i in range(M):
    tL,tR = list(map(int,input().split()))
    if tR < L or R < tL:
        ans = 0
        break
    if tL > L:
        L = tL
    if tR < R:
        R = tR

print(min(ans,R-L+1))

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

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

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

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

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

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

Solution (Python):
N, M = map(int,input().split())
A = []
B = []
for i in range(M) :
    C, D = map(int,input().split())
    A.append(C)
    B.append(D)
if min(B)-max(A)+1 > 0 :
    print(min(B)-max(A)+1)
else :
    print(0)

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+1)
F=list(range(max(L),min(R)))
print(len(F))

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

ans = min(gol) -max(sta) 
print(ans+1 if ans >= 0 else 0)

Solution (Python):
N,M=input().split()
list01=[]
for i in range(int(M)):
	list01.append(input().split())
min_list=[]
max_list=[]
for i in range(int(M)):
	min_list.append(int(list01[i][0]))
	max_list.append(int(list01[i][1]))
min_num=max(min_list)
max_num=min(max_list)
list_N=list(range(1,int(N)+1))
score=0
for i in range(len(list_N)):
	if int(min_num)<=list_N[i]<=int(max_num):
		score+=1
print(score)

Solution (Python):
import numpy as np
N, M = map(int, input().split())
LL, RR = map(int, input().split())
for i in range(M-1):
    L, R = map(int, input().split())
    LL = max(L, LL)
    RR = min(R, RR)
print(max(RR-LL+1,0))

Solution (Python):
n,m=map(int,input().split())
card=[]
L=1
R=n
for i in range(m):
    card.append([int(i) for i in input().split()])
for j in card:
    if j[0]<=R:
        if L<=j[0]:
            L=j[0]
            
    else:
        print(0)
        quit()
    if L<=j[1]:
        if j[1]<=R:
            R=j[1]
            
    else:
        print(0)
        quit()
print(R-L+1)

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

gate_s = sorted(gate,key=lambda x:x[0])
min_r = 1000000
max_l = 0

for i in range(M):
  if gate_s[i][1] < min_r:
    min_r = gate_s[i][1]
  if gate_s[i][0] > max_l:
    max_l = gate_s[i][0]
if min_r-max_l < 0:
  print(0)
else:
  print(min_r-max_l+1)

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

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

Solution (Python):

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



left_max = 0
right_min = N


for i in range(M):

    tmp_left,tmp_right = map(int, input().split())


    if tmp_left > left_max:
        left_max = tmp_left

    if tmp_right < right_min:
        right_min = tmp_right



result = right_min - left_max

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

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

Solution (Python):


v = [int(d) for d in input().split()]

LM = []
for d in range(v[1]):
   LM.append([int(d) for d in input().split()])

L = [d[0] for d in LM]
M = [d[1] for d in LM]
L.sort()
M.sort()
if L[-1] <= M[0]:
    if M[0] <= v[0]:
        print(M[0]-L[-1]+1)
        exit()
    else:
        print(v[0]-L[-1]+1)
        exit()
else:
    print('0')

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

if M == 1:
    a, b = input().split(" ")
    print(int(b)-int(a)+1)
else:    
    a, b = input().split(" ")
    aM, bm = int(a),int(b)
    
    for m in range(int(M)-1):
        a, b = input().split(" ")
        aM = max(aM,int(a))
        bm = min(bm,int(b))
        
    if(bm<aM):
        print(0)
    else:
        print(int(bm)-int(aM)+1)

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

migi = []
hidari = []
for i,j in para_val:
    migi.append(i)
    hidari.append(j)

a = max(migi)
b = min(hidari)

if b - a >= 0 : print("{}".format(b-a+ 1))
else : print("0")

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

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

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

L = []
R = []
l = 0
r = 10 ** 6
for i in range(M):
    a,b = map(int, input().split())
    L.append(a)
    R.append(b)

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

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

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

mi, mx = map(int, input().split())
flag = False
for i in range(m-1):
    l, r = map(int, input().split())
    if mi < l <= mx:
        mi = l
    if mi <= r < mx:
        mx = r
    if mx < l or r < mi:
        flag = True
        break

if flag:
    print(0)
else:
    print(mx - mi + 1)

Solution (Python):
if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N = int(s[0])
    M = int(s[1])
    L = []
    R = []
    for i in range(M):
        s = input().rstrip().split(' ')
        L.append(int(s[0]))
        R.append(int(s[1]))

    res = list(range(1,N+1))
    res_min = 1
    res_max = N

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

    print(max(res_max-res_min+1,0))

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

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

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

MIN = [LR[i][0] for i in range(M)]
MAX = [LR[i][1] for i in range(M)]

if min(MAX) >= max(MIN):
    print(min(MAX) - max(MIN) + 1)
else:
    print(0)

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

Solution (Python):
N,M=map(int,input().split())
set1=set()
L,R=map(int,input().split())
for i in range(L,R+1):
    set1.add(i)
min=min(set1)
max=max(set1)
Flag=True
for i in range(M-1):
    L,R=map(int,input().split())
    if R < min:
        Flag=False
        break
    elif L > max:
        Flag=False
        break
    if L >= min and max >= L:
        min = L
    if R >= min and max >= R:
        max = R
if Flag:
    print(max-min+1)
else:
    print(0)

Solution (Python):
N,M = map(int,input().split())
maxL = 1
minR = 100000
for i in range(M):
    Li,Ri = map(int,input().split())
    maxL = max(maxL,Li)
    minR = min(minR,Ri)
        
if minR - maxL >= 0:
    print(minR-maxL+1)
else:
    print(0)

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

min_num = 1
max_num = n
for i in range(m):
  l,r = map(int, input().split())
  if min_num < l:
    min_num = l
  if max_num > r:
    max_num = r

if max_num-min_num >= 0:
  print (max_num-min_num + 1)
else:
  print(0)

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

Solution (Python):
N, M = map(int, input().split())
list = []
for i in range(M):
    L, R = map(int, input().split())
    list.append([L,R])
anslist = [list[0][0],list[0][1]]
arukadouka = True
cnt = 0
for i in range(1,M):
    if arukadouka == True:
        if anslist[0] > list[i][1]:
            arukadouka = False
        elif anslist[1] < list[i][0]:
            arukadouka = False
        elif list[i][0] == list[i][1]:
            anslist[0] = list[i][0]
            anslist[1] = list[i][1]
        else:
            if (anslist[0]<=list[i][0]) and (anslist[1]<=list[i][1]):
                anslist[0] = list[i][0]
            elif (anslist[0]<=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[0] = list[i][0]
                anslist[1] = list[i][1]
            elif (anslist[0]>=list[i][0]) and (anslist[1]<=list[i][1]):
                pass
            elif (anslist[0]>=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[1] = list[i][1]
        cnt += 1
if arukadouka == True:
    ans = anslist[1]-anslist[0]+1
else:
    ans = 0
print(ans)

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

for i in range(M):
    tmp = list(map(int, input().split()))
    if L<tmp[0]:
        L = tmp[0]
    if R>tmp[1]:
        R = tmp[1]

ans = [x for x in range(L, R+1)]

print(len(ans))

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

start, end = 1, n
for _ in range(m):
  l, r = map(int, input().split())
  if r < start or l > end:
    start, end = 1, 0
    break
  elif l <= start and start <= r <= end:
    end = r
  elif start <= l <= end and end <= r:
    start = l
  elif l <= start and r >= end:
    pass
  else:
    start, end = l, r

print(end - start + 1)

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

count = 0
minR = min(R)
maxL = max(L)
if minR < maxL:
  count = 0
else:
  for i in range(maxL, minR+1):
    if i <= N:
      count = count + 1
      
print('%d' % count)

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

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

for i in range(M):
    L, R = list(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 = []
R = []
for i in range(m):
    li,mi = map(int,input().split())
    L.append(li)
    R.append(mi)

Lmax = L[0]
Rmin = R[0]
for i in range(m - 1):
    if Lmax < L[i + 1]:
        Lmax = L[i + 1]
    if Rmin > R[i + 1]:
        Rmin = R[i + 1]

if Lmax > n or Lmax > Rmin:
    print(0)
elif Rmin > n:
    print(n - Lmax + 1)
else:
    print(Rmin - Lmax + 1)

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

L_list = []
R_list = []

for _ in range(M):
    L, R = map(int, input().split())
    L_list.append(L)
    R_list.append(R)

ans = min(R_list) - max(L_list) + 1

if ans < 0:
    ans = 0

print(ans)

Solution (Python):
N, M = map(int, input().split())
c = [tuple(map(int, input().split())) for _ in range(M)]
right = N
left = 1
for i in range(0, M):
    left = max(left, c[i][0])
    right = min(right, c[i][1])
print(len(range(left, right+1)))

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

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

Solution (Python):
N, M = map(int, input().split())
a1 = 0
b1 = N
for i in range(M):
    a, b = map(int, input().split())
    if a1 <= a:
        a1 =a
    if b1 >= b:
        b1 = b

ans = b1-a1+1
if ans <0:
    ans = 0
print(ans)

Solution (Python):
N,M=map(int,input().split())
L,H=map(int,input().split())
c=0
w=1
while c<M-1:
    l,h=map(int,input().split())
    if L<=l<=H:
        L=l
    elif l>H:
        w=0
        break
    if L<h<H:
        H=h
    c+=1
if w!=0:
    print(H-L+1)
else:
    print(0)

Solution (Python):
from collections import Counter as co
n,m=map(int,input().split())
a=[]
x=0
y=10**6
for i in range(m):
    l,r=map(int,input().split())
    if(l>x):
        x=l
    if(r<y):
        y=r
if(y>=x):
    print(y-x+1)
else:
    print(0)

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

s_list = list()
e_list = list()

for m in range(M):
    s, e = map(int, input().strip().split())
    s_list.append(s)
    e_list.append(e)

s_max = max(s_list)
e_min = min(e_list)

if s_max > e_min:
    print(0)
else:
    print(e_min-s_max+1)

Solution (Python):
id, gate=map(int,input().split())
s,f=0,id

for i in range(gate):
    l,r=map(int,input().split())
    s=max(s,l)
    f=min(f,r)

if s>f:
    print(0)
else:
    print(f-s+1)

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


left = lr[0][0]
right = lr[0][1]
for i in range(1,m):
    if left < lr[i][0]:
        left = lr[i][0]
    if right > lr[i][1]:
        right = lr[i][1]
    
    if left > right:
        break




if left > right:
    print(0)

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

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

N, M = int(N), int(M)

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

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

n_left = 1
n_right = n
result = 0

for i in range(m):
    l_i,r_i = [int(i) for i in input().split()]
    if(l_i > n_left):
        n_left = l_i
    if(r_i < n_right):
        n_right = r_i

if(n_left > n_right):
    print(0)
else:
    print(n_right - n_left + 1)

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

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

Solution (Python):
N, M = map(int, input().split())
minNum = 1
maxNum = N
for lr in range(M):
  l, r = map(int, input().split())
  if minNum <= l and r <= maxNum:
    minNum = l
    maxNum = r
  elif minNum <= l <= maxNum and maxNum <= r:
    minNum = l
  elif l <= minNum and minNum <= r <= maxNum:
    maxNum = r
  elif r < minNum or maxNum < l:
    print("0")
    exit()

print(maxNum - minNum +1)

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

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

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

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

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

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

print(max(max_gate - min_gate + 1, 0))

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

for gate in LR:
    L.append(gate[0])
    R.append(gate[1])

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

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)
	R.append(r)
	
lmax = max(L)
rmin = min(R)
ans = rmin - lmax + 1
if ans < 0:
	ans = 0

print(ans)

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

L_max = 0
R_min = 100000
flag = False
for i in range(M):
    L, R = map(int,input().split())
    if R_min >= L:        
        if L_max <= L:
            L_max = L
    
        if R_min >= R:
            R_min = R
    else:
        flag = True

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

Solution (Python):
n,m =list(map(int,input().split()))
lis = []
max_begin = 0
min_end = n
for i in range(m):
    begin,end =list(map(int,input().split()))
    
    if begin >= max_begin:
        max_begin = begin
    if end <= min_end:
        min_end = end

if min_end-max_begin >= 0:
    print(min_end-max_begin+1)
else:
    print(0)

Solution (Python):

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


gate = [[0,0]]*m
for i in range(m):
    gate[i]=raw_input().split()

cnt = 0
min_r = n
max_l = 0
for i in range(m):
    g0 = int(gate[i][0])
    g1 = int(gate[i][1])

    if max_l < g0:
        max_l = g0

    if min_r > g1:
        min_r = g1

for j in range(n):
    j=j+1
    if j >= max_l and min_r >= j:
            cnt += 1

print cnt

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

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

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

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

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

minimum = 0
maximum = 10**8
for _ in range(M):
    L, R = map(int, input().split())
    minimum = L if minimum < L else minimum
    maximum = R if R < maximum else maximum
maximum = N if N < maximum else maximum

print(max(min(maximum - minimum + 1, N), 0))

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

L_list = []
R_list = []
for i in range(M):
	L, R = input().split(' ')
	L_list.append(int(L))
	R_list.append(int(R))

L_max = max(L_list)
R_min = min(R_list)
if L_max <= R_min:
	print(len(range(L_max, R_min+1)))
else:
	print(0)

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

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

print(rb-lb+1 if lb <= rb else 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())

keys = [True] * N

l1 = 1
r1 = N

for i in range(M):
    l, r = map(int, input().split())
    if (l1 < l):
        l1 = l
    if (r < r1):
        r1 = r

if (r1 - l1 + 1 > 0):
    print(r1 - l1 + 1)
else:
    print(0)

Solution (Python):


import sys                                                      












def main():
    
    im = InputManager()
    
    n, m = im.inputMultiple()
    left = 0
    right = n
    for _ in range(m):
        l, r = im.inputMultiple()
        left = max(left, l - 1)
        right = min(right, r - 1)
    

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


class InputManager(object):
    def __init__(self):
        pass

    @staticmethod
    def inputSingle(dtype=int):
        return dtype(input())
    
    @staticmethod
    def inputMultiple(dtype=int, aslist=False):
        if aslist:
            return list(map(dtype, input().split()))
        else:
            return map(dtype, input().split())

    @staticmethod
    def input2D(h, dtype=int):
        return [list(map(dtype, input().split())) for i in range(h)]


main()

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

Lmax = 0
Rmin = N
for i in range(M):
    L, R = list(map(int, input().split()))
    Lmax = max(Lmax, L)
    Rmin = min(Rmin, R)

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

Solution (Python):
N,M = map(int,input().split())
ID_min = 1
ID_max = N
for i in range(M):
    L_tmp,R_tmp = map(int,input().split())
    if ID_max < L_tmp or ID_min > R_tmp:
        ID_max = 0
        ID_min = 1
        break
    if L_tmp > ID_min:
        ID_min = L_tmp
    if R_tmp < ID_max:
        ID_max = R_tmp
print(ID_max - ID_min + 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())

ans = list(range(max(L), min(R) + 1))


print(len(ans))

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

L =[]
R =[]

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

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

Solution (Python):



import numpy as np
s = list(map(int, input().split()))
N = s[0]
M = s[1]

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

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

Solution (Python):
n, m = map(int, input().split())
l, r = map(int, input().split())
ans = -1
if m > 1:
    for i in range(m-1):
        _l, _r = map(int, input().split())
        if (l < _l <= r):
            l = _l
        if (l <= _r < r):
            r = _r
        if _r < l or r < _l:
            ans = 0
            print(ans)
            break

if ans == -1:
    print(r-l+1)

Solution (Python):
data = [int(s) for s in  input().split()]
l = 0
r = 10000000
flag = 0

for i in range(data[1]):
    gate = [int(s) for s in input().split()]
    
    if l < gate[0]:
        l = gate[0]

    if l > r:
        print(0)
        flag = 1
        break
        

    if r > gate[1]:
        r = gate[1]
    if r < l:
        print(0)
        flag = 1
        break
if flag == 0:
    print(r-l +1)
else:
    pass

Solution (Python):
import sys

def parse(inp):
    n,m = map(int,next(inp).split())
    lr =[]
    for _ in range(m):
        lr.append(list(map(int,next(inp).split())))
    
    return n,m,lr
  
def main(inp):
    n,m,lr = inp
    
    b1 = 1
    b1 = ((b1 << (lr[0][1]-lr[0][0]+1)) - 1) << lr[0][0]-1 
    
    b2 = 1
    for i in range(1,m):
        b2 = ((b2 << (lr[i][1]-lr[i][0]+1)) - 1) << lr[i][0]-1
        b1 = b1 & b2
        b2 = 1
    
    mask = (1 << n) - 1
    b1 = b1 & mask
    return bin(b1).count("1")
  
print(main(parse(sys.stdin)))

Solution (Python):
import numpy as np

N, M = map(int, input().split())
ls = np.empty(M)
rs = np.empty(M)


for i in range(M):
    l, r = map(int, input().split())
    ls[i] = l
    rs[i] = r

if 0 <= (np.min(rs) - np.max(ls)):
    print(int(np.min(rs) - np.max(ls) + 1))
else:
    print(0)

Solution (Python):

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

Solution (Python):


N, M = map(int, raw_input().split())
_L = 1
_R = N

for i in range(M):
    L, R = map(int, raw_input().split())
    if _L < L:
        _L = L
    if R < _R:
        _R = R 

print str(max(_R-_L+1,0))

Solution (Python):
import sys
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
min_gate = 0
max_gate = sys.maxsize
for l, r in lr:
    min_gate = max(min_gate, l)
    max_gate = min(max_gate, r)
print(max(0, max_gate - min_gate + 1))

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

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

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

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

Solution (Python):

import sys



def solve(lines):
    N,M = map(int, lines.pop(0).split(' '))
    min_n = 0
    max_n = N
    for i in range(M):
        a,b = map(int, lines.pop(0).split(' '))
        min_n = max(a, min_n)
        max_n = min(b, max_n)
    print( max(0, max_n - min_n + 1 ) )

lines = [x.strip() for x in sys.stdin.readlines()]




solve(lines)

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)
l.sort()
r.sort()
tmp = range(l[-1], r[0] + 1)
se = set(tmp)
print(len(se))

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

s_ids = []
m_ids = []

for i in range(Q):
    s_ids.append(l[i][0])
    m_ids.append(l[i][1])

max_s_id = max(s_ids)
min_m_id = min(m_ids)




if max_s_id <= min_m_id:
    print(abs(max_s_id - min_m_id) +1)
else:
    print(0)

Solution (Python):
N,M = map(int,input().split())
L=[]
R=[]
Nlist = list(range(1,N+1))
for i in range(M):
  L_n,R_n = map(int,input().split())
  L.append(L_n)
  R.append(R_n)

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

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

answer = 0

upper = a
under = 0

for i in range(b):
  c,d=map(int,input().split())
  if d <= upper:
    upper = d
  if under <= c:
    under = c

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

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

Solution (Python):


N, M = [int(i) for i in input().split()]
LRs = [[int(j) for j in input().split()] for i in range(M)]
LRs = list(map(list, zip(*LRs)))  
Lmax = max(LRs[0])
Rmin = min(LRs[1])
Rmin = min(Rmin, N)
num = Rmin - Lmax + 1 if Rmin >= Lmax else 0 
print(num)

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

left = 1
right = N

for i in range(M):
    L, R = map(int, input().split())
    if L > left :
        left = L
    if R < right:
        right = R

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

Solution (Python):
def main():
    N,M = map(int, input().split())
    lr = []
    for _ in range(M):
        l,r = map(int, input().split())
        lr.append((l,r))
    lr1 = sorted(lr, key=lambda x: x[0])
    lr2 = sorted(lr, key=lambda x: x[1])
    res = lr2[0][1] - lr1[-1][0] + 1
    if res < 0:
        res = 0
    print(res)

if __name__ == '__main__':
    main()

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

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

    if R < maxR:
        maxR = R

if maxR-minL+1 > 0:
    print(maxR-minL+1)
else:
    print(0)

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

list_left = []
list_right = []
for i in range(m):
    l,r=map(int, input().split())
    list_left.append(l)
    list_right.append(r)
test=[i for i in range(max(list_left),min(list_right)+1)]
print(len(test))

Solution (Python):
from sys import stdin
N,M=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(M):
    data.append([int(x) for x in stdin.readline().rstrip().split()])
min_=0
max_=10**5

for j in data:
    if j[0] > min_:
        min_ = j[0]
    if j[1] < max_:
        max_ = j[1]
ans = max_ - min_
if ans < 0:
  print(0)
else:
  print(ans+1)

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

Solution (Python):
N, M = map(int,input().split())
li1 = []
li2 = []
for i in range(M):
    L, R = map(int,input().split())
    li1.append([L,R])
    li2.append([R,L])
li1.sort()
li2.sort(reverse=True)
if (li2[M-1][0])<(li1[M-1][0]):
    print("0")
    exit()
print((li2[M-1][0])-(li1[M-1][0])+1)

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

L, R = 1, N
for _ in range(M):
    Li, Ri = map(int, input().split())
    if L <= Li:
        L = Li
    if Ri <= R:
        R = Ri

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

Solution (Python):

import sys
def p(*a):
  s=" ".join(map(str,a))
  
  sys.stderr.write(s+"\n")


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)
	
	p("l,r,L,R",l,r,L,R)

ans = max(0, R-L+1)

print(ans)

p("l,r,L,R",l,r,L,R)
p(ans)

Solution (Python):



a, b = map(int, input().split())
mi, mx = map(int, input().split())
o = 0
for i in range(1, b):
    n, m = map(int, input().split())
    if n > mx:
        o = 1
        break
    else:
        if mi < n:
            mi = n
        if mx > m:
            mx = m
if o == 1:
    print(0)
else:
    print(mx-mi+1)

Solution (Python):
(N, M) = (int(i) for i in input().split(' '))
(l0,r0) = (1, N)

n = -1
for i in range(0,M):
    
    (l, r) = (int(j) for j in input().split(' '))
    
    if l > r0 or r < l0:
        n = 0
        break
    (l0,r0) = (max(l,l0), min(r,r0))

if n == -1:
    n = r0-l0 +1
    
print(n)

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

llist = []
rlist = []

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

if min(rlist) >= max(llist):
  print(min(rlist) - max(llist) + 1)
else:
  print(0)