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())
mini=100001
maxi=0
for i in range(M):
    l,r=map(int,input().split())
    if l>maxi:
        maxi=l
    if r<mini:
        mini=r
res=mini-maxi+1
if res>0:
    print(mini-maxi+1)
else:
    print(0)

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

gate = np.zeros(N+1)
for i in range(0, M):
    L, R = [int(j) for j in input().split()]
    gate[L-1] += 1
    gate[R] -=1

count = 0
for i in range(1, len(gate)):
    gate[i] = gate[i] + gate[i-1]
    if gate[i] == M:
        count += 1

if gate[0] == M:
    count += 1
print(count)

Solution (Python):
n,m=(int(i) for i in input().split())
list_a=[]
list_b=[]
for i in range(m):
  a,b=(int(i) for i in input().split())
  list_a.append(a)
  list_b.append(b)
  
max_a=max(list_a)
min_b=min(list_b)

if min_b-max_a+1>=0:
  print(min_b-max_a+1)
else:
  print(0)

Solution (Python):




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


mini, maxi = map(int, input().split())

for i in range(1, M):
    L, R = map(int, input().split())
    if L > mini:
        mini = L
    if R < maxi:
        maxi = R
    if mini > maxi:
        break

if mini > maxi:
    print(0)
else:
    print(maxi - mini + 1)

Solution (Python):


n, m = map(int, input().split())
l = []
r = []
answer = 0
for i in range(m):
    a, b = map(int, input().split())
    l.append(a)
    r.append(b)
min_ok = max(l)
max_ok = min(r)
answer = max_ok - min_ok + 1
if answer < 0:
    answer = 0
print(answer)

Solution (Python):
n,m=input().split()
n=int(n)
m=int(m)
i=0
x=list()
y=list()
ans=0
g=0
ll=[]
rr=[]


while i!=m:
    i=i+1
    l,r=input().split()
    l=int(l)
    r=int(r)
    ll.append(l)
    rr.append(r)
l=max(ll)
r=min(rr)

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

Solution (Python):


n, m = [int(x) for x in input().rstrip().split(" ")]

l_max = 1
r_min = n

for i in range(m):
    l, r = [int(x) for x in input().rstrip().split(" ")]
    if l > l_max:
        l_max = l
    if r_min > r:
        r_min = r

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

Solution (Python):

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

l_max = 1
r_min = n
for i in range(m):
    li, ri = map(int, input().split())
    if l_max < li:
        l_max = li
    if r_min > ri:
        r_min = ri

print(r_min-l_max+1 if r_min-l_max >= 0 else 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())

def prison(N,M,L,R):
  max_val=min(R)
  min_val=max(L)
  num=max_val-min_val+1
  if num<=0:
    num=0
  return(num)

print(prison(N,M,L,R))

Solution (Python):



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

l_min = 1
r_max = n+1

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

ans = r_max - l_min + 1
if ans > 0:
    print(ans)
else:
    print(0)

Solution (Python):
from sys import stdin

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

    _l = 0
    _r = n

    for i in range(m):
        l,r = [int(x) for x in stdin.readline().rstrip().split()]
        _l = max(l, _l)
        _r = min(r, _r)

    if _r - _l < 0:
        print(0)
    else:
        print(_r - _l + 1)
    
if __name__=='__main__':
    main()

Solution (Python):
import numpy as np
N,M = map(int,input().split())
min_num,max_num = 0,10**6
for i in range(M):
  n_i,n_j = map(int,input().split())
  min_num = n_i if n_i > min_num else min_num
  max_num = n_j if n_j < max_num else max_num

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

Solution (Python):










import numpy as np
import math
import copy

N,M=map(int, input().split())
A=np.array([i for i in range(N+1)[1::]])


minimum = 1
maximum = N

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

    if(R < minimum or maximum < L):
        print(0)
        exit()

    if(minimum < L):
        minimum = L
    if(R < maximum):
        maximum = R

print(maximum - minimum + 1)

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

gates = [[1, N]]

for m in range(M):
  L, R = map(int, input().split())
  updated = []
  for gate in gates:
    if L <= gate[0]:
      if gate[0] <= R:
        updated.append([gate[0], min(gate[1], R)])
    else:
      if L <= gate[1]:
        updated.append([L, min(gate[1], R)])
  gates = updated


print(sum([g[1] - g[0] + 1 for g in gates]))

Solution (Python):
n, m = map(int, input().split())
la, ra = map(int, input().split())
for i in range(1, m):
    lm, rm = map(int, input().split())
    overlap = False
    if lm <= la <= rm and lm <= ra <= rm:
        continue
    if la <= lm <= ra:
        la = lm
        overlap = True
    if la <= rm <= ra:
        ra = rm
        overlap = True
    if not overlap:
        print(0)
        exit(0)

