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())
gate = [[int(i) for i in input().split()] for _ in range(m)]

minv = gate[0][0]
maxv = gate[0][1]

for x in gate:
  minv = max(minv,x[0])
  maxv = min(maxv,x[1])
  
print(maxv-minv+1 if maxv-minv+1 >= 1 else 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)


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

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

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

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)

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

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

Solution (Python):
n,m = map(int, input().split())
left = set()
right = set()
for i in range(m):
  l,r = map(int, input().split())
  left.add(l)
  right.add(r)
mini = max(left)
maxi = min(right)
print(len(range(mini,maxi+1)))

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

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

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

print(ans)

Solution (Python):
import sys

input = sys.stdin.readline
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
common_l, common_r = 0, N
for l, r in LR:
    common_l = max(l, common_l)
    common_r = min(r, common_r)
print(max(0, common_r-common_l+1))

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

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

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

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

l_max = 0
r_min = n

for _ in range(m):
    x, y = map(int, input().split())
    l_max = max(l_max, x)
    r_min = min(r_min, y)

print(len([i for i in range(l_max, r_min + 1)]))

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

min_i = 1
max_i = n

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

if max_i-min_i>=0:
    print(max_i-min_i+1)
else:
    print(0)

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

Solution (Python):
N,M=map(int,input().split())
L,R=map(int,input().split())
for i in range(M-1):
    H,I=map(int,input().split())
    if H>L:
        L=H
    if R>I:
        R=I
    if R<L:
        break
if R<L:
    print(0)
else:
    ans=list(range(L,R+1))
    print(len(ans))

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

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

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

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

N, M = I()
count = 0
 
min_a = 1
max_b = N
 
for i in range(M):
    a,b = [int(i) for i in input().split()]
    if min_a < a:
        min_a = a
    if max_b > b:
        max_b = b
 
print(max(0, max_b-min_a+1))

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

max_l, min_r = 0, float("inf")
for i in range(m):
  max_l = max(s[i][0], max_l)
  min_r = min(s[i][1], min_r)
  
print(max(min_r - max_l + 1, 0))

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

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

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

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

import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    min = L[0]
    max = R[0]
    for i in range(M):
        if L[i] > min:
            min = L[i]
        if R[i] < max:
            max = R[i]
    ans = 0
    for i in range(1, N+1):
        if min <= i <= max:
            ans += 1
    print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    L = [int()] * (M)  
    R = [int()] * (M)  
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

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

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

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

max_l = 0
min_r = f_inf
for l, r in LR:
    max_l = max(max_l, l)
    min_r = min(min_r, r)

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

Solution (Python):

n, m = map(int,input().split())
r = float('inf')
l = 0
L = []
for i in range(m):
    l_, r_ = map(int,input().split())
    L.append([l_,r_])
    l = max(l_, l)
    r = min(r_, r)
ans = 0
print(max(0, r-l+1))

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

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

for i in range(n):
    s[i + 1] += s[i]
print(s.count(m))

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

Solution (Python):
import numpy as np
n, m = map(int, input().split())
ans = [0]*n

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

c = list(np.cumsum(ans))
print(c.count(m))

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

INF = float('inf')
ans_l, ans_r = -INF, INF
for l, r in lr:
    ans_l = max(ans_l, l)
    ans_r = min(ans_r, r)
print(max(0, ans_r-ans_l+1))

Solution (Python):
N,M = map(int,input().split())
L = [0 for _ in range(N)]
for i in range(M):
  l,r = map(int,input().split())
  l-=1;r-=1 
  L[l] += 1
  if r+1 < N:
    L[r+1] -= 1
S = [0]
for i in range(N):
  temp = L[i] + S[-1]
  S.append(temp)

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

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)
x=max(a)
y=min(b)

if y-x<0:
    print('0')
else:
    print(y-x+1)

Solution (Python):

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

Solution (Python):
n, m = map(int, input().split())
max_l = 0
min_r = 10 ** 10
for _ 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 = map(int,input().split())


l = [1]*n
L,R = 0,float('inf')
for _ in range(m):
    a,b = map(int,input().split())
    L = max(L,a)
    R = min(R,b)
ans = R-L+1
if ans<=0:ans = 0
print(ans)

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

Solution (Python):

url = "https://atcoder.jp//contests/abc127/tasks/abc127_c"

def main():
    n, m = list(map(int, input().split()))
    l, r = 0, 999999
    for i in range(m):
        a,b = list(map(int, input().split()))
        l = a if a > l else l
        r = b if b < r else r
    ans = r - l
    print(ans + 1) if ans >= 0 else print(0)


