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

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

Solution (Python):
import sys


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

max_l = 0
min_r = sys.maxsize

for _ in range(M):
  l, r = map(int, input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
  
print(sum(
  max_l <= i <= min_r
  for i in range(1, N + 1)
))

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

l_list = [0] * m
r_list = [0] * m
for i in range(m):
    l_list[i], r_list[i] = [int(x) for x in input().split()]

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

Solution (Python):


import sys



















def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n, m = mi()
    l_list = []
    r_list = []
    for _ in range(m):
        l, r = mi()
        l_list.append(l)
        r_list.append(r)
    print(max(min(r_list)-max(l_list)+1, 0))

if __name__ == "__main__":
    main()

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

left = 1
right = N

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

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

Solution (Python):

N, M = map(int, input().split())
L = []
R = []
for i in range(M):
    l, r= map(int, input().split())
    L.append(l)
    R.append(r)
Lmax = max(L)
Rmax = min(R)
ans = Rmax-Lmax+1
if ans < 0:
  ans = 0
print(ans)

Solution (Python):
n,m=map(int,input().split())
p=1
q=n
for i in range(m):
  l,r=map(int,input().split())
  if p<l:
    p=l
  if r<q:
    q=r
  if p>q:
    print(0)
    exit()
print(q-p+1)

Solution (Python):
n, m = map(int, input().split())
li = [list(map(int, input().split())) for _ in range(m)] 
l_li = []
r_li = []
for l, r in li:
    l_li.append(l)
    r_li.append(r)
print(max(min(r_li) - max(l_li) +1, 0))

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)
a = max(l)
b = min(r)
print(max(b - a + 1, 0))

Solution (Python):

import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
l_list = []
r_list = []
for i in range(M):
    l, r = lr()
    l_list.append(l)
    r_list.append(r)

for i in range(M):
    if i == 0:
        maxL = l_list[i]
        minR = r_list[i]
        continue
    maxL = max(maxL, l_list[i])
    minR = min(minR, r_list[i])
ans = max(minR - maxL + 1, 0)
print(ans)

Solution (Python):

import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
l_list = []
r_list = []
for i in range(M):
    l, r = lr()
    l_list.append(l)
    r_list.append(r)




tl_list = []
tr_list = []
for i in range(M):
    tmp_left = l_list[i]
    tmp_right = r_list[i]
    tl_list.append(tmp_left)
    tr_list.append(tmp_right)
left = max(tl_list)
right = min(tr_list)








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

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

l,r=m[0][0],m[0][1]

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

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

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

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

Solution (Python):
N, M = map(int, input().split())
maxL = 0
minR = float('inf')
for i in range(M):
    L, R = map(int, input().split())
    maxL = max(maxL, L)
    minR = min(minR, R)
if minR - maxL >= 0:
    print(minR - maxL + 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)

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

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

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

lm,rm = 0,N
for i in range(0,M):
    l,r = map(int,input().split())
    lm = max(lm,l)
    rm = min(rm,r)
print(rm - lm + 1 if rm >= lm else 0)

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



ml = -1
mr = 10**6
for i in range(m):
  l,r = map(int,input().split())
  ml = max(ml,l)
  mr = min(mr,r)
if mr < ml :
  res = 0
else:
  res = mr-ml + 1

print(res)

Solution (Python):
import sys
N, M = map(int, sys.stdin.readline().split())
LR = sorted([list(map(int,sys.stdin.readline().split())) for i in range(M)])
L = 1
R = 10 ** 5
for i in LR:
    L = max(L, i[0])
    R = min(R, i[1])
if R - L >= 0:
    print(R - L + 1)
else:
    print(0)

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

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

Solution (Python):
N,M=map(int,input().split())
LR=[]
for i in range(M):
  LR.append(list(map(int,input().split())))
l,r=LR[0][0],LR[0][1]
for i in range(M):
  if LR[i][1]<=r:
    r=LR[i][1]
  if l<=LR[i][0]:
    l=LR[i][0]
ans=r-l+1
if ans>0:
  print(ans)
else:
  print(0)

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

a = 1
b = 10**5

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

    if r < b:
        b = r

if a <= b:
    print(b-a+1)

else:
    print(0)

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

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

ml = 0
mm = 10**7
for v in L:
    ml = max(ml, v[0])
    mm = min(mm, v[1])

if (mm - ml + 1 <= 0):
    print(0)
    exit()
print(mm - ml + 1)

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


left = 0
right = float('inf')
for _ in range(m):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)

ans = 0
for i in range(1, n + 1):
    if left <= i <= right:
        ans += 1
