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

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

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

if b - a + 1 >= 0:
    print(b - a + 1)
else:
    print(0)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    n, m = map(int, readline().split())
    INF = int(1e5)+1
    mx = -1
    mi = INF 
    for _ in range(m):
        l, r = map(int, readline().split())
        mx = max(l, mx)
        mi = min(r, mi)
    ans = mi-mx+1
    if ans<0: 
        print(0)
        return
    print(mi-mx+1)
if __name__ == '__main__':
    main()

Solution (Python):

from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

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

    max_L = None
    min_R = None
    for i in range(M):
        L, R = map(int, input().split())
        if max_L is None or max_L < L:
            max_L = L
        if min_R is None or min_R > R:
            min_R = R

    if max_L <= min_R:
        num = min_R - max_L + 1
    else:
        num = 0
    print(num)


if __name__ == '__main__':
    main()

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


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    n, m = map(int, input().split())
    range_list = [list(map(int, input().split())) for _ in range(m)]
    min_list = [x[0] for x in range_list]
    max_list = [x[1] for x in range_list]
    min_id = max(min_list)
    max_id = min(max_list)
    answer = max_id - min_id + 1
    answer = max(answer, 0)
    print(answer)


if __name__ == '__main__':
    main()

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

arr = [0 for i in range(N+2)]

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

cnt = 0

for i in range(len(arr)):
  if arr[i] >= M:
    cnt += 1
print(int(cnt))

Solution (Python):
n, m = list(map(int, input().split()))
low = 0
high= n+1

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


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

Solution (Python):
n, m = list(map(int, input().split()))
low =[]
high=[]

for i in range(m):
  temp = list(map(int, input().split()))
  low.append(temp[0])
  high.append(temp[1])
  
maxlow= max(low)
minhigh=min(high) 

if minhigh-maxlow >= 0:
  print(minhigh-maxlow+1)
else:
  print(0)

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

for i in range(m-1):
    l_new,r_new = map(int,input().split())
    if l_new > l:
        l = l_new
    if r_new < r:
        r = r_new

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

Solution (Python):
n,m = map(int, input().split())
s = set()
l,r = -float("inf"), float("inf")
for i in range(m):
    left, right = map(int, input().split())
    if left > l:
        l = left
    if right < r:
        r = right
if l > r:
    print(0)
else:
    print(r-l+1)

Solution (Python):
n,m=map(int,input().split())
a=list(range(1,n+1))
l=0
l1=0
r=0
r1=10**10

for i in range(m):
	l,r=map(int,input().split())
	if l>l1:
		l1=l
	if r<r1:
		r1=r
b=list(range(l1,r1+1))
a=list(set(b) & set(a))
print(len(list(a)))

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

Solution (Python):
N,M = list(map(int,input().split()))
MAX = float('-inf')
MIN = float('inf')
for i in range(M):
    L,R = list(map(int,input().split()))
    MAX = max(MAX, L)
    MIN = min(MIN, R)
count = MIN - MAX + 1
if count > 0:
    print(count)
else:
    print(0)

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

Solution (Python):
from itertools import accumulate

n, m = [int(x) for x in input().split()]
a = [0 for i in range(n + 2)]
for i in range(m):
    l, r = [int(x) for x in input().split()]
    a[l] += 1
    a[r + 1] -= 1
a = list(accumulate(a))
print(a.count(m))

Solution (Python):
def resolve():
    N, M = [int(i) for i in input().split()]
    llrr = [list(map(int, input().split())) for _ in range(M)]
    maxL = 1
    minR = N
    for l, r in llrr:
        maxL = max(maxL, l)
        minR = min(minR, r)
    ans = minR - maxL + 1
    if ans < 0:
        print(0)
    else:
        print(ans)


resolve()

Solution (Python):
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
l = 0
r = 100001
for lr in LR:
  l = max(lr[0], l)
  r = min(r, lr[1])
  
print(r - l + 1 if r - l >= 0 else 0)

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

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

Solution (Python):
N, M = map(int, input().split())
limL = -454554565454
limR = 7896541265478456
for i in range(M):
  L, R = map(int, input().split())
  limL = max([limL, L])
  limR = min([limR, R])

print(limR-limL+1) if limR-limL+1>0 else print(0)

Solution (Python):
n,m= map(int,input().split())
l1,r1 = map(int,input().split())
lis = [l1,r1]

