Problem:
 Score : 300 points    Problem Statement We have N ID cards, and there are M gates.  We can pass the i -th gate if we have one of the following ID cards: the L_i -th, (L_i+1) -th, ..., and R_i -th ID cards.  How many of the ID cards allow us to pass all the gates alone?      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq M \leq 10^5  1 \leq L_i \leq R_i \leq N         Input Input is given from Standard Input in the following format:  N  M  L_1  R_1  L_2  R_2  \vdots  L_M  R_M       Output Print the number of ID cards that allow us to pass all the gates alone.        Sample Input 1 4 2
1 3
2 4      Sample Output 1 2  Two ID cards allow us to pass all the gates alone, as follows:   The first ID card does not allow us to pass the second gate.  The second ID card allows us to pass all the gates.  The third ID card allows us to pass all the gates.  The fourth ID card does not allow us to pass the first gate.        Sample Input 2 10 3
3 6
5 7
6 9      Sample Output 2 1       Sample Input 3 100000 1
1 100000      Sample Output 3 100000

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

Solution (Python):
n, m = [int(i) for i in input().split()]
maxl = -1
minr = 10**6
for i in range(m):
    lh, rh = [int(i) for i in input().split()]
    if maxl < lh:
        maxl = lh
    if minr > rh:
        minr = rh
diff = minr - maxl
if diff >= 0:
    print(diff+1)
else:
    print(0)

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

Lmax = 1
Rmin = N

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

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

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

a = 1
b = n
for i 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):
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)
ll = max(l)
rr = min(r)

if ll>rr:
    print(0)
else:
    print(rr-ll+1)

Solution (Python):
N,M = list(map(int,input().split()))
cardl = []
cardr = []
for i in range(M):
    l,r = list(map(int,input().split()))
    cardl.append(l)
    cardr.append(r)
    
print(max(min(cardr)-max(cardl)+1,0))

Solution (Python):
from itertools import accumulate
n,m = map(int,input().split())
gates = [0]*(n+1)
for i in range(m):
    l,r  = map(int,input().split())
    gates[l-1] += 1
    gates[r] -= 1
gates = list(accumulate(gates))

ans = gates.count(m)
print(ans)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n, m = wi()
lr = mi(m)

gali = [0] * (n + 1)

for i in range(m):
    gali[lr[i][0] - 1] += 1
    gali[lr[i][1]] += -1
gali = list(accumulate(gali))
for i in range(n):
    if gali[i] == m:
        ans += 1
print(ans)

Solution (Python):


def read_input():
    n, m = map(int, input().split())

    max_l = 0
    min_r = 10**5 + 1

    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

    return max_l, min_r



def submit():
    l, r = read_input()

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




if __name__ == '__main__':
    submit()

Solution (Python):
N, M = map(int, input().split())
maxl = 0
minr = float('inf')
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):
n,m=map(int,raw_input().split())
l = []
r = []
for i in xrange(m):
	li,ri=map(int,raw_input().split())
	l.append(li)
	r.append(ri)
L = max(l)
R = min(r)
print max(0,R-L+1)

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)
print(max(0,min(B)-max(A)+1))

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

max_l = 1
min_r = N+1
for i in range(M):
    Li, Ri = (int(x) for x in input().split())
    max_l = max(max_l, Li)
    min_r = min(min_r, Ri+1)

n = max(min_r - max_l, 0)

print(n)

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

left = 1
right = N
for _ 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 = map(int, input().split())
l_max = 0
r_min = 10**5 + 1
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 + 1 > 0:
    print(r_min - l_max + 1)
else:
    print("0")

Solution (Python):
from sys import stdin

n, m = map(int, raw_input().strip().split())
inp = stdin.readlines()

left = 0
right = pow(10, 9)

for line in inp:
    l, r = map(int, line.strip().split())
    left = max(left, l)
    right = min(right, r)

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

print ans

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

Solution (Python):
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsm = lambda: map(str, sys.stdin.readline().split())

n, m = nm()
lis = [0] * (n + 1)
for i in range(m):
    l, r = nm()
    lis[l - 1] += 1
    lis[r] -= 1
for i in range(1, n + 1):
    lis[i] += lis[i - 1]
cnt = 0
for i in lis:
    if i == m:
        cnt += 1
print(cnt)

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

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

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

L = [0] * (N + 2)

for l, r in LR:
  L[l] += 1
  L[r + 1] -= 1

Ans = 0
for i in range(1, N + 1):
  L[i] += L[i - 1]
  if L[i] >= M:
    Ans += 1

print(Ans)

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