print(ans)

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

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

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

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

Solution (Python):


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

ansl = 1
ansr = n

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

print(max(ansr-ansl+1,0))

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

Solution (Python):

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

lmax = -1
rmin = n * 100
for i in range(m):
    l, r = map(int, input().split())
    lmax = max(l, lmax)
    rmin = min(r, rmin)

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

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

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

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())
  if L > L_MAX:
    L_MAX = L
  if R < R_MIN:
    R_MIN = R

Ans=max(R_MIN - L_MAX + 1, 0)
print(Ans)

Solution (Python):
n, m = map(int, input().split())
l_lst = [0] * m
r_lst = [0] * m
for i in range(m):
    l, r = map(int, input().split())
    l_lst[i] = l
    r_lst[i] = r

if max(l_lst) <= min(r_lst):
    print(min(r_lst) - max(l_lst) + 1)
else:
    print(0)

Solution (Python):
def main():
    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 Rmin < Lmax:
        print(0)
    else:
        print(Rmin - Lmax + 1)
main()

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

N, M = MAP()

LR = [LIST() for _ in range(M)]
LR = np.transpose(LR)

print(min(LR[1]) - max(LR[0]) + 1 if min(LR[1]) >= max(LR[0]) else 0)

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

Solution (Python):
N, M = map(int, input().split())
min = -1
max = 10**5+1
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,k = map(int,input().split())
rc = [list(map(int,input().split())) for i in range(k)]
transpose = [x for x in zip(*rc)]

ans = min(transpose[1]) - max(transpose[0])

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

Solution (Python):
n,m = map(int,input().split())
l_max,r_min = 0,n
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):
N,M=map(int,input().split())
l=0
r=float("Inf")
for i in range(M):
    L,R=map(int,input().split())
    l=max(l,L)
    r=min(r,R)
print(max(r-l+1,0))

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

min1=1
max1=N

for j in gate:
  if j[0]>min1:
    min1=j[0]
  if j[1]<max1:
    max1=j[1]
if max1>=min1:
  print(max1-min1+1)
else:
  print(0)

Solution (Python):
def main():
    N, M = map(int, input().split())
    L, R = 0, 1001001
    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))


if __name__ == '__main__':
    main()

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

start=1
end=n
for line in input_lines:
    if line[0]>start:
        start=line[0]
    if line[1]<end:
        end=line[1]
print(len([i for i in range(start,end+1)]))

Solution (Python):
from itertools import accumulate

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

accNums = list(accumulate(nums))


ans = accNums.count(M)
print(ans)

Solution (Python):
import sys
N,M=map(int, sys.stdin.readline().split())
ll=float("-inf")
rr=float("inf")
for _ in range(M):
    l,r=map(int, sys.stdin.readline().split())
    ll=max(ll,l)
    rr=min(rr,r)

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

Solution (Python):

import io

nim,mike=map(int,input().split())

kite,qwe=1,nim

for i in range(mike):
    pop,ioi=map(int,input().split())
    kite,qwe=max(kite,pop),min(qwe,ioi)

print(max(qwe-kite+1,0))

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

card_min = 0
card_max = N
ans = N

for l, r in LR:
    if l > card_min:
        card_min = l
    if r < card_max:
        card_max = r
    
    ans = card_max - card_min + 1
    if ans <= 0:
        ans = 0
        break
print(ans)

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

while True:
  try:
    tl,tr = map(int,input().split())
    l.append(tl)
    r.append(tr)
  except:
    break

R = min(r)
L = max(l)
cnt = 0
for i in range(1,n+1):
  if i>=L and i<=R:
    cnt +=1
    
print(cnt)

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

while True:
  try:
    tl,tr = map(int,input().split())
    l.append(tl)
    r.append(tr)
  except:
    break

R = min(r)
L = max(l)
cnt = 0
for i in range(1,n+1):
  if i>=L and i<=R:
    cnt +=1
    
print(cnt)

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

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

for i in range(m-1):
    l,r=map(int,input().split())
    if r<L:
        L,R=0,-1
        break
    if R<l:
        L,R=0,-1
        break
    L=max(L,l)
    R=min(R,r)
print(R-L+1)

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

Solution (Python):
N, M=map(int, input().split())
w=[list(map(int, input().split())) for i in range(M)]
R_min=10**6
L_max=0
cnt=0

for i in range(M):
    if w[i][1]<=R_min:
        R_min=w[i][1]

for i in range(M):
    if w[i][0]>=L_max:
        L_max=w[i][0]

for num in range(1, N+1):
    if L_max<= num <= R_min:
        cnt+=1