for i in range(m-1):
  l,r = map(int,input().split())
  if lis[0]<=l<lis[1] and lis[0]<r<=lis[1]:
    lis = [l,r]
  elif (lis[1]<l and lis[1]<r) or (l<lis[0] and r<lis[0]):
    print(0)
    exit()
  elif lis[0]<l<=lis[1] and lis[1]<r:
    lis[0] = l
  elif l<lis[0] and lis[0]<=r<lis[1]:
    lis[1] = r
    
print(lis[1]-lis[0]+1)

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

L = 1
R = 10**5
ans = 1

for i in range(m):
    l, r = map(int, input().split())
    if L <= l <= R:
        L = l
    if L <= r <= R:
        R = r
    if R < l or r < L:
        ans = 0
        break

if ans != 0:
    ans = R - L + 1

print(ans)

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

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

Solution (Python):
n, m = map(int, input().split())
ll = [0 for i in range(n+1)]
l_max = 0
r_min = 1000000
for _ in range(m):
    l, r = map(int, input().split())
    if l > l_max :
        l_max = l
    if r < r_min:
        r_min = r
if l_max <= r_min:
    print(r_min - l_max + 1)
else:
    print(0)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


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


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N,M = IL()

L = 0
R = INF

for i in range(M):
  l,r = IL()
  L = max(L,l)
  R = min(R,r)
  
print(max(0,R-L+1))

Solution (Python):
n, m = map(int, input().split())
Lmax = 0
Rmin = n
for _ 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):
def solve():
    N, M = list(map(int, input().split()))
    s = [0] * (N+1)
    
    for _ in range(M):
        l, r = list(map(int, input().split())) 
        s[l] += 1
        if r+1 != N+1:
            s[r+1] -= 1
    
    cnt = 0
    ruiseki = [0]
    for i in range(1, N+1):
        ruiseki.append(ruiseki[i-1] + s[i])
        if ruiseki[i] == M:
            cnt += 1

    print(cnt)

if __name__ == "__main__":
    solve()

Solution (Python):
N, M = map(int, input().split())
limL = 1
limR = N
for i in range(M):
  L, R = map(int, input().split())
  limL = max([limL, L])
  limR = min([limR, R])

print(limR-limL+1) if limR-limL+1>0 else print(0)

Solution (Python):

N, M = map(int, input().split())
x, y = 0, N
for i in range(1, M + 1):
    L, R = map(int, input().split())
    x = max(x, L)
    y = min(y, R)

print(len(range(x, y + 1)))

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

for m in range(M):
    L, R = map(int, input().split())
    if m==0:
        l = L
        r = R
    else:
        if l<L:
            l = L
        if R<r:
            r = R

print(len(list(range(l,r+1))))

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) < 0:
    print(0)
else:
    print(min(r)-max(l)+1)

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

L = 1
R = N
for l, r in gates:
    if l > L:
        L = l
    if r < R:
        R = r
if R >= L:
    print(R - L + 1)
else:
    print(0)

Solution (Python):
N,M=map(int,input().split())
ans=0
L,R=0,(10**5)+1
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l); R=min(R,r)
    if r<l: break
R=min(R,N)
print(abs((L-1)-R) if 0<=R-L else 0)

Solution (Python):

import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    ml = 1
    mr = N
    for Li, Ri in zip(L, R):
        ml = max(ml, Li)
        mr = min(mr, Ri)

    print(max(mr - ml + 1, 0))


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

Solution (Python):
N, M = map(int, input().split())
gate = [0] * N
maxL, minR = 0, N
for i in range(M):
    L, R = map(int, input().split())
    maxL = max(maxL, L-1)
    minR = min(minR, R-1)

count = minR - maxL + 1
if count >= 0:
    print(count)
else:
    print(0)

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

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

_min = 1
_max = N

for _ in range(M):
    l, r = map(int, input().split(' '))
    _min = max(_min, l)
    _max = min(_max, r)

print(max(0, _max - _min + 1))

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

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

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

L,R= map(int,input().split())

for i in range(M-1):
    x,y= map(int,input().split())
    L = max(L,x)
    R = min(R,y)

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

Solution (Python):
n,m=[int(x) for x in input().rstrip().split()]
ll=[]
rl=[]
for i in range(m):
  l,r=[int(x) for x in input().rstrip().split()]
  ll.append(l)
  rl.append(r)

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

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

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

