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):
def enter_card(L, R, f_point, e_point):
    if f_point < L:
        f_point = L
    if e_point > R:
        e_point = R
    return f_point, e_point

def open_list(L, R):
    if L > R:
        return 0
    else:
        return R - L + 1
   

N, M = map(int, input().split(' '))
f_point = 1
e_point = N
for i in range(M):
    L,  R = map(int, input().split(' '))
    f_point, e_point = enter_card(L, R, f_point, e_point)
a = open_list(f_point, e_point)
print(a)

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

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

left = 1
right = N



for _ in range(M):
    L, R = map(int, input().split())
    if L > left:
        left = L
    if R < right:
        right = R
    if right - left + 1 <= 0:
        break
print(max(0, right - left + 1))

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


class CountedAcID():
    def __init__(self, gates):
        self.gates = gates
        self.scope = [1, N]

    def run(self):
        self.__get_range()
        self.total = self.__return_num()


    def __get_range(self):
        for element in self.gates:
            self.scope[0] = self.__get_left(element[0], self.scope[0])
            self.scope[1] = self.__get_right(element[1], self.scope[1])

    def __get_left(self, element, left):
        if element > left:
            return element
        else:
            return left

    def __get_right(self, element, right):
        if element < right:
            return element
        else:
            return right

    def __return_num(self):
        if self.scope[1] > N:
            return N - self.scope[0] + 1

        else:
            return self.scope[1] - self.scope[0] + 1


c = CountedAcID(gates)
c.run()
num = c.total

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

Solution (Python):



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





scope = [1, n]

for element in gates:
    """
    ここで範囲の左右を取得する
    Lは0番目の最大値
    Rは1番目の最小値を検索する
    """

    if element[0] > scope[0]:
        scope[0] = element[0]

    if element[1] < scope[1]:
        scope[1] = element[1]

"""
範囲の最大値がnを超えていた場合について
最小値~nまでの範囲で処理を行う

超えていない場合について
普通にR-Lを行う
"""


def return_num():
    if scope[1] > n:
        return n - scope[0] + 1

    else:
        return scope[1] - scope[0] + 1


num = return_num()

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

Solution (Python):
def main():
    n = [int(i) for i in input().split()]
    mi = []
    ma = []
    for i in range(n[1]):
        j = [int(i) for i in input().split()]
        mi.append(j[0])
        ma.append(j[1])
    ans = min(ma)-max(mi)+1
    if ans < 0:
        ans = 0
    print(ans)


if __name__ == "__main__":
    main()

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

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

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


print(max(intv[1]-intv[0]+1,0))

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

li_w = list()

for _ in range(m):
    li_w.append(tuple(map(int, input().split())))

w_l = 0
w_r = 999999

for w in li_w:
    w_l = max(w_l, w[0])
    w_r = min(w_r, w[1])


if w_r >= w_l:
    print(w_r - w_l + 1)
else:
    print(0)

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

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

for i in range(m):
    l, r = map(int, input().split())
    if i == 0:
        l_max = l
        r_min = r
    else:
        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())
l = [-1]*(m)
r = [-1]*m
for i in range(m):
    a,b = map(int,input().split())
    l[i]=a
    r[i]=b
right = n
left = 0 


count = 0
k = 0
for i in range(m):
    right = min(r[i],right)
    left = max(l[i],left)

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

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

Left = -1
Right = n + 1
for i in range(m):
    l, r = map(int, input().split())
    Left = max(Left, l)
    Right = min(Right, r)
if Left <= Right:
    ans = Right - Left + 1
else:
    ans = 0
print(ans)

Solution (Python):
n,m = map(int, input().split())
left = 0
right = 10**5
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())
my_list = []
for i in range(M):
    line = input().split()
    my_list.append((int(line[0]), int(line[1])))

max_l = 0
min_r = 10 ** 6

for l,r in my_list:
    max_l = max(max_l, l)
    min_r = min(min_r, r)
print(max(0, min_r - max_l + 1))

Solution (Python):
from itertools import accumulate
from collections import Counter


def solve(string):
    n, m, *lr = map(int, string.split())
    t = [0] * (n + 2)
    for l, r in zip(*[iter(lr)] * 2):
        t[l] += 1
        t[r + 1] -= 1
    *t, = accumulate(t)
    c = Counter(t)
    return str(c[m]) if m in c.keys() else "0"


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(m)])))

Solution (Python):
N, M = map(int, input().split())
LN, RN = 0, N
for _ in range(M):
    L, R = map(int, input().split())
    LN = max(LN, L)
    RN = min(RN, R)
ans = RN - LN + 1
if ans < 0:
    ans = 0