print(ra - la + 1)

Solution (Python):
li = input().split()
n = int(li[0])
m = int(li[1])


l_r = [input().split() for i in range(m)]
l_r.sort(key=lambda x:int(x[0]))
l_max = int(l_r[-1][0])
l_r.sort(key=lambda x:int(x[1]))
r_min = int(l_r[0][1])
answer = 0



diff = r_min - l_max + 1
if diff > 0:
    print(diff)
else:
    print(0)

Solution (Python):


NM = [int(i) for i in input().split()]
LR = []
for i in range(NM[1]):
    LR.append([int(i) for i in input().split()])

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

'''
if ma > NM[0]:
    ma = NM[0]
if mi > NM[0]:
    mi = ma + 1
print(ma - mi + 1)
'''
if ma < mi:
    print(0)
elif NM[0] >= ma:
    print(ma-mi+1)
elif NM[0] < ma and NM[0] > mi:
    print(NM[0]-mi+1)
elif NM[0] <= mi:
    print(0)

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

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

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

    l = List[j+1][0]
    r = List[j+1][1]

    if l > left:
        left = l
    if r < right:
        right = r
    if right < left:
        print(0)
        break
else:
    print(right-left+1)

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)

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


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

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

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

Solution (Python):


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

left = gates[0][0]
right = gates[0][1]



for i in range(1, M):
    if right >= gates[i][0]: 
        left = max([left, gates[i][0]])
        right = min([right, gates[i][1]])
        
        
    else:
        print(0)
        sys.exit()
print(right - left + 1)

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

Solution (Python):
N, M = map(int, input().split())
maxL = 0
minR = N
for _ in range(M):
  L, R = map(int, input().split())
  maxL = max(maxL, L)
  minR = min(minR, R)

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

Solution (Python):
import sys
input = sys.stdin.readline
num = []
num = [int(i) for i in input().split()]

syou = []
dai = []

for temp in range(num[1]):
    suuzi = []
    suuzi = [int(i) for i in input().split()]
    syou.append(suuzi[0])
    dai.append(suuzi[1])

result= min(dai) - max(syou) + 1
if result > 0:
    print(result)
else:
    print(0)

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

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

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

gates = []
start, end = 1, n
for _ in range(m):
    L, R = map(int, input().split())
    if L > start:
        start = L
    if R < end:
        end = R

print(len(range(start, end+1)))

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

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

Lmax = 0
Rmin = N

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

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

Solution (Python):
from sys import stdin
import numpy as np

N, M = [int(x) for x in stdin.readline().rstrip().split()]


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

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

Solution (Python):


INT = list(map(int,input().split()))
N,M = INT[0],INT[1]



L_LIST = [0 for i in range(M)]
R_LIST = [0 for i in range(M)]
for i in range(M):
    LR_LIST = list(map(int,input().split()))
    L_LIST[i] = LR_LIST[0]
    R_LIST[i] =LR_LIST[1]

L_MAX = max(L_LIST)
R_MIN = min(R_LIST)

if R_MIN - L_MAX >= 0:
    print(R_MIN - L_MAX + 1)
    
else:
    print(int(0))

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

Lli = []
Rli =[]
for i in range(M):
    tmp = list(map(int,input().split()))
    Lli.append(tmp[0])
    Rli.append(tmp[1])

print(max(min(Rli)-max(Lli)+1,0))

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

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

L=0
R=N
for l,r in LR:
    if L<l:
        L=l
    if R>r:
        R=r
    

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

Solution (Python):
import numpy

N, M= map(int, input().split())
l = []
for _ in range(M):
  L, R = map(int, input().split())
  l.append([L, R])
a = numpy.array(l)

if (numpy.amin(a, axis=0)[1] - numpy.amax(a, axis=0)[0] + 1) < 0:
    print(0)
else:
    print(numpy.amin(a, axis=0)[1] - numpy.amax(a, axis=0)[0] + 1)

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

Solution (Python):
N, M = map(int, input().split())
maxL = 0
minM = N
for x in range(M):
    l, m = map(int, input().split())
    maxL = max(maxL, l)
    minM = min(minM, m)

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

Solution (Python):
def main():
    N, M = map(int, input().split())
    lr = [list(map(int, list(input().split()))) for i in range(M)]
    
    
    ll = []
    rl = []
    for line in lr:
        ll.append(line[0])
        rl.append(line[1])
    diff = min(rl) - max(ll)
    if diff < 0:
        print(0)
        return
    print(diff + 1)