for ii in range(m):
  
  a,b=map(int,input().split())
  if ii==0:
    
    r=[a,b]
  else:
    
    r=[max(a,r[0]),min(b,r[1])]
q=r[1]-r[0]+1
if q<0:
  print(0)
else:
  print(q)

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

Solution (Python):

"""
Created on Sat May 16 19:11:36 2020

@author: Kanaru Sato
"""

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

currentl,currentr = list(map(int,input().split()))
flag = 1

for i in range(m-1):
    l,r = list(map(int,input().split()))
    if currentl <= l and l <= currentr:
        currentl = l
    if currentl <= r and r <= currentr:
        currentr = r
    if currentr < l or r < currentl:
        flag = 0
        print(0)
        break

if flag == 1:
    print(currentr-currentl+1)

Solution (Python):
from collections import Counter 
from collections import defaultdict
from collections import deque
import itertools
import heapq
import sys
sys.setrecursionlimit(10**6)




input_list = lambda : list(map(int,input().split()))


n,m=input_list()
l=0
r=n
for _ in range(m):
    now=input_list()
    l=max(l,now[0])
    r=min(r,now[1])


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

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

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

def solve(l1, r1, l2, r2):
    if r1 < l2 or r2 < l1:
        return None
    elif l1 < l2 and r1 < r2:
        return [l2, r1]
    elif l1 >= l2 and r1 <= r2:
        return [l1, r1]
    elif l1 <= l2 and r2 <= r1:
        return [l2, r2]
    else:
        return [l1, r2]


def main():
    n, m = map(int, input().split())
    ans = [1, n]
    for _ in range(m):
        l, r = map(int, input().split())
        
        ans = solve(ans[0], ans[1], l, r)
        
        if ans is None:
            print(0)
            exit(0)
    print(ans[1] - ans[0] + 1)

if __name__ == '__main__':
    main()

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.sort()
R.sort()

if L[-1] > R[0]:
    print(0)
else:
    print(R[0] - L[-1] + 1)

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

Solution (Python):
n,m = list(map(int, input().split()))
high=10**10
low=0
for i in range(m):
    a,b = list(map(int, input().split()))
    if a>low:
        low=a
    if b<high:
        high=b
if low>high:
    print(0)
else:
    print(high-low+1)

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

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

for _ in range(M):
    L, R = map(int, input().split())
    Ls.add(L)
    Rs.add(R)

print(len([i for i in range(max(Ls), min(Rs)+1)]))

Solution (Python):
n, m = map(int, input().split())
gl = []
for i in range(m):
  g = list(map(int, input().split()))
  gl.append(g)
zeroflag = False
for j in range(1, m):
  db = gl[j][0] - gl[j - 1][0]
  da = gl[j][1] - gl[j - 1][1]
  if db >= 0:
    if da >= 0:
      gl[j][1] = gl[j - 1][1]
  else:
    if da >= 0:
      gl[j][0] = gl[j - 1][0]
      gl[j][1] = gl[j - 1][1]
    else:
      gl[j][0] = gl[j - 1][0]
  if gl[j][0] > gl[j][1]:
    zeroflag = True
    break
if zeroflag == False:
  print(gl[-1][1] - gl[-1][0] + 1)
else:
  print('0')

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


if __name__ == "__main__":
    main()

Solution (Python):
import itertools
import math
import fractions
import functools
import copy
n, m = map(int, input().split())
lr = []
for i in range(m):
    l, r = map(int, input().split())
    lr.append([l,r])

result = list(range(lr[0][0],lr[0][1]+1))


maximum = lr[0][1]
minimum = lr[0][0]
for i in range(len(lr)):
    maximum = min(maximum,lr[i][1])
    minimum = max(minimum,lr[i][0])

if maximum < minimum:
    print(0)
else:
    print(maximum-minimum+1)

Solution (Python):
N, M = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(M)]
G = [0, N]
def overlap(a, b):
    if max(a[0], b[0]) > min(a[1], b[1]):
        return [0, 0]
    return [max(a[0], b[0]), min(a[1], b[1])]
for m in range(M):
    G = overlap(G, A[m])
if G == [0, 0]:
    print(0)
else:
    print(G[1] - G[0] + 1)

Solution (Python):
N, M = map(int, input().split())
maxL = 1
minR = N
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())
min_l = -1
min_r = N+1
for _ in range(M):
  l,r = map(int,input().split())
  if min_l < l:
    min_l = l
  if min_r > r:
    min_r = r
    