print(ans)

Solution (Python):
import sys
import numpy as np

def main(lines):
  N, M = np.fromstring(lines[0], dtype=int, sep=" ")
  a = np.fromstring(''.join(lines[1:]), dtype=int, sep=" ").reshape((2, M), order='F')
  
  lmax = a[0].max()
  rmin = a[1].min()
  if lmax > rmin:
    print(0)
  else:
    print(rmin - lmax + 1)

lines = sys.stdin.readlines()
main(lines)

Solution (Python):
s=list(map(int,input().split()))
t=[list(map(int,input().split())) for i in range(s[1])]
a=0
b=100001
for i in range(s[1]):
  if a<t[i][0]:
    a=t[i][0]
  if b>t[i][1]:
    b=t[i][1]
if b-a>=0:
  print(b-a+1)
else:
  print(0)

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

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

if m==1:
  print(L[0][1]-L[0][0]+1)
else:
  l=[]
  r=[]
  for item in L:
    l.append(item[0])
    r.append(item[1])
  x=max(l)
  y=min(r)
  if y>=x:
    print(y-x+1)
  else:
    print(0)

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

master_l, master_r = -1, N
for l, r in LR:
	master_l = max(master_l, l)
	master_r = min(master_r, r)

print(max(0, master_r - master_l + 1))

Solution (Python):
import sys

input = sys.stdin.readline

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

l_list = []
r_list = []

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

l_maybe_answer = max(l_list)
r_maybe_answer = min(r_list)

if l_maybe_answer <= r_maybe_answer:
    print(min(r_list) - max(l_list) + 1)
else:
    print(0)

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

imos = [0] * (N+1) 
for l,r in LR:
    imos[l-1] += 1
    imos[r] -= 1

for i in range(N):
    imos[i+1] += imos[i]
print(imos.count(M))

Solution (Python):
N, M = map(int, input().split())
ans = set(range(1, N+1))
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(0, r_min - l_max + 1))

Solution (Python):
import numpy as np

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

l = np.zeros(m, dtype=np.int)
r = np.zeros(m, dtype=np.int)

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


x = max(r.min() - l.max() + 1, 0)

print(x)

Solution (Python):
N,M = (int(x) for x in input().split())
Lmax = 0
Rmin = N
for i in range(M):
  Li,Ri = (int(x) for x in input().split())
  if Lmax < Li:
    Lmax = Li
  if Rmin > Ri:
    Rmin = Ri
answer = Rmin - Lmax + 1
if answer < 0:
  answer = 0
print(answer)

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

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

Solution (Python):
n,m = map(int, input().split())
ans = 0
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 = map(int, input().split())
max_L = 1
min_R = N

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

Solution (Python):
N,M = map(int,input().split())
imos = [0]*(N+1)
for i in range(M):
    l,r = map(int,input().split())
    imos[l-1] += 1
    imos[r] -= 1
from itertools import accumulate
imos = list(accumulate(imos))
res = 0
for a in imos:
    if a == M:
        res += 1
print(res)

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

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

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

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

Solution (Python):
n,m=map(int,input().split())
l,r=map(int,input().split())
s=[l,r]
for i in range(1,m):
    l,r=map(int,input().split())
    if s[0]<l:
        s[0]=l
    if s[1]>r:
        s[1]=r

    if s[1]-s[0]<0:
        s=[0,-1]
        break
print(s[1]-s[0]+1)

Solution (Python):
N, M = [int(i) for i in input().split()]
cards = [0] * (N + 1)

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

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

print(len([i for i in cards if i == M]))

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

Lmax = max(L[i] for i in range(M))
Rmin = min(R[i] for i in range(M))

if Lmax>Rmin:
  print('0')
else:
  print(str(Rmin-Lmax+1))

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

lmax = 0
rmin = n+1

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(rmin-lmax+1)
else:
    print(0)

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

left_max, right_min = map(int, input().split())
for m in range(M-1):
    L, R = map(int, input().split())
    left_max = max(L, left_max)
    right_min = min(R, right_min)

if left_max <= right_min:
    print(right_min-left_max+1)
else:
    print(0)

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

for  i  in  range(m):
    nums.append([int(s)  for  s  in  input().split()])
for  j  in  range(m):
    left.append(nums[j][0])
    right.append(nums[j][1])
if  min(right)-max(left)>=0:
    print(min(right)-max(left)+1)
else:
    print(0)

Solution (Python):
maxi =[]
mini =[]
n,m =map(int, input().split())
for i in range(m):
    k =[int(s) for s in input().split()]
    mini.append(min(k))
    maxi.append(max(k))
