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


import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,M=MAP()

imos=[0]*(N+1)
for i in range(M):
    l,r=MAP()
    imos[l-1]+=1
    imos[r]-=1

acc=list(accumulate(imos))

mx=max(acc)
if mx==M:
    print(acc.count(mx))
else:
    print(0)

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

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

if lmax > rmin:
    print(0)
else:
    print(rmin-lmax+1)

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

table=[0]*(n+2)
for i in range(m):
    table[a[i][0]]+=1
    table[a[i][1]+1]-=1

for i in range(1,n+1):
    table[i]+=table[i-1]

ans=0
for i in table:
    if i==m:
        ans+=1

print(ans)

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

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

cards = [0 for i in range(N+1)]
for i in range(M):
    L,R = (int(k) for k in input().split())
    cards[L-1] += 1
    cards[R] -= 1

anscards = [0 for i in range(N)]
total = cards[0]
for i in range(N):
    anscards[i] = total
    total += cards[i+1]      
    
print(anscards.count(M))

Solution (Python):
import sys
rr = lambda: sys.stdin.readline().strip()
rri = lambda: int(rr())
rrs = lambda: [int(_) for _ in rr().split()]

N, M = rrs()
data = [rrs() for _ in range(M)]

L = data[0][0]
R = data[0][1]

for d in data:
    if L < d[0]:
        L = d[0]
    if R > d[1]:
        R = d[1]

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

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

Solution (Python):
n, m = (int(i) for i in input().split())
lr = [[int(i) for i in input().split()] for i in range(m)]
lr.sort(key=lambda x:x[0])
low = 1
high = n
for l,e in lr:
    if low < l:
        low = l
    if high > e:
        high = e
if high >= low:
    print(high-low+1)
else:
    print("0")

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

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

Solution (Python):




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


L_max = max(L_list)
R_min = min(R_list)
for i in range(M):
    pass

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

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

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

A = [0, 1000000]
for i in range(M) :
    L, R = map(int, input().split(' '))
    A[0] = max(A[0], L)
    A[1] = min(A[1], R)

ans = max(A[1] - A[0] + 1, 0)
print(ans)

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

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

n_card=min(r_list)-max(l_list)+1

if n_card<=0:
    print(0)
else:
    print(n_card)

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

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

Solution (Python):


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

start = 1
end = N

for i in range(M):
    L,R = list(map(int,input().split()))
    start = max(start, L)
    end = min(end, R)

print(max(0, end - start + 1))

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

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    N, M = LI()
    LR = []
    for _ in range(M):
        LR.append(LI())
    left = 0
    right = INF
    for l, r in LR:
        left = max(left, l)
        right = min(right, r)
    ans = max(right - left + 1, 0)
    return ans

print(main())

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

max_l = max(lr[i][0] for i in range(m))
min_r = min(lr[i][1] for i in range(m))

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

Solution (Python):
n, m = map(int, input().split())
card = [0 for i in range(n + 1)]

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

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

ans = 0

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

print(ans)

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

Left= 0
Right = N + 1
for i in range(M):
    Left = max(Left,LR[i][0])
    Right = min(Right,LR[i][1])
    
if Right - Left < 0:
    print(0)
else:
    print(Right - Left + 1)

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

LL = []
RR = []

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

lmax = max(LL)
rmin = min(RR)

ans = rmin - lmax + 1

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

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

Solution (Python):
n, m = map(int, input().split())
lr_array = [[int(x) for x in input().split()] for x in range(m)]
res_l  = 0
res_r  = float("inf")
for l, r in lr_array:
    res_l = max(res_l, l)
    res_r = min(res_r, r)
if res_l == res_r:
    print(1)
elif res_l > res_r:
    print(0)
else:
    print(res_r-res_l+1)

Solution (Python):




import itertools
import functools

def main():
    
    n, m = [int(s) for s in input().split()]
    lrs = [[int(s) for s in input().split()] for _ in range(m)]
    ls = [lr[0] for lr in lrs]
    rs = [lr[1] for lr in lrs]
    
    l = functools.reduce(max, ls)
    r = functools.reduce(min, rs)
    ans = r-l+1 if l<=r else 0
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

"""



"""
import sys


def solve(N, M, cards):
    left, right = 1, N
    for l, r in cards:
        if l > left:
            left = l
        if r < right:
            right = r

    return max(0, right-left+1)


def main(args):
    N, M = map(int, input().split())
    cards = [[int(i) for i in input().split()] for _ in range(M)]
    ans = solve(N, M, cards)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
n, m = map(int, input().split())
ll = 0
rr = n
for _ in range(m):
    l, r = map(int, input().split())
    ll = max(ll, l)
    rr = min(rr, r)

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