print(max(min_r-min_l+1,0))

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

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

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

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

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

l = 0
r = N+1

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

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

Solution (Python):
if __name__ == "__main__":
    n, m = map(int, input().split())
    l = 1
    r = n

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

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

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

print(len(range(max(L),min(R)+1)))

Solution (Python):
from sys import stdin
import numpy as np
N, M = [int(x) for x in stdin.readline().rstrip().split()]
C = np.zeros(N)
Rmin = N
Lmax = 1
for i in range(M):
    L, R = [int(x) for x in stdin.readline().rstrip().split()]
    if R < Rmin:
        Rmin = R
    if L > Lmax:
        Lmax = L
if Lmax > Rmin:
    print(0)
else:
    print(N-(Lmax-1)-(N-Rmin))

Solution (Python):
from itertools import accumulate

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

num = [0] * (n + 1)
for l, r in lr:
    num[l - 1] += 1
    num[r] -= 1
num = list(accumulate(num))
print(num.count(m))

Solution (Python):
from itertools import accumulate

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

num = [0] * (n + 1)
for l, r in lr:
    num[l - 1] += 1
    num[r] -= 1
num = list(accumulate(num))
print(num.count(m))

Solution (Python):
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 'None'

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

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N, M = map(int, input().split())
    maxL, minR = 0, N+1
    for i in range(M):
        L, R = map(int, input().split())
        maxL = max(L, maxL)
        minR = min(R, minR)
    ans = minR -maxL + 1
    print(ans if ans>0 else 0)

if __name__ == '__main__':
    main()

Solution (Python):

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

left = 1
right = 10**5

for l, r in LR:
    left = max(left, l)
    right = min(right, r)
ans = right - left + 1
if ans<0:
    print(0)
else:
    print(ans)

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

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

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

if max(s)<=min(S):
    print(min(S)-max(s)+1)
else:
    print('0')

Solution (Python):
def main():
    ini_inp=list(map(lambda x:int(x),input().split()))
    N=ini_inp[0]
    M=ini_inp[1]

    L=[]
    R=[]
    for i in range(M):
        sec_inp=list(map(lambda x:int(x),input().split()))
        L.append(sec_inp[0])
        R.append(sec_inp[1])

    low=L[0]
    up=R[0]
    flag=0
    for i in range(1,M):
        if L[i]>up:
            flag=1
            break
        elif L[i]>=low:
            low=L[i]
        elif R[i]<=up:
            up=R[i]

    if flag==0:
        print(up-low+1)
    else:
        print(0)

if __name__=="__main__":
    main()

Solution (Python):
N, M = map(int, input().split())
a = []
for i in range(M):
    a.append(list(map(int, input().split())))
b = [0, float("INF")]
for i in range(M):
    b[0] = max(a[i][0], b[0])
    b[1] = min(a[i][1], b[1])
if b[0] <= b[1]:
    print(b[1] - b[0] + 1)
else:
    print(0)

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

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

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





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

end = n-1
start = 0
for i in range(m):
    if start <= l[i] and r[i] <= end:
        start = l[i]
        end = r[i]
    elif l[i] < start and r[i] <= end:
        end = r[i]
    elif start <= l[i] and end < r[i]:
        start = l[i]

    if end-start+1 < 0:
        print(0)
        exit()
print(end-start+1)

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

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


imos=[0]*(n+1)
for i in lr:
    start,end=i
    imos[start-1]+=1
    imos[end]-=1
for i in range(1,n):
    imos[i]=imos[i-1]+imos[i]

print(imos.count(m))

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

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

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

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

left = 1
right = N
for key in keys:
    if left < key[0]:
        left = key[0]
    if key[1] < right:
        right = key[1]
if right < left:
    print(0)
else:
    print(right - left + 1)

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

Solution (Python):

import re
import math
from collections import defaultdict
from collections import deque
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache

readline=sys.stdin.readline
sys.setrecursionlimit(2000000)

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)

def main():
    N,M=list(map(int,list(input().split())))
    A=[0]*(N+1)
    for a in range(M):
        l,r=map(int,input().split())
        l-=1
        r-=1
        A[l]+=1
        A[r+1]-=1
    for a in range(1,len(A)):
        A[a]+=A[a-1]
    print(A.count(M))
main()