if __name__ == '__main__':
    main()

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

Solution (Python):

import numpy as np
N, M = map(int, input().split())
l_max = 1
r_min = 100000
for i in range(M):
    l, r = map(int, input().split())
    if l > l_max:
        l_max = l
    if r < r_min:
        r_min = r
    if r_min < l_max:
        print(0)
        exit()
print(r_min - l_max + 1)

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

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

Solution (Python):
import collections
N, M = map(int, input().split())
S = [list(map(int, input().split())) for i in range(M)]
smax = max([s[0] for s in S])
smin = min([s[1] for s in S])
if smin - smax + 1 >= 0 :
    print(smin - smax + 1)
else :
    print(0)

Solution (Python):
N, M= (int(i) for i 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)

ans = set(range(L_max,R_min+1))

print(len(ans))

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

sum = 0
maxl = max(L)
minr = min(R)
for id in range(N):
    if maxl <= id and id <= minr:
        sum += 1

print(sum)

Solution (Python):
N, M=map(int, input().split())
LR=list(sorted([list(map(int, input().split())) for _ in range(M)]))
a=LR[-1][0]
LR=list(sorted(LR,  key=lambda x: x[1]))
b=LR[0][1]
print(b-a+1 if b>=a else 0)

Solution (Python):
card_num, gate_num = map(int, input().split())

ans_start, ans_end = map(int, input().split())
for _ in range(gate_num-1):
    s, e = map(int, input().split())
    if e < ans_start or ans_end < s:
        print(0)
        exit()
    elif s < ans_start and ans_start < e < ans_end:
        ans_end = e
    elif ans_start < s < ans_end and ans_end < e:
        ans_start = s
    elif ans_start == e:
        ans_end = e
    elif ans_end == s:
        ans_start = s

print(ans_end - ans_start + 1)

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

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

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

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

for i in range(M):
    L, R = map(int, input().split())
    if maxL < L:
        maxL = L
    if minR != None and R < minR:
        minR = R
    

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

Solution (Python):
import numpy as np
a=[]
a=input().split(" ")
N=int(a[0])
M=int(a[1])

G = []
for i in range(M):
    array = list(map(int, input().strip().split()))
    G.append(array)

b=[x[0] for x in G]
c=[x[1] for x in G]

B=max(b)
C=min(c)
y=C-B+1
if y>=0:
    print(y)
elif y<0:
    print('0')

Solution (Python):
N, M = map(int, input().split())
ming = 100001
maxg = 0
for i in range(M):
    l, r = map(int, input().split())
    ming = min(r, ming)
    maxg = max(l, maxg)

print(max(0, ming-maxg+1))

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

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

small, large = 0, 0

for _ in range(0, m):
  i = input()
  s = int(i.split()[0])
  l = int(i.split()[1])
  if small == 0:
    small, large = s, l
  else:
    if small < s:
      small = s
    if large > l:
      large = l

if small > large:
  print(0)
elif small > n:
  print(0)
elif small <= n:
  if large <= n:
    print(large - small + 1)
  elif large > n:
    print(n - small + 1)

Solution (Python):
from sys import stdin

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

maxl = 1
minr = n
for i in range(m):
  l, r = [int(x) for x in stdin.readline().rstrip().split()]
  maxl = max(maxl, l)
  minr = min(minr, r)
  
ans = minr - maxl + 1

if ans < 0:
  ans = 0

print(ans)

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

Solution (Python):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n, m = map(int, input().split())
a = [0] * (n + 2)
r = defaultdict(int)
for i in range(m):
    x,y = map(int,input().split())
    a[x - 1] += 1
    a[y] += -1

for i in range(n + 1):
    a[i + 1] += a[i]
count = 0
flg = True
for i in range(n):
    if a[i] >= m:
        count += 1
print(count)

Solution (Python):
n, m = map(int,input().split())
min = 1
max = n
for var in range(0, m):
  a, b= map(int, input().split())
  if min < a:
    min = a

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

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

x = [0] * M
y = [0] * M
for i in range(M):
    x[i], y[i] = map(int, input().split())

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

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

gate_cond = [[int(x) for x in input().split()] for _ in range(m)]
max_under_th = max([l[0] for l in gate_cond])
min_upper_th = min([l[1] for l in gate_cond])
if min_upper_th - max_under_th >= 0:
    print(min_upper_th - max_under_th + 1)
else:
    print(0)

