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

lr = []

ming = 0
maxg = 10 **6

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

for i in lr:
    if i[0] > ming:
        ming = i[0]
    if i[1] < maxg:
        maxg = i[1]

print(max([0, maxg - ming + 1]))

Solution (Python):
import numpy as np
n, k = map(int,input().split())
gates = [0]*(n+1)
for i in range(k):
  l,r = map(int,input().split())
  gates[l-1] += 1
  gates[r] -= 1
print(np.count_nonzero(np.cumsum(gates) == k))

Solution (Python):
n, m = map(int, input().split())
a = [ list(map(int, input().split())) for i in range(m) ]
print(max(min(a, key=lambda x: x[1])[1] - max(a, key=lambda x: x[0])[0] + 1, 0))

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

Solution (Python):

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

ans,ansf = 1,n

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

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
from decimal import Decimal
from operator import mul

INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)



class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


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 combinations_count(n, r):
    if n < r:
        return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True



def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]


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

ans = n
left = 0
right = n - 1
for i in range(m):
    left = max(left, l[i])
    right = min(right, r[i])
    ans = min(ans, max(right - left + 1, 0))
print(ans)

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

print(max(R_min-L_max+1,0))

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

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

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

print(card.count(M))

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

n, m = [int(w) for w in input().split()]

m_l = 0
m_r = n
for i in range(m):
    l, r = [int(w) for w in input().split()]
    m_l = max(m_l, l)
    m_r = min(m_r, r)
print(max(m_r - m_l + 1, 0))

Solution (Python):
class Imos_1:
    def __init__(self,N):
        self.len=N
        self.list=[0]*(N+1)

    def Add(self,F,T,C):
        self.list[F]+=C
        self.list[T+1]-=C

    def Cumulative_Sum(self):
        Y=[0]*(self.len)
        S=0
        for i in range(self.len):
            S+=self.list[i]
            Y[i]=S

        return Y

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

I=Imos_1(N)
for _ in range(M):
    L,R=map(int,input().split())
    I.Add(L-1,R-1,1)

J=I.Cumulative_Sum()
print(J.count(M))

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

gates = [0] * (n + 2)

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

from itertools import accumulate
gates = list(accumulate(gates))

print(gates.count(m))

Solution (Python):
N,M = map(int,input().split())
min_L = 1
max_R = 1 * 10 ** 7
for i in range(M):
    L,R=map(int,input().split())
    min_L = max(L,min_L)
    max_R = min(R,max_R)

if max_R >= min_L :  
    print(max_R - min_L + 1)
else :
    print(0)

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

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

Solution (Python):
N,M=list(map(int,input().split()))
l=1
r=N
for m in range(M):
    l_,r_ = list(map(int,input().split()))
    l = max(l,l_)
    r = min(r,r_)
    
ans = r-l+1

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

Solution (Python):
N,M=list(map(int,input().split()))
l=1
r=N
for m in range(M):
    l_,r_ = list(map(int,input().split()))
    l = max(l,l_)
    r = min(r,r_)
    
ans = r-l+1

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

Solution (Python):









import sys
input = sys.stdin.readline

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


L = 0
R = N
for i in range(M):
    L = max(L, list_LR[i][0])
    R = min(R, list_LR[i][1])
if L > R:
    print(0)
else:
    print(R-L+1)

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

ls.sort()
rs.sort()

ans = rs[0] - ls[-1] + 1
ans = max(0,ans)

print(ans)

Solution (Python):
n,m = map(int, input().split())
imos = [0]*(n+2)

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

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

print(ans)

Solution (Python):

N, M = map(int, input().split())
valL = 0
valR = 10 ** 5
ans = 0
for m in range(M):
    L, R = map(int, input().split())
    valL = max(valL, L)
    valR = min(valR, R)
if valL > valR:
    ans = 0
else:
    ans = valR - valL + 1
print(ans)

Solution (Python):
n, m = map(int, input().split())
Authority = [list(map(int, input().split())) for _ in range(m)]
cards = list(range(1, n + 2))
l_list = []
r_list = []
for i in range(m):
    l_list.append(Authority[i][0])
    r_list.append(Authority[i][1])
        
