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 = list(map(int, input().split()))

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

L = []
R = []

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

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

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

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

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

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

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

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

max_l = -1*float('INF')
min_r = float('INF')
for i in range(m):
    max_l = max(max_l,l[i][0])
    min_r = min(min_r,l[i][1])

ans = min_r - max_l + 1

if(ans < 0):
    print(0)
    exit()

if(ans <= n):
    print(ans)
else:
    print(n)

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

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

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

Solution (Python):
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N,M = LI()
left = 0
right = 10000000

for _ in range(M):
    l,r = LI()
    left = max(left,l)
    right = min(right,r)

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

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

L, R = 1, N

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

    if(r < L or R < l):
        print(0)
        exit()
    else:
        L = max(l,L)
        R = min(r,R)

print(R-L+1)

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

ans_1=max(l_list)
ans_2=min(r_list)
print(max(ans_2-ans_1+1,0))

Solution (Python):

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

MAX = N + 2
array = [0] * (MAX)

for l, r in X:
    array[l] += 1
    array[r + 1] -= 1

for i in range(MAX - 1):
    array[i + 1] += array[i]

print(sum(v == M for v in array))

Solution (Python):
import sys
N, M = map(int, input().split())
ansl = 1
ansr = N
for _ in range(M):
    l, r = map(int, sys.stdin.readline().split())
    ansl = max(ansl, l)
    ansr = min(ansr, r)
ans = max(0, ansr - ansl + 1)
print(ans)

Solution (Python):

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

for i in range(m):
    imos[l[i]-1] += 1
    imos[r[i]] -= 1

cum_sum = [imos[0]]
for i in range(1, len(imos)):
    cum_sum.append(cum_sum[i - 1] + imos[i])

ans = 0
for count in cum_sum:
    if count == m: ans += 1
print(ans)

Solution (Python):
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n, m = MI()
    l, r = MI()
    for _ in range(m-1):
        li, ri = MI()
        l = max(l, li)
        r = min(r, ri)
    ans = max(0, r-l+1)
    print(ans)

if __name__ == '__main__':
    main()

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

left = 1
right = n+1

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

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

Solution (Python):
n, m = map(int, input().split())
cards = [0] * n
for i in range(m):
    l, r = map(lambda x: int(x)-1, input().split())
    cards[l] += 1
    if r+1 < n:
        cards[r+1] -= 1

from itertools import accumulate
ac = list(accumulate(cards))

ans = 0
for a in ac:
    if a == m:
        ans += 1
print(ans)

Solution (Python):
n,m,*a=map(int,open(0).read().split())
b=min(a[1::2])-max(a[::2])+1
print(b if b>0 else 0)

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

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

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

Solution (Python):





























































global FLAG_LOG
FLAG_LOG = False


def log(value):
    
    FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    
    
    N, M = list(map(int, lines[0].split()))
    
    
    
    
    
    valueses = list()
    for i in range(M):
        valueses.append(list(map(int, lines[i+1].split())))

    ll = None
    rr = None

    log(f'valueses=[{valueses}]')

    for i in range(M):
        l = valueses[i][0]
        r = valueses[i][1]
        if ll is None:
            ll = l
        elif ll < l:
            ll = l
        else:
            log(f'll=[{l}], l=[{l}]')
        if rr is None:
            rr = r
        elif rr > r:
            rr = r
        else:
            log(f'rr=[{rr}], r=[{r}]')

    log(f'l=[{l}]')
    log(f'r=[{r}]')

    return [max(rr-ll+1, 0)]



def get_input_lines():
    line = input()
    N, M = list(map(int, line.split()))
    lines = list()
    lines.append(line)
    for _ in range(M):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['4 2', '1 3', '2 4']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['10 3','3 6', '5 7', '6 9']
        lines_export = [1]
    if pattern == 3:
        lines_input = ['100000 1', '1 100000']
        lines_export = [100000]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines()
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

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

Solution (Python):
N, M = map(int,input().split())
a = []
b = []
for i in range(M):
    L, R = map(int,input().split())
    a.append(L)
    b.append(R)
a = sorted(a)
b = sorted(b)
A = a[M - 1]
B = b[0]
if A <= B:
    print(B - A + 1)
else:
    print(0)

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

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

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

Solution (Python):
n,m=map(int,input().split())
l=[]
r=[]
for j in range(m):
    templ,tempr=map(int,input().split())
    l.append(templ)
    r.append(tempr)
ans=min(r)-max(l)+1
if ans<0:
    ans=0
print(ans)

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

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

lefts.sort()
rights.sort()

if lefts[-1] > rights[0]:
    print(0)
else:
    print(rights[0] - lefts[-1] + 1)

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