Solution (Python):
n, m = map(int, input().split(" "))
min_l = 0
max_l = 10**5
for _ in range(m):
    l, r = map(int, input().split(" "))
    if min_l < l:
        min_l = l
    if r < max_l:
        max_l = r
if max_l < min_l:
    print(0)
else:
    print(max_l - min_l + 1)

Solution (Python):
N, M = map(int, input().split())
l_max = -1
r_min = N + 1
for m 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(r_min - l_max + 1, 0))

Solution (Python):


import sys
input = sys.stdin.readline

def io_generator():
	return input()

def iil(a_io):
	return map(int, a_io().split())





def main(io):
	rr,n =iil(io)
	lmax=-1
	rmin=rr+1
	for i in range(n):
		l,r=iil(io)
		lmax=max(lmax,l)
		rmin=min(rmin,r)
	
	return max(0, rmin-lmax+1)



if __name__ == "__main__":
	io= lambda : io_generator()
	ret = main(io)
	if ret is not None:
		print(ret)

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

N = inputs[0]
M = inputs[1]

start = 1
end = N

for i in range(M):
    inputs = list(map(int, input().split()))
    if start < inputs[0]:
        start = inputs[0]
    if inputs[1] < end:
        end = inputs[1]

if start <= end:
    print(end - start + 1)
else:
    print(0)

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

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

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

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

lown=[list[0][0]]
hin=[list[0][1]]


for i in range(1,g):
    if list[i][0]>lown[-1]:
        lown.append(list[i][0])
        
for i in range(1,g):
    if list[i][1]<hin[-1]:
        hin.append(list[i][1])
        
ans=[k for k in range(lown[-1],hin[-1]+1)]
print(len(ans))

Solution (Python):
import collections
import math 
from operator import itemgetter
N,a = map(int, input().split())
t =[]
dp = []
qp = []
for i in range(a):
    n,m = map(int, input().split())
    dp.append(n)
    qp.append(m)
m = max(dp)
M = min(qp)
if m > M:s =0
else: s = M-m+1

print(s)

Solution (Python):
N,M = map(int,input().split())
lmax,mmin = 0,1000000
for i in range(M):
    l,m = map(int,input().split())
    if lmax < l:
        lmax = l
    if mmin > m:
        mmin = m

if lmax > mmin:
    ans = 0
elif lmax == mmin:
    ans = 1
else:
    ans = mmin-lmax
    ans += 1
print(ans)

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

max = 0
min = 0

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

    if min <= L:
        min = L
    elif min == 0:
        min = L

    if max >= R:
        max = R
    elif max == 0:
        max = R

ans = max - min + 1

if ans < 0:
    ans = 0

print(ans)

Solution (Python):

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

L = []
R = []

ans = 0

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

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

for i in range(1,N+1):
    if Lmax <= i and i <= Rmin:
        ans += 1

print(ans)

Solution (Python):
nm = input().split()
minArr = []
maxArr = []

for m in range(int(nm[1])):
    tmp = input().split()
    minArr.append(int(tmp[0]))
    maxArr.append(int(tmp[1]))

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

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

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

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

a1 = max(L)
a2 = min(R)

ans = a2-a1+1

if ans<0 : ans = int(0)

print(int(ans))

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

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

if min(anss)-max(ans)+1 > 0:
  print(min(anss)-max(ans)+1)
if min(anss)-max(ans)+1 <= 0:
  print(0)

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

a=[]
b=[]
for i in range(M):

    X, Y= map(int, input().split())
    a.append(X)
    b.append(Y)
    
    if a[0]<a[i]:
      a[0]=a[i]
      
    if b[0]>b[i]:
      b[0]=b[i]

res=b[0]-a[0]+1

if res<0:
  res=0

print(res)

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

for i in range(m):
    l += [input().split()]
    if int(l[i][0]) > max:  
        max = int(l[i][0])
    if int(l[i][1]) < min:
        min = int(l[i][1])

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

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

Solution (Python):



import re

def main():
    ID_gate = re.split(" +", input())
    ID = ID_gate[0]
    gate = ID_gate[1]
    min = 0
    max = 1000000
    for n in range(int(gate)):
        l_r = re.split(" +", input())
        if min < int(l_r[0]):
            min = int(l_r[0])
        if int(l_r[1]) < max:
            max = int(l_r[1])
    if max - min + 1 < 0:
        print(0)
    else:
        print(max - min + 1)

if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin, exit

N, G = map(int, stdin.readline().split())
left, right = 1, N
for i in range(G):
    l, r = map(int, stdin.readline().split())
    if l > left:
        left = l
    if r < right:
        right = r
    if right < left:
        print(0)
        exit()

print(right - left + 1)

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