Solution (Python):

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

arr = [0]*(n+2)

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

    arr[l-1]+=1
    arr[r]-=1

for i in range(1,len(arr)):
    arr[i] += arr[i-1]


ans = 0

for i in range(len(arr)):
    if arr[i]==m:
        ans+=1

print(ans)

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

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

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

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

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) 


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

ans_max = -1
ans_min = mod
for i in range(M):
    ans_max = max(ans_max,L[i])
    ans_min = min(ans_min,R[i])

ans = max(ans_min-ans_max+1,0)
print(ans)

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

l = 1
r = n
for _ in range(m):
    li, ri = list(map(int, input().split()))
    l = max(l, li)
    r = min(r, ri)

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

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

from itertools import accumulate

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

cnt = [0]*(N+1)

for L, R in info:
    L -= 1; R -= 1
    cnt[L] += 1
    cnt[R+1] -= 1

cnt = list(accumulate(cnt))

ans = 0
for c in cnt:
    if c >= M:
        ans += 1
print(ans)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate

n ,m = li()

imos = [0]*(n+1)

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

cum = list(accumulate(imos))

ans = 0
for ci in cum:
    if ci == m:
        ans += 1

print(ans)

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

imos = [0] * (n + 2)

for l, r in L:
    imos[l] += 1
    imos[r + 1] -= 1

ans = 0

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

print(ans)

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


if __name__ == "__main__":
    main()

Solution (Python):

n, m = map(int, input().split())
lr = [list(map(int, input().split())) for i in range(m)]
card = [0 for i in range(n + 1)]
for i, j in lr:
    card[i - 1] += 1
    card[j] -= 1

for i in range(1, n + 1):
    card[i] += card[i - 1]
ans = 0
for i in card[:-1]:
    if i == m:
        ans += 1
print(ans)

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

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

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

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

Solution (Python):





N,M = map(int, input().split())
csum = [0]*(N+2)
for _ in range(M):
  l,r = map(int, input().split())
  csum[l] += 1
  csum[r+1] -= 1

for i in range(1,N+2):
  csum[i] += csum[i-1]
print(csum.count(M))

Solution (Python):
from itertools import accumulate as ac
n,m=map(int,input().split())
b=[0]*(n+1)
for i in range(m):
    l,r=map(int,input().split())
    b[l-1]+=1
    b[r]-=1
b=list(ac(b))[:-1]
print(b.count(m))

Solution (Python):
def getlist():
	return list(map(int, input().split()))

N, M = getlist()
l = 0
r = float("inf")
for i in range(M):
	L, R = getlist()
	if L > l:
		l = L
	if R < r:
		r = R

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

Solution (Python):
s = [0 for _ in range(int(10e5) + 1)]

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

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


  s[l] += 1
  s[r+1] -= 1

ans = 0

for i in range(1,int(1e5)+1):
  s[i] = s[i-1] + s[i]

  if s[i] == m:
    ans += 1
print(ans)

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

l = -1
r = float("inf")

for x,y in A :
    if l < x :
        l = x
    if y < r :
        r = y
print(max(0,r-l+1))

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

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

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

for i in range(m):
  l,r = map(int,input().split())
  mi = max(mi,l)
  ma = min(ma,r)
  
print(max(0,ma-mi+1))