min_l = 0
max_l = 10**6

for _ in range(m):
    L, R = map(int, input().split())
    min_l = max(min_l, L)
    max_l = min(max_l, R)

print(max_l - min_l + 1 if max_l - min_l + 1 > 0 else 0)

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

min = lst[0][0]
max = lst[0][1]
for v in lst:
    if v[0] > min:
        min = v[0]
    if v[1] < max:
        max = v[1]
if min <= max:
    print(max-min + 1)
elif min > max:
    print(0)

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

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

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

l_max = 1
r_min = N
for _ in range(M):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)

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

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

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

Solution (Python):
N,M = [int(i) for i in input().split()]
c = [1, N]
for _ in range(M):
    a,b = [int(i) for i in input().split()]
    a,b = min(a,b), max(a,b)
    if c[0] <= a <= b <= c[1]:
        c = [a,b]
    elif a < c[0] <= b < c[1]:
        c = [c[0], b]
    elif c[0] < a <= c[1] < b:
        c = [a, c[1]]
    elif a <= b < c[0]:
        print(0)
        break
    elif c[1] < a <= b:
        print(0)
        break
else:
    print(c[1] - c[0] + 1)

Solution (Python):
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, M = rl()
LR = []
for i in range(M):
	LR.append(rl())
cnts = [0]*(N+1)
for l, r in LR:
	cnts[l-1] += 1
	cnts[r] -= 1
ans = 0
s = 0
for i in range(N):
	s += cnts[i]
	if s == M:
		ans += 1
print(ans)

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

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

Solution (Python):

def input2():
	return map(int,input().split())

N,M=input2()
L,R=[],[]
for i in range(M):
	l,r=input2()
	L.append(l)
	R.append(r)

MIN=0
MAX=N

for j in range(M):
	if L[j]>MIN:
		MIN=L[j]
	if R[j]<MAX:
		MAX=R[j]

if MAX<MIN:
	print(0)
else:
	print((MAX-MIN)+1)

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

Solution (Python):
n, m = map(int, input().split())
imos = [0] * (n + 1)
for _ in range(m):
    l, r = map(int, input().split())
    imos[l-1] += 1
    imos[r] -= 1
cnt = 1 if imos[0] == m else 0
for i in range(1, n):
    imos[i] += imos[i-1]
    if imos[i] == m:
        cnt += 1
print(cnt)

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

Solution (Python):
N,M = map(int,input().split())
right = N
left = 1
for _ in range(M):
  a,b = map(int,input().split())
  right = min(right,b) 
  left = max(left,a)

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

Solution (Python):
def readinput():
    n,m=map(int,input().split())
    l=[]
    r=[]
    for i in range(m):
        li,ri=map(int,input().split())
        l.append(li)
        r.append(ri)
    return n,m,l,r

def main(n,m,l,r):
    lmax=1
    rmin=n
    for i in range(m):
        lmax=max(lmax,l[i])
        rmin=min(rmin,r[i])
    return max(rmin-lmax+1,0)

if __name__=='__main__':
    n,m,l,r=readinput()
    ans=main(n,m,l,r)
    print(ans)

Solution (Python):
n,m = map(int,input().split())
l = [list(map(int,input().split())) for _ in range(m)] 
l1=[]
l2=[]
for i in l:
  l1.append(i[0])
  l2.append(i[1])
a=max(l1)
b=min(l2)
if b-a>=0:
  print(b-a+1)
else:
  print("0")

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

Solution (Python):

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


cards = np.zeros(n+1, dtype=int)

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

cum = cards.cumsum()

print(len(cum[cum==m]))

Solution (Python):
import numpy as np
N,M=map(int,input().strip().split())
LR=np.array([list(map(int,input().strip().split())) for _ in range(M)])
print(max(min(LR[:,1])-max(LR[:,0])+1,0))

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

max_l = 0
min_r = 100000
for _ in range(M):
  l, r = map(int, input().split())
  if max_l < l:
    max_l = l
  if min_r > r:
    min_r = r

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

Solution (Python):
import numpy as np
N,M=map(int,input().strip().split())
LR=np.array([list(map(int,input().strip().split())) for _ in range(M)])
print(max(min(LR[:,1])-max(LR[:,0])+1,0))

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

Solution (Python):
k, g = map(int, input().split())
id = [map(int, input().split()) for i in range(g)]
l, r = list(zip(*id))
print(max(min(r) - max(l) + 1, 0))

Solution (Python):
k, g = list(map(int, input().split()))
id = [list(map(int, input().split())) for i in range(g)]
l, r = list(zip(*id))
print(min(r) - max(l) + 1) if min(r) - max(l) >= 0 else print(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)
s=0
t=n+1
for i in range(m):
    if l[i]>s:
        s=l[i]
    if r[i]<t:
        t=r[i]