print(cnt)

Solution (Python):
import sys
import numpy as np

n, m, *lr = map(int, sys.stdin.read().split())
l, r = np.array(lr).reshape(-1, 2).T

def main():
    res = max(0, r.min() - l.max() + 1)
    print(res)

if __name__ ==  '__main__':
    main()

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

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

ma = 0
mi = float("INF")
for i in range(0,m):
    ma = max(ma,A[i][0])
    mi = min(mi,A[i][1])

ans = mi-ma+1
if ans >0:
  print(ans)
else :
  print(0)

Solution (Python):
n,m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(m)]
l_max = 0
r_min = 100000000
for i in range(m):
    if (l_max < a[i][0]):
        l_max = a[i][0]
for i in range(m):
    if (r_min > a[i][1]):
        r_min = a[i][1]
print(max(r_min-l_max+1, 0))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

import itertools

n, m = map(int, readline().split())
lr = [list(map(int, readline().split())) for i in range(m)]
memo = [0] * (n + 1)
for l, r in lr:
    memo[l - 1] += 1
    memo[r] -= 1
cumsum = list(itertools.accumulate(memo))
cnt = 0
for i in range(n):
    if cumsum[i] == m:
        cnt += 1
print(cnt)

Solution (Python):
N,M = [int(i) for i in input().split(" ")]
ans_L = 0
ans_R = 1e5+1

for _ in range(M):
    L,R = [int(i) for i in input().split(" ")]
    if ans_L < L:
        ans_L = L
    if ans_R > R:
        ans_R = R

if (ans_R - ans_L) >= 0:
    print(ans_R - ans_L + 1)
else:
    print(0)

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

low = 0
high = 10 ** 5 + 1
for i in range(m):
    ll,rr = map(int,input().split())
    
    low = max(low,ll)
    high = min(high,rr)

if high>=low:
    print(high-low+1)
else:
    print(0)

Solution (Python):
n,m = map(int,input().split())
l = []
r = []
low = 0
high = 10 ** 5 + 1
for i in range(m):
    ll,rr = map(int,input().split())
    l.append(ll)
    r.append(rr)
    low = max(low,ll)
    high = min(high,rr)

if high>=low:
    print(high-low+1)
else:
    print(0)

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

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

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

left = 0
right = n

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

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

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

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)

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

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

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

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

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

L = [L_R[i][0] for i in range(M)]
R = [L_R[i][1] for i in range(M)]

L_max = max(L)
R_min = min(R)

if L_max <= R_min:
    ans = R_min - L_max + 1
else:
    ans = 0

print(ans)

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

l = []
r = []

for i in range(m):
  l.append(a[i][0])
  r.append(a[i][1])

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

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

if __name__ == "__main__":
    main()

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

tmp1, tmp2 = 0, 10**5

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


if tmp2-tmp1 >= 0:
    print(tmp2-tmp1+1)
else:
    print(0)

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

Solution (Python):
def myAnswer(N,M,L,R) -> int:
   MAXL=max(L)
   MINR =min(R)
   ans = [i for i in range(1,N+1) if(MAXL<= i and MINR >= i)]
   return len(ans)


def modelAnswer():
   tmp=1
def main():
   N, M = map(int,input().split())
   L =[]
   R = []
   for _ in range(M):
      l,r = map(int,input().split())
      L.append(l)
      R.append(r)
   print(myAnswer(N,M,L[:],R[:]))
   

if __name__ == '__main__':
   main()

Solution (Python):

def myAnswer(N,M,L,R) -> int:
   total = 0
   MAXL=max(L)
   MINR =min(R)
   for i in range(1,N+1):
      if(MAXL<= i and MINR >= i):
         total += 1
   return total


def modelAnswer():
   tmp=1
def main():
   N, M = map(int,input().split())
   L =[]
   R = []
   for _ in range(M):
      l,r = map(int,input().split())
      L.append(l)
      R.append(r)
   print(myAnswer(N,M,L[:],R[:]))
   

if __name__ == '__main__':
   main()

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

for i in range(M):
  lsub.append(LR[i][0])
  rsub.append(LR[i][1])

L = max(lsub)
R = min(rsub)

ans = min(R, N) - L + 1

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

Solution (Python):
n, m = map(int, input().split())
ans = 0
l_max = 0
r_min = n+1
for i in range(m):
  l, r = map(int, input().split())
  if r < r_min:
    r_min = r
  if l > l_max:
    l_max = l
for i in range(1, n+1):
  if i >= l_max and i <= r_min:
    ans += 1
