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())
lr =[list(map(int,input().split()))for _ in range(m)]

lr.sort(reverse = True)
ansl = lr[0][0]
ansr = 10**9
for i in range(m):
    r = lr[i][1]
    if r < ansr:
        ansr = min(ansr,r)
ans = max(0,ansr-ansl+1)

print(ans)

Solution (Python):
n, m = map(int, input().split())
l0, r0 = 1, n
for j in range(m):
    l1, r1 = map(int, input().split())
    l0 = max(l0, l1)
    r0 = min(r0, r1)
print(max(0, r0-l0+1))

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

Solution (Python):
N,M = map(int,input().split())
LR = [tuple(map(int,input().split())) for i in range(M)]
maxl = 0
minr = N+1
for l,r in LR:
    maxl = max(maxl, l)
    minr = min(minr, r)
print(max(0, minr-maxl+1))

Solution (Python):
n,m = map(int,input().split())
ans = [i+1 for i in range(n+1)]
l = []
r = []
for i in range(m):
    l1,r1 = map(int,input().split())
    l.append(l1)
    r.append(r1)
    
left = max(l)
right = min(r)
print(len(ans[left:right+1]))

Solution (Python):
from itertools import accumulate

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

card = [0]*N
for _ in range(M):
    l, r = map(lambda x: int(x)-1, input().split())
    card[l] += 1
    if r < N-1:
        card[r+1] -= 1
card = accumulate(card)

ans = 0
for ci in card:
    if ci == M:
        ans += 1
print(ans)

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

R=[0]*N
R[0]=K[0]
for i in range(1,N):
  R[i]=R[i-1]+K[i]
import numpy as np
R=np.array(R)
if R.max()==M:
  print((R==M).sum())
else:
  print(0)

Solution (Python):

from sys import stdin,setrecursionlimit
setrecursionlimit(10 ** 7)
n,m = map(int,stdin.readline().rstrip().split())
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(m)]
mi = 0
ma = 10**10
for i in li:
    mi = max(mi,i[0])
    ma = min(ma,i[1])
if mi > ma:
    print(0)
else:
    print(ma-mi+1)

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

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

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

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

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

max_L = 0
min_R = float("inf")
for i in range(m):
    L, R = LR[i]
    max_L = max(max_L, L)
    min_R = min(min_R, R)
if min_R - max_L < 0:
    print(0)
else:
    print(min_R - max_L + 1)

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

for _ in range(m):
    l,r = map(int,input().split())
    ans[0] = max(l, ans[0])
    ans[1] = min(r, ans[1])

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

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

Solution (Python):
import sys

def resolve():
  n, m = map(int, input().split())
  l = 1
  r = 100000
  for i in range(m):
    a, b = map(int, input().split())
    l = max(l, a)
    r = min(r, b)

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

if __name__ == "__main__":
  resolve()

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

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

l = 1
r = N
for i in range(M):
    l = max(l, LR[i][0])
    r = min(r, LR[i][1])

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

Solution (Python):
N,M = map(int,input().split())
k_1 = []
k_2 = []
for i in range(M):
  l,r = map(int,input().split())
  k_1.append(l)
  k_2.append(r)
if max(k_1)<=min(k_2):
  print(min(k_2)-max(k_1)+1)
else:
  print(0)

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

Solution (Python):
def examA():
    N, M = LI()
    l = 1; r = N
    for _ in range(M):
        L, R = LI()
        l = max(L,l)
        r = min(R,r)
    ans = max(0,r-l+1)
    print(ans)
    return

def examB():
    ans = 0
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return read().rstrip().decode('utf-8')
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examA()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Solution (Python):
def main():
    n, m = map(int, input().split())
    gates = [0 for _ in range(n + 1)]
    for _ in range(m):
        l, r = map(lambda x: int(x) - 1, input().split())
        gates[l] += 1
        gates[r + 1] -= 1
    for i in range(1, n + 1):
        gates[i] += gates[i - 1]
    answer = 0
    for i in range(n):
        if gates[i] == m:
            answer += 1
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
inputted = list(map(int, input().split()))
N = inputted[0]
M = inputted[1]

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