if __name__ == '__main__':
    main()

Solution (Python):
import math
import collections
import itertools

def YesNo(Bool):
    if(Bool):
        print("Yes")
    else:
        print("No")
    return

def resolve():

    N,M=map(int,input().split())
    L=[0]*M
    R=[0]*M
    Lmax=0
    Rmin=10**10
    for i in range(M):
        L[i],R[i]=map(int,input().split())
        Rmin=min(R[i],Rmin)
        Lmax=max(L[i],Lmax)

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

resolve()

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

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

L_SORT = sorted(L)
R_SORT = sorted(R)




L_MAX = L_SORT[M-1]
R_MIN = R_SORT[0]





if (R_MIN - L_MAX) < 0:
  print(0)
else:
  print(R_MIN-L_MAX + 1)

Solution (Python):
n, m = map(int, input().split())
min_id = 0
max_id = 10**9
for _ in range(m):
    l, r = map(int, input().split())
    min_id = max(l, min_id)
    max_id = min(r, max_id)

ans = max_id - min_id + 1
if ans <= 0:
    print(0)
else:
    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(0, R - L + 1))

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

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

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

Solution (Python):
n, m = map(int, input().split())
lmax, rmin = map(int, input().split())
for i in range(1, m):
  l, r = map(int, input().split())
  lmax = max(lmax, l)
  rmin = min(rmin, r)
print(max(0, rmin-lmax+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())
    l_ans=max(l_ans,l)
    r_ans=min(r_ans,r)

if r_ans>=l_ans:
    print(r_ans-l_ans+1)
else:
    print(0)

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

L, R = 0, 10 ** 5 + 1
for _ in range(M):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r+1)

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

Solution (Python):


def main():
    N, M = map(int, input().split())
    imos = [0] * (N + 2)
    ans = 0

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

    for i in range(1, N + 2):
        imos[i] += imos[i - 1]
        if imos[i] == M and i != N + 1:
            ans += 1
    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
def abc127c():
    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(min(rs)-max(ls)+1, 0)
    print(ans)
abc127c()

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

L_list = []
R_list = []

for i in range(M):
    A, B = map(int, input().split())
    L_list.append(A)
    R_list.append(B)

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

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

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

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

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

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

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

Solution (Python):
N,M = map(int,input().split())
min_i = 0
max_i = N+1
for _ in range(M):
  a,b = map(int,input().split())
  min_i = max(min_i,a)
  max_i = min(max_i,b+1)
  
c = max(max_i - min_i,0)
print(c)

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

Solution (Python):

n, m = map(int, input().split())
l_max, r_min = -1, float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    l_max = max(l_max, li)
    r_min = min(r_min, ri)

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

Solution (Python):
from itertools import accumulate
N, M = map(int, input().split())











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

imos = list(accumulate(imos))

ans = 0
for im in imos:
    ans += (im == M)

print(ans)

Solution (Python):
from itertools import accumulate
N, M = map(int, input().split())

left = 1
right = N

for i in range(M):
    a,b = map(int, input().split())
    left = max(left,a)
    right = min(right, b)