print(len(cards[max(l_list):min(r_list) + 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(right - left + 1 if right - left >= 0 else 0)

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())
    l_max = max(l_max, l)
    r_min = min(r_min, r)

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

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

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

Solution (Python):
from itertools import accumulate
n,m=map(int,input().split())
t=[0 for i in range(n)]
for i in range(m):
    l,r=map(int,input().split())
    t[l-1]+=1
    if r<n:
        t[r]-=1
ans=list(accumulate(t))
tmp=0
for i in range(n):
    if ans[i]==m:
        tmp+=1
print(tmp)

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

L = 1
R = N

for m in range(M):
    L_, R_ = map(int,input().split())
    if L < L_:
        L = L_
    if R_ < R:
        R = R_

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

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

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

import numpy as np
card = list(np.cumsum(card))
print(card.count(m))

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

card = [0]*(n+2)
for _ in range(m):
    l, r = map(int, input().split())
    card[l] +=1
    card[r+1] -=1
count =0
import numpy as np
card = np.cumsum(card)
for i in card:
    if i ==m:
        count+=1

print(count)

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

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

ans = max - min + 1

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

Solution (Python):
N,M = map(int,input().split())
l = -1
r = N+1

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

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

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

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

print(res)

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(L)
r = min(R)

if l <= r:
    ans = r-l+1
else:
    ans = 0

print(ans)

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

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

Solution (Python):
f=lambda:map(int,input().split())
_,m=f()
l,r=zip(*[f() for _ in range(m)])
print(max(0,min(r)-max(l)+1))

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

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

L, R = 0, float("inf")

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

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

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

L, R = 0, float("inf")

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

count = 0
if L <= R:
    for i in range(1, N+1):
        if L <= i <= R:
            count += 1

print(count)

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

    left = 1
    right = N

    for _ in range(M):
        l, r = map(int, input().split())

        if left < l:
            left = l
        if right > r:
            right = r

    ans = right - left

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


main()

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

left = 1
right = N

for _ in range(M):
    l, r = map(int, input().split())

    if left < l:
        left = l
    if right > r:
        right = r

ans = right - left

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

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

left = 1
right = N

for l, r in LR:
    if left < l:
        left = l
    if right > r:
        right = r

ans = right - left

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

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

left = 1
right = N

for l, r in LR:
    left = max(left, l)
    right = min(right, r)

ans = right - left

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

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

left = 0
right = float('inf')
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())
lmax = 0
rmin = 10**5
for i in range(M):
    L, R = map(int, input().split())
    lmax = max(lmax, L)
    rmin = min(rmin, R)
ans = rmin - lmax
if ans >= 0:
    print(ans + 1)
else:
    print(0)

Solution (Python):
n, m = map(int, input().split())
id_card = [0] * (n + 1)

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

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

print(ans)

Solution (Python):
n, m = map(int, input().split())
lmax, rmin = 0, 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):
n,m = map(int, input().split())
max_l = float("-inf")
min_r = float("inf")
for _ in range(m):
  l,r = map(int, input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
ans = min_r - max_l + 1
if ans > 0:
  print(ans)
else:
  print(0)

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

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

Solution (Python):
N, M = map(int, input().split())
lmax = 0
rmin = 10**5
for i in range(M):
    L, R = map(int, input().split())
    lmax = max(lmax, L)
    rmin = min(rmin, R)
ans = rmin - lmax
if ans >= 0:
    print(ans + 1)
else:
    print(0)

Solution (Python):
hoge = input().split()
N = int(hoge[0])
M = int(hoge[1])
 
mi = 0
ma = N
 
for j in range(M):
    i = input().split(" ")
    l = int(i[0])
    r = int(i[1])
    if l >= mi:
        mi = l
    if r <= ma:
        ma = r
r = ma - mi + 1
if r > 0:
    print(r)
else:
    print(0)

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

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

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

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

ansl=-1
ansr=10**6

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

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

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

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

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

L = 1
R = 10**5

for i in range (M):
    l = LR[i][0]
    r = LR[i][1]

    if l > L:
        L = l
    if r < R:
        R = r

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

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

count_dict = {}

gate = set(range(10**5 + 1))

L = 1
R = 10**5

for i in range (M):
    l = LR[i][0]
    r = LR[i][1]

    if l > L:
        L = l
    if r < R:
        R = r

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

Solution (Python):
hoge = input().split()
N = int(hoge[0])
M = int(hoge[1])

mi = 0
ma = N

for j in range(M):
    i = input().split(" ")
    l = int(i[0])
    r = int(i[1])
    if l >= mi:
        mi = l
    if r <= ma:
        ma = r
r = ma - mi + 1
if r > 0:
    print(r)
else:
    print(0)

Solution (Python):
N, M = map(int, input().split())
aL, aR = 0, 10**6
for _ in range(M):
    L, R = map(int, input().split())
    aL = max(aL, L)
    aR = min(aR, R)
if aR>= aL:
    print(aR-aL+1)
else:
    print(0)

Solution (Python):
n,m=map(int,input().split())
table=[0]*(n+2)
for i in range(m):
    l,r=map(int,input().split())
    table[l]+=1
    table[r+1]-=1
from itertools import accumulate
from collections import Counter as co
var=co((accumulate(table)))

if m not in var:print(0)
else:print(var[m])

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

cards = [lrs[0][0], lrs[0][1]] 

for lr in lrs[1:]:
    if (lr[1] < cards[0]) or (lr[0] > cards[1]):
        print(0)
        exit()

    cards[0] = max(cards[0], lr[0])
    cards[1] = min(cards[1], lr[1])
    

print(cards[1] - cards[0] + 1)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

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

L = []; R = []
for _ in range(m):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

maxL = max(L)
minR = min(R)

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

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

L = []; R = []
for _ in range(m):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

maxL = max(L)
minR = min(R)

if minR < maxL:
    ans = 0
else:
    ans = minR-maxL+1
print(ans)

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

l = 1
r = n

for i, j in LR:
  l = max(l, i)
  r = min(r, j)
  
print(max(0, r - l + 1))

Solution (Python):
n, m = map(int,input().split())
lsl = []
lsr = []
for i in range(m):
    l, r = map(int,input().split())
    lsl.append(l)
    lsr.append(r)
lmax = max(lsl)
rmin = min(lsr)

if lmax > rmin:
    print(0)
else:
    ans1 = set(range(1,n + 1))
    ans2 = set(range(lmax,rmin + 1))
    ans = ans1 & ans2
    print(len(ans))

Solution (Python):
n,m=map(int,input().split())
gate=[list(map(int,input().split())) for i in range(m)]
small=[]
tall=[]
for i in range(m):
    small+=[gate[i][0]]
    tall+=[gate[i][1]]
smallmax=(max(small))
tallmin=(min(tall))
if tallmin-smallmax>=0:
    print(tallmin-smallmax+1)
else:
    print(0)

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

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

import sys
input = sys.stdin.readline
N,M = (int(i) for i in input().split())
ans_L =[]
ans_R =[]
for i in range(M):
    L,R = (int(x) for x in input().split())
    ans_L.append(L)
    ans_R.append(R)

ans =0
max_L =max(ans_L)
min_R =min(ans_R)
if max_L > min_R:
    ans =0
else:
    ans =min_R -max_L +1
print(ans)

Solution (Python):
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n,m = i2()
l = []
r = []
for i in range(m):
	L,R = i2()
	l.append(L)
	r.append(R)
print( max(min(r) - max(l) + 1, 0) )

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

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

ans = 0
Lmax = max(L)
Rmin = min(R)

for a in range(n):
    if Lmax <= a+1 and a+1 <= Rmin:
        ans += 1

print(ans)

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

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)

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

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

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

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