Solution (Python):
N, M =map(int,input().split())
L = 0
R = N
for i in range(M):
  Lnew, Rnew =map(int,input().split())
  if(L < Lnew):
    L = Lnew
  if(R > Rnew):
    R = Rnew

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

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

Min, Max = 1, N

for i in range(M):
    L, R = LR[i]
    Min = max(Min, L)
    Max = min(Max, R)

print(max(Max - Min + 1, 0))

Solution (Python):
n,m=[int(x) for x in input().split()]
first_gate=0
last_gate=10**10
for _ in [0]*m:
    small,large=[int(x) for x in input().split()]
    first_gate=max(first_gate,small)
    last_gate=min(last_gate,large)
print(max(0,last_gate- first_gate+1))

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

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

if __name__ == '__main__':
    main()

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

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

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

sys.setrecursionlimit(10**7)

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

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

L_max = 1
R_min = N

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

Solution (Python):
N,M=map(int,input().split())
L=[]
MM=[]
for i in range(0,M):
  l,m=map(int,input().split())
  L.append(l)
  MM.append(m)
ma=min(MM)
mi=max(L)
ans=ma-mi+1

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

Solution (Python):
N,M = map(int,input().split(' '))
l = [list(map(int,input().split())) for i in range(M)]
st = 0
ed = N
flag = False
for i in l:
    if st<i[0]:
        st = i[0]
    if i[1]<ed:
        ed = i[1]
    if st>ed:
        flag = True
        print(0)
        break
if not flag:
    print(ed-st+1)

Solution (Python):



import numpy as np

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

max_l = 0
min_r = int(1e9)
for _ in range(M):
    l, r = map(int, input().rstrip().split(' '))
    max_l = max(max_l, l)
    min_r = min(min_r, r)
    
output = min_r - max_l + 1
if output < 0:
    print(0)
else:
    print(output)

Solution (Python):
N, M = map(int, input().split())
T = {i: 0 for i in range(N+1)}
for i in range(M):
    l, r = map(int, input().split())
    T[l-1] += 1
    T[r] -= 1

for i in range(1, N):
    T[i] += T[i-1]

ans = 0
for i in range(N):
    if T[i] == M:
        ans += 1
print(ans)

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, M = tuple(li())
interval = [1, N]
def intersect(ia, ib):
    if not ia or not ib:
        return []
    l = max(ia[0], ib[0])
    r = min(ia[1], ib[1])
    if l > r:
        return []
    else:
        return [l, r]

for i in range(M):
    l, r = tuple(li())
    interval = intersect(interval, [l, r])
if not interval:
    print(0)
else:
    print(interval[1] - interval[0] + 1)

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

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

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

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

low = 1
high = n

for i in range(0,m):
    if gate[i][0] > high or gate[i][1] < low:
        print(0)
        break
    low = max(low,gate[i][0])
    high = min(high,gate[i][1])
else:
    print(high-low+1)

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

imos = [0] * (10 ** 5 + 2)

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

for i in range(10 ** 5 + 1) :
    imos[i+1] += imos[i]

print(imos.count(M))

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N, M = inpl()
L = 1
R = N
for _ in range(M):
    Li, Ri = inpl()
    if Li > L:
        L = Li
    if Ri < R:
        R = Ri
print(max(R-L+1,0))

Solution (Python):
n,m=[int(_) for _ in input().split()]
ld=1
rd=n
for iii in range(m):
  l,r=[int(_) for _ in input().split()]
  if ld<l:
    ld=l
  if rd>r:
    rd=r
if ld<=rd:
  print(rd-ld+1)
else:
  print(0)

Solution (Python):
N,M=(int(x) for x in input().split())
mx=0
mn=10**5+1
for i in range(M):
    L,R=(int(x) for x in input().split())
    mn=min(R,mn)
    mx=max(L,mx)

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

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

Solution (Python):
N,M = [int(x) for x in input().split()]
mini = 0 
maxi = 1.1e+5
for i in range(M):
    l,r = [int(x) for x in input().split()]
    mini = max(mini,l)
    maxi = min(maxi,r)

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

Solution (Python):
import sys

readline = sys.stdin.readline

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

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

LR.sort(key=lambda x:x[0])


span = LR[0]
for l, r in LR:
    if l > span[1]:
        span = None
        break
    else:
        span[0] = l
        span[1] = min(r, span[1])

if span is None:
    print(0)
else:
    print(span[1] - span[0] + 1)

Solution (Python):
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)
print(max(min(R)-max(L)+1, 0))

Solution (Python):



def solve(m, bounds):
    le, ri = bounds[0]
    for i in range(1, m):
        new_le, new_ri = bounds[i]
        le = max(le, new_le)
        ri = min(ri, new_ri)
        if le > ri:
            return 0
    return ri - le + 1