left = 1
right = N
for LRi in LR:
    left = max(left, LRi[0])
    right = min(right, LRi[1])

    if left > right: break
    
answer = max(0, right - left + 1)
print(answer)

Solution (Python):
inputted = list(map(int, input().split()))
N = inputted[0]
M = inputted[1]

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

left = 0
right = N
for LRi in LR:
    left = max(left, LRi[0])
    right = min(right, LRi[1])

    if left > right: break
    
answer = max(0, right - left + 1)
print(answer)

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

ans = [table[0][0], table[0][1]]
cnt = 0

for i in table[1:]:
    if ans[0] <= i[0] <= ans[1]:
        ans[0] = i[0]
    if ans[0] <= i[1] <= ans[1]:
        ans[1] = i[1]
    if ans[1] < i[0] or i[1] < ans[0]:
        cnt += 1
        break

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

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

Solution (Python):
n,m = map(int,input().split(' '))
i = 0
lmax = 0
rmin = n
while i < m:
    l,r = map(int,input().split(' '))
    if l > lmax:
        lmax = l
    if r < rmin:
        rmin = r
    i += 1
if lmax <= rmin:
    print(rmin-lmax+1)
else:
    print(0)

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

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
L, R = ZIP(M)
l, r = max(L), min(R)
print(0 if l > r else r-l+1)

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

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

lList = [arr[0] for arr in LR]
rList = [arr[1] for arr in LR]

l = max(lList)
r = min(rList)

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

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

small,big = 0,INF
for i in range(M):
    small = max(small,LR[i][0])
    big = min(big,LR[i][1])

if small <= big:
    print(big-small+1)
else:
    print(0)

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

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

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

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

Solution (Python):
n, m = map(int, input().split())
L = 0
R = 100000

for i in range(m):
    l, r = map(int, input().split())
    if l > L:
        L = l
    if r < R:
        R = r

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

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

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

for n 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):

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
LR = np.array([lr() for _ in range(M)])
L = LR[:, 0]; R = LR[:, 1]
answer = R.min() - L.max() + 1
if answer < 0:
    answer = 0
print(answer)

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
L = 0; R = 10 ** 6
for _ in range(M):
    l, r = lr()
    if l > L:
        L = l
    if r < R:
        R = r

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

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

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

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n,m = iim()
lmax = 0
rmin = n
for i in range(m):
    l,r = iim()
    lmax = max(l,lmax)
    rmin = min(r,rmin)

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

Solution (Python):

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

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

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

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

mn = 0
mx = n

for i in range(m):
    tmp = [int(i) for i in input().split()]
    mn = max(tmp[0], mn)
    mx = min(tmp[1], mx)

print(mx - mn + 1 if mx >= mn else 0)

Solution (Python):
n,m = map(int,input().split())
a = []
lmax = 0
rmin = 10**6
for i in range(m):
    a.append(list(map(int,input().split())))
    if a[i][0] > lmax:
        lmax = a[i][0]
    if a[i][1] < rmin:
        rmin = a[i][1]
if rmin-lmax < 0 :
    print(0)
else :   
    print(1+rmin-lmax)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n,m=MI()
ls=[]
rs=[]
for i in range(m):
    line=input().rstrip().split()
    ls.append(int(line[0]))
    rs.append(int(line[1]))
mx=0
mn=10**6
for l in ls:
    if l>mx:
        mx=l

for r in rs:
    if r<mn:
        mn=r

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

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

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

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

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

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

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

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

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

Solution (Python):
_, __, *d = map(int, open(0).read().split())
print(max(min(d[1::2]) - max(d[::2]) + 1, 0))

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