count=[0]+[0]*N+[0]
for l,r in lr:
    count[l]+=1
    count[r+1]-=1

i=1
while i<N+2:
    count[i]=count[i]+count[i-1]
    i+=1

print(sum(c==M for c in count))

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

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

for i in range(M):
    L_new, R_new = map(int, input().split())
    if L < L_new:
        L = L_new
    if R_new < R:
        R = R_new

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

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N, M, *LR=map(int, read().split())
    Lmax = max(LR[::2])
    Rmin = min(LR[1::2])
    
    print(max(0, Rmin-Lmax + 1))
    return


if __name__ == '__main__':
    main()

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

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

Solution (Python):
import itertools

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

card = [0] *N

for i in range(M):
    l, r = map(int,input().split())
    card[l-1] += 1
    if (r+1)-1 < N:
        card[r] -= 1
        
cumsum = list(itertools.accumulate(card))

print(cumsum.count(M))

Solution (Python):
import math

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

_left, _right = 1, n

for l, r in lrL:
    _left = max(_left, l)
    _right = min(_right, r)

print(max(0, _right - _left + 1))

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

l=-float('inf')
r=float('inf')
for i in range(m):
  L,R=[int(x) for x in input().rstrip().split()]
  if l<L:
    l=L
  if R<r:
    r=R