Solution (Python):

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

Solution (Python):
import numpy as np
n,m=map(int,input().split())
L=[list(map(int,input().split())) for i in range(m)]
list = np.asarray(L)

MAX=np.max(list,axis=0)
MIN=np.min(list,axis=0)

ans = MIN[1]-MAX[0]+1
if ans>0:
  print(MIN[1]-MAX[0]+1)
else:
  print(0)

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

    ans = f(N, M, lr)
    
    
    print(ans)


def WA(N, M, lr):
    import numpy as np
    a = np.zeros(N, dtype=int)
    for L, R in lr:
        a[L-1:R] += 1
    ans = (a == M).sum()
    return ans


def f(N, M, lr):
    l_max = max(L for L, _ in lr)
    r_min = min(R for _, R in lr)

    ans = r_min - l_max + 1
    ans = max(ans, 0)
    return ans


def TLE(N, M, lr):
    ans = 0
    for i in range(1, N+1):
        g = 0
        for L, R in lr:
            if L <= i <= R:
                g += 1
        if g == M:
            ans += 1

    return ans


if __name__ == '__main__':
    main()

Solution (Python):
n,m=map(int,input().split())
x = []
y = []
for i in range(m):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

mn=min(y)
mx=max(x)

ans=max(mn-mx+1,0)
print(ans)

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):
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.split() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N, M = makeMultiInteger(input_string[0])
    S = makeIntMatrix(input_string[1:])
    c = 0
    ra = range(1, N+1)
        
    min_range = ra
    min_l = ra[0]
    min_r = ra[-1]
    for s in S:
        l = s[0]
        r = s[1]
        if l > min_range[0]:
            min_l = l
        if r < min_range[-1]:
            min_r = r
        min_range = range(min_l, min_r+1)
        if len(min_range) == 0:
            return 0
    
    return len(min_range)
    
    



def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
n, m = map(int, input().split())
arr_l = []
arr_r = []
for i 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 = 1
R = N
for i in range(M):
    l, r = map(int, input().split())
    if L < l:
        L = l
    if R > r:
        R = r

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

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

left_max = max(lr_mat[:, 0])
right_min = min(lr_mat[:, 1])

cnt = 0
for i in range(1, n+1):
    if(left_max <= i) and (i <= right_min):
        cnt += 1

print(cnt)

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

max_Li = -1
min_Ri = 10000000
for i in range(M):
    L_i, R_i = map(int, input().split())
    max_Li = max(max_Li, L_i)
    min_Ri = min(min_Ri, R_i)


print(max(0, min_Ri - max_Li + 1))

Solution (Python):

n, m = map(int, input().split())
l, r = map(int, input().split())
for i in range(m - 1):
    tmp_l, tmp_r = map(int, input().split())
    if l < tmp_l:
        l = tmp_l
    if tmp_r < r:
        r = tmp_r

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

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

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

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

Solution (Python):
import sys
args = sys.stdin.readlines()
n, m = map(int, args[0].split(' '))
l_1 = 1
r_1 = n

for gate in args[1:]:
  l, r = map(int, gate.split(' '))  
  if l > l_1:
    l_1 = l
  if r < r_1:
    r_1 = r

ids = r_1 - l_1 + 1
if ids < 0:
  print(0)
else:
  print(ids)

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

l, r = 1, 10**5
for i in range(M):
    l_, r_ = [int(n) for n 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(' '))
l_ = []
r_ = []
for i in range(m):
    l,r = map(int, input().split(' '))
    l_ += [l]
    r_ += [r]
l = max(l_)
r = min(r_)
if n < l:
    print(0)
elif n < r:
    r = n
    print(r - l + 1)
elif l > r:
    print(0)
elif l == r:
    print(1)
else:
    print(r - l + 1)

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

l = 1
r = n



for list in lr:
    if list[0] > l:
        l = list[0]
    if list[1] < r:
        r = list[1]


result = r-l+1
if l > r:
    result = 0
elif l == r:
    result = 1

print(result)

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

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

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

ans = 0
for i in range(n):
    if imos[i] == m:
        ans += 1

print(ans)

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

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

maxL=0
minR=N

for i in range(M):
    L, R = map(int, input().split(" "))
    
    if minR < L or R < maxL:
        print("0")
        exit()
    
    maxL=max(maxL, L)
    minR=min(minR, R)


print(minR-maxL+1)

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

Solution (Python):
from collections import Counter
n, m = list(map(int, input().split()))
ll = []
x = 0
y = 10 ** 5

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

s = (y - x) + 1
if s < 0:
    s = 0
print(s)