if min(maxi)- max(mini) >= 0:
    print(min(maxi)-max(mini)+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)
n_all = min(rs) - max(ls) + 1
if n_all < 0: n_all =0
print(n_all)

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

Solution (Python):
import math

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

    A, B = map(int, input().split())

    for i in range(1, M):
        a, b = map(int, input().split())
        A = max(A, a)
        B = min(B, b)

    print(max(B - A + 1, 0))

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

card_list = [1] * n

minr = 10**9
maxl = 0

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

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

l=[]
r=[]

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

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

Solution (Python):

import sys
import math
import numpy as np
import itertools



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



lr = [[int(i) for i in input().split()] for i in range(m)] 

l=1
r=n
for i in range(m):
    l=max(l,lr[i][0])
    r=min(r,lr[i][1])



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

Solution (Python):
a, b = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(b)] 
tmin = e[0][0]
tmax = e[0][1]
for i, l in enumerate(e):
  if (tmin < l[0]):
    tmin = l[0]
  if (tmax > l[1]):
    tmax = l[1]
  if (tmax - tmin) < 0:
    break
if (tmax-tmin < 0):
  print(0)
else:
  print(tmax-tmin+1)

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

min_list = []
max_list = []
for i in range(m):
  min_num, max_num = map(int,input().split())
  min_list.append(min_num)
  max_list.append(max_num)

min_num = max(min_list)
max_num = min(max_list)

ans = max_num - min_num + 1

if ans < 0:
  ans = 0

print(ans)

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

card_list = [1] * n

minr = 10**9
maxl = 0

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

Solution (Python):
n,m = map(int,input().split())
L = []
ans = 0
for i in range(m):
  L.append(list(map(int,input().split())))
imos = [0]*(n+1)
for i in range(m):
  k = L[i][0]
  l = L[i][1]
  imos[k-1] += 1
  imos[l] -= 1
for i in range(1,n+1):
  imos[i] += imos[i-1]
for i in range(n+1):
  if imos[i] == m:
    ans += 1
print(ans)

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

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

A, B = 1, N
for lr in LR:
    L, R = lr
    if R < A or B < L:
        print(0)
        exit()

    if A <= L <= R <= B:
        A, B = L, R
    elif L <= A <= R <= B:
        B = R
    elif A <= L <= B <= R:
        A = L
print(B - A + 1)

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

A, B = 1, N
for lr in LR:
    L, R = lr
    if R < A or B < L:
        print(0)
        exit()

    if A <= L <= R <= B:
        A, B = L, R
    elif L <= A <= R <= B:
        B = R
    elif A <= L <= B <= R:
        A = L
print(B - A + 1)

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

A, B = 1, N
for lr in LR:
    L, R = lr
    if R < A or B < L:
        print(0)
        exit()

    if A <= L <= R <= B:
        A, B = L, R
    elif L <= A <= R <= B:
        B = R
    elif A <= L <= B <= R:
        A = L
print(B - A + 1)

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

A, B = 1, N
for lr in LR:
    L, R = lr
    if R < A or B < L:
        print(0)
        exit()

    if A <= L <= R <= B:
        A, B = L, R
    elif L <= A <= R <= B:
        B = R
    elif A <= L <= B <= R:
        A = L
print(B - A + 1)

Solution (Python):
N,M = map(int,input().split())
l = []
r = []
for m in range(M):
  lr1,lr2 = map(int,input().split())
  l.append(lr1)
  r.append(lr2)