print(max(0,right-left+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)
  
Rm=min(R)
LM=max(L)

if Rm-LM >= 0:
  print(Rm-LM+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())
    
    if L > l:
        l = L
            
    if R < r:
        r = R
                
    ans = r - l + 1
    if ans < 0:
        print(0)
        break

else:
    print(ans)

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

Solution (Python):

import sys
input=sys.stdin.readline

def main():
    gates=[]
    n,m = map(int,input().split())
    gates=[list(map(int,input().split())) for _ in range(m)]
    min_data=0
    max_data=10**5+1

    for gate in gates:
        if min_data < gate[0]:
            min_data=min(gate)
        if max_data > gate[1]:
            max_data=max(gate)
    print(len(list(range(min_data,max_data+1))))

if __name__=="__main__":
    main()

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

a = 1
b = N

for i in range(M):
    if(L[i] >= a and R[i] >= b):
        a = L[i]
    if(L[i] <= a and R[i] <= b):
        b = R[i]
    if(L[i] >= a and R[i] <= b):
        a = L[i]
        b = R[i]
    if(R[i] < a or L[i] > b):
        print(0)
        exit()


print(b-a+1)

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

L = []

for i in range(M):
    LR = list(map(int, input().split()))
    L.append(LR)

res = [L[0][0], L[0][1]]

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

if res[1] < res[0]:
    print(0)
else: 
    print(res[1] - res[0] + 1)

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

ll, rr = 1, l
for l, r in lr:
    if ll <= l <= rr or l <= ll <= r:
        ll, rr = max(ll, l), min(rr, r)
    else:
        print(0)
        exit()
print(rr-ll+1)

Solution (Python):
import sys
import copy
import math
import itertools
import numpy as np
l = [int(c) for c in input().split()]
N=l[0]
M=l[1]
LR=[list(map(int,input().split())) for c in range(M)]
small = 0
large = N
for i in range(M):
    if small<LR[i][0]:
        small=LR[i][0]
    
    if large>LR[i][1]:
        large=LR[i][1]

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

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

Lm = -1
Rm = float('inf')

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

print(max(Rm - Lm + 1, 0))

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

l_ans = 0
r_ans = float('inf')
for l, r in l_r:
    l_ans = max(l_ans, l)
    r_ans = min(r_ans, r)
if r_ans < l_ans:
    ans = 0
else:
    ans = r_ans-l_ans+1
print(ans)

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

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

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

Solution (Python):

"""
Created on Fri Sep 11 01:27:46 2020

@author: liang
"""

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

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

limit_min = max(l_list)
limit_max = min(r_list)
cnt = 0

for i in range(1, n + 1):
	if limit_min <= i <= limit_max:
		cnt += 1

print(cnt)

Solution (Python):







e1,e2 = map(int,input().split())


h = []
for i in range(e2):
    h.append(list(map(int,input().split())))
L=[]
R=[]
for i in range(len(h)):
    L.append(h[i][0])
for i in range(len(h)):
    R.append(h[i][1])

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

Solution (Python):
N,M=map(int,input().split())
l=[]
r=[]
for i in range(M):
    L,R=map(int,input().split())
    l.append(L)
    r.append(R)
a=max(l)
b=min(r)
x=list(range(a,b+1))
print(len(x))

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())
s=0
t=10**10
for i in range(m):
  l,r=map(int,input().split())
  s=max(s,l)
  t=min(t,r)

if s>t:
  print(0)
else:
  print(t-s+1)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m = map(int, input().split())
l_key = []
r_key = []
for i in range(m):
    l, r = map(int, input().split())
    l_key.append(l)
    r_key.append(r)

print(max(0, min(r_key) - max(l_key) + 1))

Solution (Python):
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())


N,M = MI()
A = [0]*(N+2)

for i in range(M):
    l,r = MI()
    A[l] += 1
    A[r+1] -= 1

from itertools import accumulate

A = list(accumulate(A))
print(sum(A[i] == M for i in range(1,N+1)))