print(ans)

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

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

left = 0
right = 10**5

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

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

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

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

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

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

Solution (Python):
def main():
    ll = []
    rl = []
    n,m = map(int,input().split())
    for _ in range(m):
        l,r = map(int, input().split())
        ll.append(l)
        rl.append(r)
    l = max(ll)
    r = min(rl)
    print(max(0,r-l+1))

if __name__ == '__main__':
  main()

Solution (Python):
import bisect,collections,copy,itertools,math,numpy,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())

N,M = LI()
LR = [LI() for _ in range(M)]
a = 0
b = float('INF')
for x in LR:
    a = max(x[0],a)
    b = min(x[1],b)
print(b-a+1 if b-a+1>=0 else 0)

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

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

left = 0
right = 100000

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

result = right - left + 1

print(result if result >= 0 else 0)

Solution (Python):
n, m = [int(i) for i in input().strip().split()]
a = []
for _ in range(m):
    a.append([int(i) for i in input().strip().split()])
a.sort(key=lambda s: s[1])
result = a[0]
for i in range(1, len(a)):
    if result[1] >= a[i][0]:
        result = [max(result[0], a[i][0]), min(result[1], a[i][1])]
    else:
        result = []
        break
print(result[1] - result[0] + 1 if len(result) else 0)

Solution (Python):
from itertools import accumulate
n, m = map(int, input().split())
l = [list(map(int, input().split())) for i in range(m)]


ans = [0] * (10 ** 5 + 2)
for i, j in l:
    ans[i] += 1
    ans[j + 1] -= 1

print(list(accumulate(ans)).count(m))

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


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

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

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

if __name__ == "__main__":
    main()

Solution (Python):

import sys
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
L = [0 for i in range(M)]
R = [0 for i in range(M)]
for i in range(M):
    L[i], R[i] = [int(x) for x in stdin.readline().rstrip().split()]

ans = set()
ans1 = 0
ans2 = 10**6
for k in range(M):
    if ans2 < L[k]:
        print("0")
        sys.exit()
    if ans1 < L[k]:
        ans1 = L[k]
    if ans2 > R[k]:
        ans2 = R[k]

    
    
    
    
    
    
    
print(ans2+1-ans1)

Solution (Python):
n, m = (int(x) for x in input().split())
l = 0
r = n + 1
for i in range(m):
  ll, rr = (int(x) for x in 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= list(map(int,input().split()))
end = []
start = []
for i in range(M):
    A,B = list(map(int,input().split()))
    end.append(B)
    start.append(A)
end_min = min(end)
start_max = max(start)
if start_max > end_min:
    print(0)
else:
    print(end_min-start_max+1)

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

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

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

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

begin = 1
end = n
for i in range(m):
    a, b = map(int, input().split())
    if begin < a:
        begin = a
    if b < end:
        end = b

if begin <= end:
    print(end - begin + 1)
else:
    print(0)

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

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

    n,m=map(int,input().split())
    CARD=[0]*(n+1)
    for i in range(m):
        L,R=map(int,input().split())
        CARD[L-1]+=1
        CARD[R]-=1
    ans=0
    for i in range(n):
        if CARD[i]==m:ans+=1
        CARD[i+1]=CARD[i+1]+CARD[i]

    print(ans)

if __name__=='__main__':
    main()

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

    print(ans)

if __name__=='__main__':
    main()

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

    print(ans)

if __name__=='__main__':
    main()

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

print(ans)

Solution (Python):
def main():
    import sys

    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

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

saitei = 0
saidai = N

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

print(max(saidai-saitei+1, 0))

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

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

Solution (Python):
N,M = map(int, input().split())
L_R = []
for _ in range(M):
    L_R.append(list(map(int, input().split())))
    
l_min = 0
r_max = N
for l,r in L_R:
    if l_min <l:
        l_min = l
    if r_max > r:
        r_max =r
    

if r_max >= l_min:
    print(r_max - l_min + 1)
else:
    print(0)

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

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

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

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

res = [0]*n
for l, r in lr:
    res[l-1] += 1
    if r < n:
        res[r] -= 1
res_imos =[0]*(n+1)
for i in range(n):
    res_imos[i+1] = res_imos[i]+res[i]
ans = 0
for i in range(n):
    if res_imos[i+1]==m:
        ans+=1
print(ans)

Solution (Python):
N, M = map(int, input().split())
left, right = map(int, input().split())
for _ in range(M-1):
    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())


max_left = 0
min_right = N


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


if max_left>min_right:
    print(0)
else:
    ans = min_right - max_left + 1
    print(ans)

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