Solution (Python):
N, M = map(int, input().split())
a = 1
b = 100000
for i 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):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    a, b = LI()
    if a <= 5:
        print(0)
    elif 6 <= a <= 12:
        print(b // 2)
    else:
        print(b)
    return


def B():
    r, d, x = LI()
    for i in range(10):
        print(x * r - d)
        x = x * r - d
    return


def C():
    n, m = LI()
    imo = [0 for i in range(n+1)]
    for _ in range(m):
        l, r = LI_()
        imo[l] += 1
        imo[r + 1] -= 1
    for i in range(1,n):
        imo[i] += imo[i - 1]
    ans = 0
    
    for i in imo:
        if i == m:
            ans += 1
    print(ans)
    return


def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == '__main__':
    C()

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

else:
    print(r-l+1)

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

max_left = 1
min_right = n

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

print(max(min_right - max_left + 1, 0))

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

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

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

LT = max(L)
RB = min(R)

print(max(RB-LT+1,0))

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

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

min_i = 100000000000
max_i = 0
for i in range(m):
    min_i = min(min_i, info[i][1])
    max_i = max(max_i, info[i][0])


print(max(min_i - max_i + 1, 0))

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

for i in range(M):
    x,y = map(int,input().split())
    L[x] += 1
    L[y+1] -= 1
from itertools import accumulate
accumulated =  list(accumulate(L))
cnt = 0
for x in accumulated:
    if x == M:
        cnt += 1
print(cnt)

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

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

Solution (Python):
N, M = map(int, input().split())
x, y = -1, 10**6
for i in range(M):
    l, r = map(int, input().split())
    x = max(l, x)
    y = min(r, y)
print(max(0, y - x + 1))

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

ll, rr = 1, N

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

print(max(rr - ll + 1, 0))

Solution (Python):

def main():
    buf = input()
    buflist = buf.split()
    N = int(buflist[0])
    M = int(buflist[1])
    LR = []
    for i in range(M):
        buf = input()
        buflist = buf.split()
        LR.append((int(buflist[0]), int(buflist[1])))
    LR = list(sorted(LR))
    left_limit = LR[-1][0]
    LR = list(sorted(LR, key=lambda x:x[1]))
    right_limit = LR[0][1]
    print(max(0, right_limit - left_limit + 1))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
inf = float('inf')
mod = 10**9+7


def INT_(n): return int(n)-1


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


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [input() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    from itertools import accumulate
    N, M = MI()
    imos = [0]*(N+2)
    for _ in range(M):
        l, r = MI()
        imos[l] += 1
        imos[r+1] -= 1
    cum = list(accumulate(imos))
    ans = 0
    for a in cum:
        if a == M:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
N,M = map(int, raw_input().split())
L = []
R = []
for i in range(M):
	l1,r1 = map(int, raw_input().split())
	L.append(l1)
	R.append(r1)
l2 = max(L)
r2 = min(R)
if(l2 <= r2):
	print(r2-l2+1)
else:
	print(0)

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

lm = 0
rm = N

for l, r in LR:
    lm = max(lm, l)
    rm = min(rm, r)

result = max(rm - lm + 1, 0)

print(result)

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

Solution (Python):

n,m = map(int, input().split())
tmp = []
tip = [0] * 10 ** 7
ma = 0
for i in range(m):
    x,y = map(int, input().split())
    ma = max(ma, y)
    tip[x] += 1
    tip[y + 1] -= 1

for i in range(1, ma + 1):
    tip[i] += tip[i - 1]

print(tip.count(m))

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

left = 1
right = N
for i in LR:
    Li, Ri = map(int, i.split(" "))
    left = max(left, Li)
    right = min(right, Ri)

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

Solution (Python):



def solve():
    n, m = map(int, input().split())
    l, r = 0, 55301301
    for i in range(m):
        a, b = map(int, input().split())
        l = max(a, l)
        r = min(b, r)
    print(max(0, r - l + 1))

if __name__=="__main__":
    solve()

Solution (Python):



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

ll=1
rr=n
for _ in range(m):
    l,r = [int(i) for i in input().split()]
    if ll < l:
        ll=l
    if rr > r:
        rr=r
    


print(max(0,rr-ll+1))

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

L = 1
R = 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):
import sys
sys.setrecursionlimit(10**7)






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

    max_l = max(l_list)
    min_r = min(r_list)
    print(max(0, min_r - max_l + 1))

solve()

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

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

Solution (Python):
def getN():
    return int(input())

def getMN():
    return list(map(int, input().split()))

def getlist():
    return list(map(int, input().split()))

n, m = getlist()
imos = [0 for i in range(n+1)]
for i in range(m):
    a, b = getlist()
    imos[a-1] += 1
    imos[b] -= 1

accum = [0]
tmp = 0
for val in imos:
    tmp += val
    accum.append(tmp)

ans = 0
for v in accum:
    if v == m:
        ans += 1

print(ans)

Solution (Python):
import sys


fin = sys.stdin.readline
N, M = [int(elem) for elem in fin().split()]
cards = [[int(elem) - 1 for elem in fin().split()] for _ in range(M)]

num_cards_passes_at = [0] * N
for l, r in cards:
    num_cards_passes_at[l] += 1
    if r < N - 1:
        num_cards_passes_at[r + 1] -= 1

cur_state = 0
for i in range(N):
    cur_state += num_cards_passes_at[i]
    num_cards_passes_at[i] = cur_state


print(sum(state == M for state in num_cards_passes_at))

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

Solution (Python):
from itertools import accumulate
n,m = map(int,input().split())
imos = [0]*(10**5+10)
for i in range(m):
    l,r = map(int,input().split())
    imos[l-1] += 1
    imos[r] -= 1
A = accumulate(imos)
cnt = 0
for a in A:
    if a == m:
        cnt += 1
print(cnt)

Solution (Python):
import sys
from collections import deque
import math
sys.setrecursionlimit(20000000)

n,m = map(int,input().split())
x = []
LE = 0
RI = n
for i in range(m):
    a,b = map(int,input().split())
    LE = max(LE,a)
    RI = min(RI,b)
print(max(0,RI-LE+1))

Solution (Python):
def cumsum(l):
    for i in range(1, len(l)):
        l[i] += l[i-1]
    return l


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

    imos = cumsum(imos)
    

    print(len([x for x in imos if x == M]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys, math, collections, heapq, itertools
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    N, M = map(int, line_input())
    gate = [0 for n in range(N+1)]
    for i in range(M):
        l, r = map(int, line_input())
        gate[l-1] += 1
        gate[r] -= 1
    ans = (1 if gate[0] == M else 0)
    for i in range(1, N):
        gate[i] = gate[i] + gate[i-1]
        if gate[i] == M: ans += 1
    print(ans)
    return 0
  
if __name__ == "__main__":
    solve()

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(L)
r = min(R)

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

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

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

max_l = -1
min_r = 999999
for i in range(0, m):
    l, r = map(int, raw_input().split(" "))
    max_l = max(max_l, l)
    min_r = min(min_r, r)

ans = min_r - max_l + 1

if ans <= 0:
    print(0)

else:
    print(ans)

Solution (Python):
from itertools import accumulate
N, M = map(int, input().split())
T = [0]*(N+1)

for _ in range(M):
    L, R = map(int, input().split())
    T[L-1] += 1
    T[R] -= 1

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

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

l_max, r_min = -1, 100000
for _ in range(M):
    L, R = list(map(int, input().split()))
    if l_max < L:
        l_max = L
    if r_min > R:
        r_min = R

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

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

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

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

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b



def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    dp = [0] * (10**5 + 100)
    for _ in range(M):
        L, R = map(int, input().split())
        dp[L] += 1
        dp[R + 1] -= 1

    for i in range(len(dp) - 1):
        dp[i + 1] += dp[i]

    ans = 0
    for i in range(len(dp)):
        ans += dp[i] == M
    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
n,m = (int(num) for num in input().split())

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

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

Solution (Python):


import sys
import math

import array
import bisect
import collections
from collections import Counter, defaultdict
import fractions
import heapq
import re

sys.setrecursionlimit(1000000)


def array2d(dim1, dim2, init=None):
    return [[init for _ in range(dim2)] for _ in range(dim1)]

def argsort(l, reverse=False):
    return sorted(range(len(l)), key=lambda i: l[i], reverse=reverse)

def argmin(l):
    return l.index(min(l))

def YESNO(ans, yes="YES", no="NO"):
    print([no, yes][ans])

II = lambda: int(input())
MI = lambda: map(int, input().split())
MIL = lambda: list(MI())
MIS = lambda: input().split()


def main():
    N, M = MI()
    lr = [0] * (N + 1)
    rr = [0] * (N + 1)
    for i in range(M):
        l, r = MI()
        lr[l] += 1
        rr[r] += 1
    c = 0
    ans = 0
    for i in range(N + 1):
        c += lr[i]
        if c == M: ans += 1
        c -= rr[i]
    return ans


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

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

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

Solution (Python):
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD=10**9+7

N,M=rl()
L=[0]*M
R=[0]*M
for i in range(M):
    L[i],R[i] = rl()

gate = [0]*(N+2)
for l,r in zip(L,R):
    gate[l] += 1
    gate[r+1] -= 1
for i in range(1,N+1):
    gate[i] += gate[i-1]

print(gate.count(M))

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

Solution (Python):
import sys


s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, M = i2nn()
    LR = ii2nnn(M)
    L, R = 0, int(1e+5)
    for l, r in LR:
        if L < l:
            L = l
        if R > r:
            R = r
    n = R - L + 1
    if n < 0:
        n = 0
    print(n)

main()

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

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

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

Solution (Python):
def main():
    from sys import stdin, stdout

    def read():
        return stdin.readline().rstrip('\n')

    def read_array(sep=None, maxsplit=-1):
        return read().split(sep, maxsplit)

    def read_int():
        return int(read())

    def read_int_array(sep=None, maxsplit=-1):
        return [int(a) for a in read_array(sep, maxsplit)]

    def write(*args, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in args) + end)

    def write_array(array, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in array) + end)

    n, m = read_int_array()
    l, r = 1, n
    for i in range(m):
        l1, r1 = read_int_array()
        l = max(l, l1)
        r = min(r, r1)
    write(max(r+1-l, 0))


main()

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