for k in range(m):
  i,r = map(int,input().split())
  
  i_now.append(i)
  r_now.append(r)

i_max = max(i_now)
r_min = min(r_now)

ans = r_min - i_max + 1

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

Solution (Python):
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))

n,m = load_list()
lr = []

lmax = 0
rmin = 10**6
for _ in range(m):
    l, r = load_list()
    lmax = max(lmax, l)
    rmin = min(rmin, r)

print(len(range(lmax, rmin+1)))

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

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

Solution (Python):
import sys

input = sys.stdin.readline


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

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


if __name__ == "__main__":
    main()

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

Solution (Python):

import sys
input = sys.stdin.readline











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

maxL,minR = 0,10**5+1
for l,r in LR:
    maxL = max(maxL,l)
    minR = min(minR,r)

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

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

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

    a = [tuple([int(t)for t in input().split()])for _ in [0]*m]

    t = max(a,key=lambda x:x[0])[0]
    s = min(a,key=lambda x:x[1])[1]
    ans = s-t+1
    if ans<0:
        print(0)
    else:
        print(ans)

if __name__ == "__main__":
    main()

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

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

L_max = 1
R_min = N

for lr in LR:
  if (L_max < lr[0]):
    L_max = lr[0]

  if (R_min > lr[1]):
    R_min = lr[1]

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N, M = input_nums()
    L = 1
    R = N
    for i in range(M):
        l, r = input_nums()
        L = max(L, l)
        R = min(R, r)
        if L > R:
            print(0)
            exit(0)
    print(R-L+1)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N, M = input_nums()
    L = 1
    R = N
    for i in range(M):
        l, r = input_nums()
        L = max(L, l)
        R = min(R, r)
        if L > R:
            print(0)
            exit(0)
    print(R-L+1)







if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect




from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  n, m = map(int, readline().split())
  left = 0
  right = 10**106
  for _ in range(m):
    l, r = map(int, readline().split())
    left = max(l, left)
    right = min(r, right)
  print(max(right-left+1, 0))
  
  
if __name__ == '__main__':
  main()

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

mini = max(LR[:,0])
maxi = min(LR[:,1])

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

Solution (Python):

n, m = map(int, input().split())
l = []
r = []
for i in range(m):
    lin = list(map(int, input().split()))
    l.append(lin[0])
    r.append(lin[1])
lma = max(l)
rmi = min(r)
if lma > rmi:
    print(0)
else:
    print(rmi - lma + 1)

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

l = 0
r = 10 ** 5 + 1
for i in range(M):
    l = max(l, L[i])
    r = min(r, R[i])

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

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

maxi = 10**6
mini = 0
for lr in LR:
    mini = max(lr[0],mini)
    maxi = min(lr[1],maxi)
if maxi-mini < 0:
    print(0)
else:
    print(maxi-mini+1)

Solution (Python):


def resolve():
    N,M=map(int,input().split())
    l, r = map(int, input().split())
    for i in range(1,M):
        ll,rr=map(int,input().split())
        l=max(l,ll)
        r=min(r,rr)

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


resolve()

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

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

l_max = 0
r_min = 10**9

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

ans = 0
for i in range(l_max, r_min+1):
    ans += 1

print(ans)

Solution (Python):
N, M = map(int, input().split())
left, right = map(int, input().split())
for _ in range(M-1):
    c_left, c_right = map(int, input().split())
    if c_left > left:
        left = c_left
    if c_right < right:
        right = c_right

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

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

s=[0]*n
left=[]
right=[]
for i in range(m):
    l,r=map(int,input().split())
    left.append(l)
    right.append(r)
    
if min(right)<max(left):
    print(0)
else:
    print(min(right)-max(left)+1)

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

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

Solution (Python):
n, m = map(int, input().split())
array = [list(map(int, input().split())) for i in range(m)]
result = [1, n]
for el in array:
    if el[0] > result[1] or result[0] > el[1]:
        result = [1, 0]
        break
    else:
        work = sorted(result + el)
        result = [work[1], work[2]]