def main():
    n, m = (int(z) for z in input().split())
    bounds = [tuple(int(x) for x in input().split()) for _ in range(m)]
    res = solve(m, bounds)
    print(res)


if __name__ == "__main__":
    main()

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

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


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

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

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

right = N
left = 0

for i in e:
    if left < i[0]:
        left = i[0]
    if right > i[1]:
        right = i[1]
if right >= left:
    print(right - left + 1)
else:
    print("0")

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())
l_list = []
r_list = []
for i in range(m):
    l, r = map(int, input().split())
    l_list.append(l)
    r_list.append(r)
if min(r_list) - max(l_list) < 0:
    print(0)
    exit()
else:
    print(min(r_list) - max(l_list) + 1)

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

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

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

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

ans = 0
cnt = 0
for i, c in enumerate(cl):
    cnt += c
    if cnt == M:
        ans += 1


print(ans)

Solution (Python):

N, M = [int(n) for n in input().split()]
L = [0]*M
R = [0]*M
imos = [0 for _ in range(N+1)]
for i in range(M):
    L[i], R[i] = [int(n) for n in input().split()]
    imos[L[i]-1] += 1
    imos[min(R[i], N)] -= 1

for i in range(1,N+1):
    imos[i] = imos[i-1] + imos[i]

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

print(ans)

Solution (Python):
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n, m = f()
    g = [ f() for i in range(m)]

    dm = [0] * (n+2)

    for x,y in g:
        dm[x] += 1
        dm[y+1] -= 1

    cnt = 0

    
    for i in range(1, n+1):
        dm[i] = dm[i] + dm[i-1]
        if dm[i] == m:
            cnt += 1

    print(cnt)


solve()

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

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

for i in range(m):
    li, ri = [int(i) for i in input().split(" ")]
    if l < li:
        l = li
    if ri < r:
        r = ri

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

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

l = 1
r = N
for i in range(M):
    li, ri = map(int, input().split())
    l = max(l, li)
    r = min(r, ri)

ans = r - l + 1

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

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

Solution (Python):
N, M = map(int, input().split())
lr = [list(map(int, input().split())) for i in range(M)]
maxx = 100000
minn = 0

for l, r in lr:
    minn = max(l, minn)
    maxx = min(r, maxx)

if maxx - minn >= 0:
    print(maxx - minn + 1)
else:
    print(0)

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

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

s = 1
e = n

for _ in range(m):
  l,r = map(int,input().split())
  
  s = max(s,l)
  e = min(e,r)
  
print(max(e-s+1,0))

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

for i in range(M):
    l, r = list(map(int, input().split()))
    if ll <= l:
        ll = l
    if r <= rr:
        rr = r

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

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

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

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

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

LMax = LR[0][0]
RMin = LR[0][1]
for l,r in LR[1:]:
  if LMax < l:
    LMax = l
  if RMin > r:
    RMin = r

print(max(0,RMin - LMax + 1))

Solution (Python):


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


st = -1
end = N
for i in range(M):
    if L[i] > st:
        st = L[i]
    if R[i] < end:
        end = R[i]

r = end - st + 1
if r < 0:
    r = 0
print(r)

Solution (Python):
N,M=list(map(int,input().strip().split(' ')))
Gates=[]
left=[]
right=[]
for _ in range(M):
    a,b=list(map(int,input().strip().split(' ')))
    left+=[a]
    right+=[b]
    Gates+=[(a,b)]
ans=max(min(right)-max(left)+1,0)
print(ans)

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

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

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

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

Solution (Python):

def solve(): 
    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)
    res = max(r-l+1, 0)
    return str(res)

if __name__ == '__main__':
    print(solve())

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

n,q = m()

a = [0 for i in range(n)]

for i in range(q):
	j,k = m()
	a[j-1] += 1
	if k != n:
		a[k] -= 1
co = 0
for i in range(1,n):
	a[i] += a[i-1]
for i in range(n):
	if a[i] == q:
		co += 1

print(co)

Solution (Python):
N, M = [int(item) for item in input().split()]
now_r = 10000000000
now_l = 0
for i in range(M):
  l, r = [int(item) for item in input().split()]
  now_r = min(r, now_r)
  now_l = max(l, now_l)
  
if (now_l > now_r):
  print (0)
else:
  print (now_r - now_l + 1)

Solution (Python):
def run(N, M, L, R):
    maxL = max(L)
    minR = min(R)
    if maxL > minR:
        ret = 0
    else:
        ret = minR - maxL + 1
    return ret


def main():
    N, M = map(int, input().split())
    L = []
    R = []
    for _ in range(M):
        LR = list(map(int, input().split()))
        L.append(LR[0])
        R.append(LR[1])
    print(run(N, M, L, R))