Solution (Python):
'''
Created on 2020/09/08

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  
  N,M=map(int,pin().split())
  L,R=map(int,pin().split())
  for i in range(M-1):
    l,r=map(int,pin().split())
    if L<l and l<=R:
      L=l
    elif R<l:
      print(0)
      return
    if L<=r and r<R:
      R=r
    elif r<L:
      print(0)
      return
  print(R-L+1)
  return 
main()

Solution (Python):


import numpy as np


INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n,m=INTM()
    ls=[]
    rs=[]
    for i in range(m):
        l,r=INTM()
        ls.append(l)
        rs.append(r)
    
    ans=min(rs)-max(ls)+1
    if ans<=0:
        ans=0
    print(ans)



    
if __name__=='__main__':
    do()

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

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

ans = [1]*n

s = 0
m = n

for i in list:
    if i[0] > s:
        s = i[0]
    if i[1] < m:
        m = i[1]
if m>=s:
    print(m-s+1)
else:
    print(0)

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

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

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

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

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

Solution (Python):

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

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

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

max = 0
min = n+1
for i in range(m):
    if lr[i][0] > max :
        max = lr[i][0]
    if lr[i][1] < min :
        min = lr[i][1]

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

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

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

Solution (Python):
N,M=map(int,input().split())
temp=0
TEMP=N-1
for i in range(M):
    a,b=map(int,input().split())
    if b-1<temp or TEMP<a-1:
        print(0)
        exit()
    if a-1>temp:
        temp=a-1
    if TEMP>b-1:
        TEMP=b-1
    
print(TEMP-temp+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)

L_max = 0
R_min = 100000

for i in range(M):
	L_max = max(L_max, L[i])
	R_min = min(R_min, R[i])

cnt = R_min - L_max + 1

if cnt < 0:
	print("0")
else:
	print(cnt)

Solution (Python):
n, m = map(int, input().split())
a = []
for i in range(m):
    a.append(list(map(int, input().split())))
haba = a[0]
for i in a:
    if haba[0] <= i[0] <= haba[1]:
        haba[0] = i[0]
    elif haba[1] < i[0]:
        print(0)
        exit()
    if haba[0] <= i[1] <= haba[1]:
        haba[1] = i[1]
    elif i[1] < haba[0]:
        print(0)
        exit()
if n < haba[0]:
    ans = 0
elif haba[0] <= n <= haba[1]:
    ans = n - haba[0] + 1
else:
    ans = haba[1] - haba[0] + 1
print(ans)

Solution (Python):
n, m = map(int, input().split())
a = []
for i in range(m):
    a.append(input().split())
haba = a[0]
for i in a:
    if int(haba[0]) <= int(i[0]) <= int(haba[1]):
        haba[0] = i[0]
    elif int(haba[1]) < int(i[0]):
        print(0)
        exit()
    if int(haba[0]) <= int(i[1]) <= int(haba[1]):
        haba[1] = i[1]
    elif int(i[1]) < int(haba[0]):
        print(0)
        exit()
if n < int(haba[0]):
    ans = 0
elif int(haba[0]) <= n <= int(haba[1]):
    ans = n - int(haba[0]) + 1
else:
    ans = int(haba[1]) - int(haba[0]) + 1
print(ans)

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

max_l, min_r = 0, N

for l, r in LR:
    max_l = max(l, max_l)
    min_r = min(r, min_r)
    if min_r < max_l:
        print(0)
        exit()

print(min_r - max_l + 1)

Solution (Python):

import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

if __name__ == '__main__':
    main()

Solution (Python):
[n,m] = [int(i) for i in input().split()]
to_acc = [0] * (n+1)
for i in range(m):
  [l,r] = [int(j)-1 for j in input().split()]
  to_acc[l] += 1
  to_acc[r+1] -= 1
acc = [0]
ans = 0
for i in range(n+1):
  acc.append(acc[-1] + to_acc[i])
for i in range(1,n+1):
  if m == acc[i]:
    ans += 1
print(ans)

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

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

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

mn = 0
mx = 10**6

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

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

Solution (Python):
r=input().split()
N=int(r[0])
M=int(r[1])
data=[[int(s) for s in input().split()] for i in range(M)]
left=[]
right=[]
for i in range(M):
    left.append(data[i][0])
    right.append(data[i][1])
if min(right)-max(left)+1<=0:
    print(0)
else:
    print(min(right)-max(left)+1)

Solution (Python):
def main():
    x = list(map(int, input().split()))
    n = x[0]
    m = x[1]

    common_section = [1, n]

    for i in range(m):
        a = list(map(int, input().split()))
        if a[0] > common_section[0]:
            common_section[0] = a[0]
        if a[1] < common_section[1]:
            common_section[1] = a[1]

    answer = common_section[1] - common_section[0] + 1
    if answer < 0:
        answer = 0
    print(answer)


if __name__ == "__main__":
    main()

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

n = 0
m = 999999

for i in range(M):
  a,b = map(int, input().split())
  n = max(a, n)
  m = min(b , m)

print(max(m-n+1, 0))

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

n = 0
m = 999999

for i in range(M):
  a,b = map(int, input().split())
  n = max(a, n)
  m = min(b , m)

print(m-n+1 if (m >= n) else 0)

Solution (Python):
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n, m = Input()
    max_l = 0
    min_r = 1e6
    for i in range(m):
        l, r = Input()
        max_l = max(max_l, l)
        min_r = min(min_r, r)

    ans = min_r - max_l + 1
    ans = max(ans, 0)
    print(ans)

main()

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):
n,m=map(int,input().split())
lb=0
ub=n
for i in range(m):
  l,r=map(int,input().split())
  lb=max(lb,l)
  ub=min(ub,r)
  
if ub>=lb:
  print(ub-lb+1)
else:
  print(0)

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

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

now = 0
ans = 0
for i in range(1, n+1):
    now += g[i]
    if now >= m:
        ans += 1

print(ans)

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

max_left=0
min_right=N

for i in range(M):
    if s[i][0]>max_left:
        max_left=s[i][0]
    if s[i][1]<min_right:
        min_right=s[i][1]
        
cnt=0
for j in range(max_left,min_right+1):
    cnt+=1
print(cnt)

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

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

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

N, M = MI()
gate = [0] * (N + 2)
ans = 0

for _ in range(M):
    L, R = MI()
    gate[L] += 1
    gate[R + 1] -= 1

for i in range(1, N + 1):
    gate[i] += gate[i - 1]
    
for i in range(1, N + 1):
    if gate[i] == M:
        ans += 1
        
print(ans)