if l<=r:
  print(r-l+1)
else:
  print(0)

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


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

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

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

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

l = []
r = []

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


ans = rmin - lmax + 1
if (ans < 0):
  ans = 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)

ans = 0
l = max(L)
r = min(R)
for i in range(N):
    if i+1 >= l and i+1 <= r:
        ans += 1

print(ans)

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

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

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

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

print(A.count(M))

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

Solution (Python):
def main():
    n,m=map(int,input().split())
    l=-1
    r=10**5+1
    for _ in range(m):
        l2,r2=map(int,input().split())
        l=max(l,l2)
        r=min(r,r2)
    print(max(0, min(r,n)-l+1))
    
if __name__ == "__main__":
    main()

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

Solution (Python):
N, M = map(int, input().split())
ok = [0, pow(10, 5)]

for i in range(M):
    l, r = map(int, input().split())
    if l >= ok[0]:
        ok[0] = l
    if r <= ok [1]:
        ok[1] = r
if ok[0] > ok[1]:
    print('0')
else:
    print(ok[1] - ok[0] +1)

Solution (Python):


import sys
input=sys.stdin.readline

def main():
    N,M=map(int,input().split())
    lr=[tuple(map(int,input().split())) for i in range(M)]
    imos=[0]*(N+2)
    cnt=0
    for i in lr:
        imos[i[0]]+=1
        imos[i[1]+1]-=1
    for j in range(N+1):
        imos[j+1]+=imos[j]
    for i in imos:
        if i == M:
            cnt+=1
    print(cnt)
    
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):

 

 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
 

 







 
N,M=map(int, input().split())
LR = [list(map(int,input().split())) for i in range(M)]
 
Lmin = 1
Rmax = N

for i in range(M):
    if Lmin < LR[i][0]:
        Lmin = LR[i][0]
    if LR[i][1] < Rmax:
        Rmax = LR[i][1]
    if Rmax < Lmin:
        print(0)
        sys.exit()

print(Rmax-Lmin+1)

Solution (Python):
n,m = map(int, input().split())
LR = [list(map(int, input().split())) for i in range(m)]
X = [LR[i][0] for i in range(m)]
Y = [LR[i][1] for i in range(m)]

print(max(min(Y)-max(X)+1,0))

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

left = 1
right = n

for i in range(m):
    a, b = map(int, input().split())
    if a > left:
        left = a
    if b < right:
        right = b
    
    if left > right:
        print(0)
        exit()

print(right-left+1)

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

in_min = 1
in_max = N

for i in range(M):
	l, r = map(int, input().split())
	
	in_min = max([in_min, l])
	
	in_max = min([in_max, r])


if(in_max < in_min):
	
	print(0)
else:
	cnt = in_max - in_min +1
	print(cnt)

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

lmax = 0
rmin = int(1e10)

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

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

Solution (Python):
def solve(lx, rx):
    diff = min(rx) - max(lx)
    ans = diff+1 if diff >= 0 else 0
    return ans


if __name__ == '__main__':
    n, m = map(int, input().split())
    lx = []
    rx = []

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

    print(solve(lx, rx))

Solution (Python):
N, M, *_LR = map(int, open(0).read().split())
LR = list(zip(*[map(int, iter(_LR))]*2))

L_max = 1
R_min = N

for L,R in LR:
    L_max = max(L, L_max)
    R_min = min(R, R_min)

card_num = R_min - L_max + 1

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