print(max(0,min(r) - max(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 = max(L)
r_min = min(R)

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

Solution (Python):

n, g = map(int, input().split())
cards = [[int(x) for x in input().split()] for _ in range(g)]

cards.sort(key=lambda x: x[0])
lmx = cards[g - 1][0]
cards.sort(key=lambda x: x[1])
rmn = min(cards[0][1], n)

print(max(0, rmn - lmx + 1))

Solution (Python):
import sys

n, g = map(int, input().split())
cards = [[int(x) for x in sys.stdin.readline().rstrip().split()] for _ in range(g)]

cards.sort(key=lambda x: x[0])
lmx = cards[g - 1][0]
cards.sort(key=lambda x: x[1])
rmn = min(cards[0][1], n)

print(max(0, rmn - lmx + 1))

Solution (Python):
import sys
import operator

n, g = map(int, input().split())
cards = [[int(x) for x in sys.stdin.readline().rstrip().split()] for _ in range(g)]

cards.sort(key=operator.itemgetter(0))
lmx = cards[g - 1][0]
cards.sort(key=operator.itemgetter(1))
rmn = min(cards[0][1], n)

print(max(0, rmn - lmx + 1))

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

l_max = 0
r_min = N

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

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

Solution (Python):
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
data = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
min_id =0
max_id = 100000
for i in range(M):
  min_id = max(min_id,data[i][0])
  max_id = min(max_id,data[i][1])
num = max_id - min_id + 1 if max_id - min_id >= 0 else 0
print(num)

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

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

Solution (Python):
N,M = map(int,input().split())
pool = []
for i in range(M):
    i_1,i_2 = map(int,input().split())
    pool.append([i_1,i_2])
min_val = max([int(pool[i][0]) for i in range(M)])
max_val = min([int(pool[i][1]) for i in range(M)])
if min_val > max_val:
    print(0)
else:
    print(max_val-min_val+1)

Solution (Python):
N, M = map(int, input().split())
l_max = 0
r_min = 100001

for i in range(M):
  l, r = map(int, input().split())
  if l > l_max:
    l_max = l
  if r < r_min:
    r_min = r
    
mylist = list(range(l_max,r_min+1))

print(len(mylist))

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

left = 1
right = N
for i in range(M):
    if left<List[i][0]:
        left = List[i][0]
    if List[i][1]<right:
        right = List[i][1]

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

Solution (Python):

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


LR=[]
for i in range(M):
        LR.append(input().split())


numbers=[]
for i in range(N+1):
    numbers.append(i)




Lmax=1
Rmax=100000
for j in LR:

    L=int(j[0])
    R=int(j[1])

    if Lmax<L:
        Lmax=L
    if Rmax>R:
        Rmax=R
result=0

for i in numbers:

    if i>=Lmax:

        if i<=Rmax:
            result+=1



print(result)

Solution (Python):


input_line = input()
N,M = [int(s) for s in input_line.split(' ') if s]
Ls = []
Rs = []
for _ in range(M):
    input_line = input()
    L,R = [int(s) for s in input_line.split(' ')if s]
    Ls.append(L)
    Rs.append(R)

output = min(Rs)-max(Ls)+1
output = output if output > 0 else 0
print(output)

Solution (Python):

"""
Created on Thu Jun  6 21:15:08 2019

@author: hisaki
"""

import numpy as np

N,M = map(int,input().split())
L_array = np.zeros(M)
R_array = np.zeros(M)
for i in range(M):
    L,R = map(int,input().split())
    L_array[i] = L
    R_array[i] = R
    
L_max = np.max(L_array)
R_min = np.min(R_array)

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

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

S = [0] * (int(1e5) + 11)
for i in range(len(L)):
    S[i + 1] = S[i] + L[i]

ans = 0
for s in S:
    if s == m : ans += 1

print(ans)

Solution (Python):


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

L = 0
R = N

for i in xrange(M):
	li,ri = map(int,raw_input().split())
	L = max(L,li)
	R = min(R,ri)

print max(0,R-L+1)

Solution (Python):
import sys

N, M = map(int, input().split())
card = [1, N]
for i in range(M):

    gate = [int(x) for x in input().split()]
    card[0] = max(card[0], gate[0])
    card[1] = min(card[1], gate[1])
    if(card[0] > card[1]):
        print(0)
        sys.exit()
        
print(card[1] - card[0] + 1)

Solution (Python):

N, M = map(int, input().split())
L, R = (
    zip(*(map(int, input().split()) for _ in range(M))) if M else
    ((), ())
)

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


print(ans)

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

Solution (Python):
N, M = map(int, input().split())
Doors = [list(map(int, input().split())) for _ in range(M)]
IDs = [0 for _ in range(N+1)]

for Door in Doors:
    IDs[Door[0] - 1] += 1
    IDs[Door[1]]     -= 1
   

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

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

Solution (Python):
N,M = map(int,input().split())
gateid = [list(map(int,input().split())) for i in range(M)]
idleft = max(gateid,key=lambda x:x[0])
idright = min(gateid,key=lambda x:x[1])
ans = idright[1]-idleft[0]+1
if ans<0:
  ans = 0
print(ans)

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

lmax = max(gates, key=lambda x: x[0])[0]
rmin = min(gates, key=lambda x: x[1])[1]

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

Solution (Python):
from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]
G = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
L = [G[k][0] for k in range(M)]
R = [G[k][1] for k in range(M)]
L.sort(reverse=True)
R.sort()
print(max(R[0] - L[0] + 1, 0))

Solution (Python):
def main():
    NM = list(map(int, input().split()))
    N, M = NM[0], NM[1]
    LR = []
    L, R = [], []
    ans = 0
    maxL = 1
    minR = N

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

    for j in range(M):
        L.append(LR[j][0])
        R.append(LR[j][1])

    for k in range(M):
        maxL = max(maxL, L[k])
        minR = min(minR, R[k])

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

if __name__ == "__main__":
    main()

Solution (Python):
N,M=map(int,input().split())
LL=[]
LR=[]
for i in range(M):
    LLR=list(map(int,input().split()))
    LL.append(LLR[0])
    LR.append(LLR[1])
ans=max(min(LR)-max(LL)+1,0)
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)
a = min(R)-max(L)+1
if a>=1:
  print(a)
else:
  print(0)

Solution (Python):
def resolve():
    (n, m) = list(map(int, input().split(" ")))
    
    for i in range(m):
        o = list(map(int, input().split(" ")))
        if i == 0:
            lmax = o[0]
            rmin = o[1]
        else:
            lmax = max(lmax, o[0])
            rmin = min(rmin, o[1])

    print(rmin-lmax+1 if lmax <= rmin else 0)


if '__main__' == __name__:
    resolve()

Solution (Python):
import sys
from fractions import gcd
from functools import reduce
from itertools import combinations

import numpy as np

sys.setrecursionlimit(200000)


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


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def is_MM(s):
    num = int(s)
    return 0 < num <= 12


def solve():
    N, M = il()
    L = []
    R = []
    for m in range(M):
        lr = il()
        L.append(lr[0])
        R.append(lr[1])
    return max(0, min(R) - max(L) + 1)


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

Solution (Python):
import sys
from fractions import gcd
from functools import reduce
from itertools import combinations

import numpy as np

sys.setrecursionlimit(200000)


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


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def is_MM(s):
    num = int(s)
    return 0 < num <= 12


def solve():
    N, M = il()
    L_max = 0
    R_min = float("inf")
    for m in range(M):
        lr = il()
        L_max = max(L_max, lr[0])
        R_min = min(R_min, lr[1])
    return R_min - L_max + 1 if L_max <= R_min else 0


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

Solution (Python):
import sys


scan = sys.stdin.readline


def main():
  n, m = map(int, scan().rstrip().split())
  l = 0
  r = n
  
  for _ in range(m):
    l_i, r_i = map(int, scan().rstrip().split())
    l = max(l, l_i)
    r = min(r, r_i)
    
  cards = r - l + 1 if r >= l else 0
  
  print(cards)
  

if __name__ == "__main__":
  main()

Solution (Python):
N, M = [int(i) for i in input().split()]
gate_idx = [[int(i) for i in input().split()] for M in range(M)]
import numpy as np
l_max = np.max(gate_idx, axis=0)
r_min = np.min(gate_idx, axis=0)
ans = r_min[1] - l_max[0] + 1
if ans < 0: ans = 0
print(ans)

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

Solution (Python):

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

Solution (Python):
n,m_=map(int,input().split())
m=0
M=9**9
for i in range(m_):
    l,r= map(int, input().split())
    m=max(m,l)
    M=min(M,r)
print(0if M<m else M-m+1)

Solution (Python):
def main():
    n, m = [int(i) for i in input().split()]
    l_max, r_min = 0, 10**6
    for i in range(m):
        l, r = [int(j) for j in input().split()]
        l_max = max(l, l_max)
        r_min = min(r+1, r_min)
    if r_min-l_max <= 0:
        print(0)
    else:
        print(r_min-l_max)


if __name__ == '__main__':
    main()

Solution (Python):


if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    l = []
    r = []
    for _ in range(m):
        a, b = list(map(int, input().split()))
        l.append(a)
        r.append(b)

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

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

Solution (Python):
import sys
     
def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]
   
    under_limit = [x[0] for x in input]
    over_limit = [x[1] for x in input]
     
    print(max(min(over_limit) - max(under_limit) + 1, 0))
     
     
if __name__ == "__main__":
    main()

Solution (Python):
import sys

def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]

    card = [1, num_card]

    for gate in input:
        card[0] = max(card[0], gate[0])
        card[1] = min(card[1], gate[1])

    print(max(card[1] - card[0] + 1, 0))


if __name__ == "__main__":
    main()

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

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

l_max = 0
r_min = float('inf')
for _ in range(m):
  l, r = [int(i) for i in input().split()]
  if l > l_max:
    l_max = l
  if r < r_min:
    r_min = r

ans = r_min - l_max + 1

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

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

l = []
r = []
for _ in range(m):
  l_tmp, r_tmp = [int(i) for i in input().split()]
  l.append(l_tmp)
  r.append(r_tmp)
  
l_max = max(l)
r_min = min(r)

ans = r_min - l_max + 1

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

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