if __name__ == '__main__':
    main()

Solution (Python):


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

minimum, maximum = 0, N+1

for i in range(M):
    L, R = list(map(int, input().split()))
    if minimum < L:
        minimum = L
    if R < maximum:
        maximum = R

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

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

p=0
q=100000000000000

for i in range(M):
    l,r=map(int,input().split())
    p=max(p,l)
    q=min(q,r)
print(max(q-p+1,0))

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

l = 0
r = n+1

for i in range(m):
    li,ri = words(int)
    l = max(l,li)
    r = min(r,ri)

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

Solution (Python):
import sys

N, M = list(map(int, sys.stdin.readline().strip().split()))

l, r = list(map(int, sys.stdin.readline().strip().split()))

for _ in range(M-1):
    tl, tr = list(map(int, sys.stdin.readline().strip().split()))

    if r > tr:
        r = tr
    if l < tl:
        l = tl

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

L_lst = []
R_lst = []
for i in range(M):
  L_lst.append(LR[i][0])
  R_lst.append(LR[i][1])
  
L_max = max(L_lst)
R_in = min(R_lst)

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

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

Solution (Python):
import sys
input = sys.stdin.readline
n,m = input().split()
n = int(n)
m = int(m)
l = 0
r = n+1
for i in range(m):
    l2,r2 = input().split()
    l2 = int(l2)
    r2 = int(r2)
    if l2 > l:
        l = l2
    if r2 < r:
        r = r2
if (r - l + 1) >= 0:
    print(r-l+1)
else:
    print("0")

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

print(max(min(rs) - max(ls) + 1, 0))

Solution (Python):
r, m = [int(i) for i in input().split()]
l = []
r = []
for i in range(m):
    lx, rx = [int(j) for j in input().split()]
    l.append(lx)
    r.append(rx)
ans = max(0, min(r) - max(l) + 1)
print(ans)

Solution (Python):
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)

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

Solution (Python):
n,m=map(int,input().split())
p,P=map(int,input().split())
for i in range(m-1):
  a,b=map(int,input().split())
  if p<a:
    p=a
  if b<P:
    P=b
print(max(0,P-p+1))

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

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

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

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

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

min = 1
max = N
for i in range(M):
	l, r = map(int, input().split(" "))
	if l > min:
		min = l
	if r < max:
		max = r

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

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(0,R-L+1))

Solution (Python):
N, M =list(map(int, input().split()))
LR_list = [list(map(int, input().split())) for _ in range(M)]
get_L = lambda lst: lst[0]
get_R = lambda lst: lst[1]
Lmax = max(map(get_L, LR_list))
Rmin = min(map(get_R, LR_list))
if (Rmin < Lmax):
    print(0)
else:
    print(Rmin - Lmax +1)

Solution (Python):

N ,M = map(lambda x: int(x),input().split())

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

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

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, LR):
    l = 1
    r = N
    for L, R in LR:
        l = max(l, L)
        r = min(r, R)
    return max((r - l + 1), 0)


def main():
    
    N, M = read_int_n()
    LR = [read_int_n() for _ in range(M)]
    print(slv(N, M, LR))


if __name__ == '__main__':
    main()

Solution (Python):
N,M=map(int ,raw_input().split())
S = 1
E = N
for i in range(M):
    L,R=map(int ,raw_input().split())
    if S<L:
        S=L
    if E>R:
        E=R
if E<S:
    print(str(0))
else:
    print(str(E-S+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:
    print(R-L+1)
else:
    print(0)

Solution (Python):

from math import *

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

l=[0]*n
r=[0]*n

for i in range(m):
	s=input().split(" ")
	l[int(s[0])-1]+=1
	r[int(s[1])-1]+=1



i=0

c=0
tmp=0

while(i<n):
	tmp+=l[i]
	if(tmp>=m):
		c+=1
	tmp-=r[i]
	i+=1

print(c)

Solution (Python):

"""
Created on Sat May 25 20:54:19 2019

@author: Yamazaki Kenichi
"""

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

left, right = 1, N
for c in lr:
    left = max(left,c[0])
    right = min(right,c[1])
ans = max(right - left + 1, 0)
print(ans)

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

Solution (Python):
n,m=map(int,input().split())
L=1
R=n
for _ 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):

if __name__ == '__main__':
    n,m = list(map(int, input().split()))
    lnum = 0
    rnum = 100000
    for i in range(m):
        l,r = list(map(int, input().split()))
        lnum = max(lnum, l)
        rnum = min(rnum, r)
    
    result = rnum - lnum + 1
    if(result < 0):
        print(0)
    else:
        print(result)

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

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