if s>t:
    print(0)
else:
    print(t-s+1)

Solution (Python):
N, M = input().split(' ')
N = int(N)
M = int(M)
min = 1
max = N
for i in range(M):
  l, m = input().split(' ')
  l = int(l)
  m = int(m)
  if min>m or max<l:
    print(0)
    break 
  if min < l:
    min = l
  if max > m:
    max = m
else:
  print(max-min+1)

Solution (Python):
n,m=map(int,input().split())
a=[]
for x in range(m):
  s=list(map(int,input().split()))
  a.append(s)
  
min1=1
max1=n

for y in range(m):
  l=a[y][0]
  r=a[y][1]
  if min1<l:
    min1=l
  if max1>r:
    max1=r
    
if max1-min1>=0:
  print(max1-min1+1)
  
else:
  print(0)

Solution (Python):
n,m = map(int,input().split())
l = 0
r = n+2
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):
import sys
import math
import itertools


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, M = map(int, input().split())
    L = 1
    R = 10 ** 5 + 1

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

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


if __name__ == "__main__":
    main()

Solution (Python):
n,m = map(int,input().split())
lr= [list(map(int,input().split()))for a in range(m)]
l,r = [list(b) for b in zip(*lr)]
left,right = max(l),min(r)
cnt = 0
for i in range(1,n+1):
	if left <= i and i <= right:
		cnt += 1
print(cnt)

Solution (Python):
n,m=map(int,input().split())
p=[0]*(n+1)
for _ in range(m):
  l,r=map(int,input().split())
  p[l-1]+=1
  p[r]-=1
for i in range(n):
  p[i+1]+=p[i]
print(p.count(m))

Solution (Python):

N,M = map(int,input().split())
li = []
ri = []
num = 0


for i in range(M):
    L,R = map(int,input().split())
    li.append(L)
    ri.append(R)
L = max(li)
R = min(ri)
num = R-L+1
if num <0:
    num= 0
    
print(num)

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

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

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

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

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

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

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

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

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

Solution (Python):

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

L = [0] * M
R = [0] * M


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

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

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

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

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

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

L_max=0
R_min=10**6
for i in range(m):
    if gate[i][0]>L_max:
        L_max=gate[i][0]

for i in range(m):
    if gate[i][1]<R_min:
        R_min=gate[i][1]


ans=0

for i in range(1, n+1):
    if L_max<=i<=R_min:
        ans+=1

print(ans)

Solution (Python):







__giturl__ = "$URL$"


from sys import stdin
input = stdin.readline


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

  min_ = -float('inf')
  max_ = float('inf')
  for _ in range(M):
    L, R = map(int, input().split())
    min_ = max(min_, L)
    max_ = min(max_, R)

  ans = 0
  for i in range(1, N+1):
    (max_ - min_ + 1)
    if min_<=i<=max_:
      ans += 1
  print(ans)


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

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

x = max(l)
y = min(r)

p = 0
for i in range(1,n+1):
    if x <= i and i <= y:
        p += 1

print(p)

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

lower = 0
upper = N+1
for _ in range(M):
    L, R = map(int, input().split())
    lower = max(lower, L)
    upper = min(upper, R+1)
c = upper - lower
if c <= 0:
    print(0)
else:
    print(c)

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



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

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

Solution (Python):


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

ids = list(range(1, N+1))

L_max = 0
R_min = 10000000000000000000
for _ in range(M):
    (L, R) = map(int, input().split(" "))
    if L > L_max:
        L_max = L
    if R < R_min:
        R_min = R

ids = [i for i in ids if L_max <= i <= R_min]
print(len(ids))

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

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

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

Solution (Python):
N,M =list(map(int,input().split()))
la=1
rm=N
for i in range(M):
  a,b=list(map(int,input().split()))
  la=max(la,a)
  rm=min(rm,b)
print(max(0,rm-la+1))

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)
if max(left)>min(right):
    print(0)
else:
    print(min(right)-max(left)+1)

Solution (Python):
data=input().split()
N=int(data[0])
M=int(data[1])
data_gate=[input().split() for i in range(M)]
data_min=[]
data_max=[]
for i in range(M):
    data_min.append(int(data_gate[i][0]))
    data_max.append(int(data_gate[i][1]))
m=max(data_min)
M=min(data_max)
if M>=m:
    ans=M-m+1
else:
    ans=0
print(ans)

Solution (Python):
n,m = list(map(int,input().split()))
la = 0
ra = n
for i in range(m):
    l,r=list(map(int,input().split()))
    la = max(la,l)
    ra = min(ra,r)