print(result[1]-result[0]+1)

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

LM = 0
RM = N+1

for i in range(M):
  L, R = map(int, input().split())
  if L > LM:
    LM = L
  
  if R < RM:
    RM = R
if LM <= RM:
  print(N-(LM-1)-(N-RM))
else:
  print(0)

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

for m in range(M):
  L2,R2 = map(int,input().split())
  L1 = max(L1,L2)
  R1 = min(R1,R2)
  
print(max(0,R1-L1+1))

Solution (Python):
n,m = map(int, input().split())
Lmin = 0
Rmax = n
for i in range(m):
    L,R = map(int, input().split())
    Lmin = max(L-1, Lmin)
    Rmax = min(R, Rmax)
if Lmin < Rmax:
    ans = Rmax - Lmin
else:
    ans = 0
print(ans)

Solution (Python):
N,M = map(int,input().split())
LR = [[int(x) for x in input().split()] for _ in range(M)]
L = 1
R = N
for l,r in LR:
  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())
a=[]
b=[]
for i in range(m):
  f,g=map(int,input().split())
  a.append(f)
  b.append(g)

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

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

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

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

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

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

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

ans = min_r - max_l + 1

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

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




under = 1
top = n

for i in range(m):
    l, r = map(int, input().split())
    if under < l:
        under = l
    if top > r:
        top = r


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

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

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

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

Solution (Python):
import sys

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

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

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

Solution (Python):
N, M = [int(i) for i in input().split()]
left = 1
right = N
for _ in range(M):
  L, R = [int(i) for i in input().split()]
  if left < L:
    left = L
  if right > R:
    right = R
if left <= right:
  print(right-left+1)
else:
  print(0)

Solution (Python):


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

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

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

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

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

ans = [0, 10**6]

for i in range(M):
    ans[0] = max(ans[0], L[i][0])
    ans[1] = min(ans[1], L[i][1])

print(ans[1]-ans[0]+1 if ans[1]-ans[0]+1>0 else 0)

Solution (Python):
n,m = map(int,input().split())
lr = [list(map(int,input().split())) for _ in range(m)]
for i in range(m-1):
  if lr[i][0] > lr[i+1][0]:
    lr[i+1][0] = lr[i][0]
  if lr[i][1] < lr[i+1][1]:
    lr[i+1][1] = lr[i][1]
ans = lr[-1][1] - lr[-1][0] + 1
if ans >= 0:
  print(ans)
else:
  print(0)

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

Solution (Python):
def mi():
    return map(int, input().split())
'''
3 4
*G*S
G**S
*G*S
'''
n,m = mi()
l = -1
r = 1e10
for i in range(m):
    a,b=mi()
    if l>b or b<a:
        print(0)
        exit()
    l = max(l,a)
    r = min(r,b)
if r<l:
    print(0)
else:
    print(r-l+1)

Solution (Python):
INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

n, m = I()
a = [0] * (n + 10)
for i in range(m):
    x, y = I()
    a[x] += 1
    a[y + 1] -= 1
ans = 0
for i in range(1, n + 10):
    a[i] = a[i] + a[i - 1]
    if a[i] == m:
        ans += 1
print(ans)

Solution (Python):
n,m=map(int,input().split())
l=1;ma=n
te=set(range(1,n+1))
for i in range(m):
    x,y=map(int,input().split())
    l=max(x,l)
    ma=min(ma,y)
print(abs(l-ma)+1 if l<=ma else 0)

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

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

Solution (Python):
x=list(map(int, input().split()))
a=1
b=x[0]
for i in range(x[1]):
	c,d=list(map(int, input().split()))
	if a<c:
		a=c
	if b>d:
		b=d

if b-a>=0:
	print(b-a+1)
else:
	print(0)