print(max(0,ra-la+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.append(l)
    R.append(r)

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

num = 0 
for j in range(maxL,minR+1):
    num +=1

print(num)

Solution (Python):
N, M = list(map(int, input().split()))
L, R, flg = 1, N, True
for i in range(M):
    _L, _R = list(map(int, input().split()))
    if L <= _L and _R <= R:
        L = _L
        R = _R
    elif _L <= L and R <= _R:
        continue
    elif L <= _L <= R <= _R:
        L = _L
    elif _L <= L <= _R <= R:
        R = _R
    else:
        flg = False
if flg:
    print(R - 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)
print(min(R)-max(L)+1 if min(R)-max(L)+1>0 else 0)

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

def main():
    n, m = map(int, input().split())
    L, R = 1, n
    for _ in range(m):
        l, m = map(int, input().split())
        if l > L: L = l
        if m < R: R = m
    print(max(0, R-L+1))
    
if __name__ == '__main__':
    main()

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

def main():
    n, m = map(int, input().split())
    L, R = 1, n
    for _ in range(m):
        l, m = map(int, input().split())
        if l > L: L = l
        if m < R: R = m
    if R-L+1 < 0:
        print(0)
    else:
        print(R-L+1)
    
if __name__ == '__main__':
    main()

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

Solution (Python):

def solve():
    dp = [0] * N
    L = max(l)
    R = min(r)
    if L <= R:
        i = list(range(L,R+1))
        ans = len(i)
    else:
        ans = 0
    print(ans)

if __name__ == "__main__":
    N,M = list(map(int, input().split()))

    l,r = [0]*M, [0]*M
    for c in range(M):
        tmp = list(map(int, input().split()))
        l[c] = tmp[0]
        r[c] = tmp[1]
    solve()

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

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

P = [0]*(N+1)

for i in range (0, M):
	A, B = map(int, input().split())
	P[A-1]+=1
	P[B]-=1
    
for i in range (1, N+1):
	P[i]+=P[i-1]
    
P = sorted(P, reverse = True)

count = 0

for i in range (0, N+1):
	if P[i] == M:
		count+=1
	else:
		print(count)
		exit()

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

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

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

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

Solution (Python):
N,M = (int(x) for x in input().split())
Now = [0]*N
maxL = 1
minR = N
for T in range(0,M):
    L,R = (int(x) for x in input().split())
    if maxL<L:
        maxL = L
    if minR>R:
        minR = R
if maxL>minR:
    print(0)
elif maxL==minR:
    print(1)
elif maxL<minR:
    print(minR-maxL+1)

Solution (Python):
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n,m=mp()
a=-float("inf")
b=float("inf")
for i in range(m):
    l,r=mp()
    a=max(a,l)
    b=min(b,r)
print(max(b-a+1,0))

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

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

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

Solution (Python):

(n, m,), *g = [[*map(int, i.split())] for i in open(0)]
ma = n
mi = 0
for l, r in g:
    mi = max(mi, l)
    ma = min(ma, r)

print(max(ma-mi+1,0))

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations

from heapq import heapify,heappop,heappush


from functools import reduce,lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    

    
    N, M = map(int, input().split())
    
    
    S = tuple(tuple(map(int, input().split())) for i in range(M)) 
    acc=[0]*N
    for l,r in S:
        l-=1;r-=1
        acc[l]+=1
        if r+1<N:
            acc[r+1]-=1
    acc=accumulate(acc)
    count=0
    for a in acc:
        if a==M:
            count+=1
    print(count)







if __name__ == "__main__":
    main()

Solution (Python):
n, m = map(int, input().split())
l = 1
r = 10**5
for _ in range(m):
  l_, r_ = map(int, input().split())
  l = max(l, l_)
  r = min(r, r_)
  
if r>=l:
  print(r-l+1)
else:
  print(0)

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

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

Solution (Python):
import numpy as np

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

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

limit1 = max(l[:, 0])
limit2 = min(l[:, 1])

print(max(limit2 - limit1 + 1, 0))

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

L = [None] * M
R = [None] * M

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

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

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

Solution (Python):
def solve():
    N, M = map(int, input().split())
    Count = [0] * (N + 1)
    for i in range(M):
        l, r = map(int, input().split())
        Count[l-1] += 1
        Count[r] -= 1
    ans = 0
    if Count[0] == M: ans = 1
    for i in range(1, N):
        Count[i] += Count[i-1]
        if Count[i] == M: ans += 1
    print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):


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

l=[0]*100005
r=[1000005]*100005
z=1000010
for i in range(m):
   l[i],r[i]=map(int,input().split()) 
mi=max(l)
ma=min(r)



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