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

Solution (Python):
N, M = list(map(int,input().split()))
L = []
R = []
for i in range(M):
    in1, in2 = list(map(int,input().split()))
    L.append(in1)
    R.append(in2)
ans = max(0, min(R) - max(L) + 1)
print(ans)

Solution (Python):
def main():
    n, m = map(int,input().split()) 
    startlis = []
    endlis = []

    for i in range(m):
        a, b = map(int, input().split())
        startlis.append(a)
        endlis.append(b)
    
    start = max(startlis)
    end = min(endlis)

    print(max(0,end - start + 1))





    
if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, m = map(int, input().split())
    l_max = 0
    r_min = float('inf')
    for _ 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 + 1 - l_max, 0))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
n,m = map(int, raw_input().split(' '))
def intersection(A,B):
	a,b  = max(A[0],B[0]), min(A[1],B[1])
	return [a,b] if a <= b else []

r = [1,n]
for _ in range(m):
	a,b = map(int, raw_input().split(' '))
	r = intersection(r, [a,b])
	if len(r) == 0: break
print (r[1] - r[0] + 1) if r else 0

Solution (Python):
n,m = map(int, raw_input().split(' '))
def intersection(A,B):
	a,b  = max(A[0],B[0]), min(A[1],B[1])
	return [a,b] if a <= b else []

r = [1,n+1]
for _ in range(m):
	a,b = map(int, raw_input().split(' '))
	r = intersection(r, [a,b])
	if len(r) == 0: break
print (r[1] - r[0] + 1) if r else 0

Solution (Python):
n,m=map(int,input().split())
l,r=1,pow(10,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):
n,m=map(int,input().split())

left=1
right=n

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

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

Solution (Python):
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():
    N, M = map(int, input().split())
    card = [0 for _ in range(N + 1)]

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

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

    
    print(card.count(M))


if __name__ == '__main__':
    solve()

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

Solution (Python):
def solve(l,r):
  l_max = max(l)
  r_min = min(r)
  return max(r_min - l_max + 1, 0)
  
N,M = map(int,input().split())
L = [0]*M
R = [0]*M
for i in range(M):
  L[i],R[i] = map(int,input().split())
print(solve(L,R))

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
l = 0
r = inf
for _ in range(m):
  a, b = rm()
  l = max(a, l)
  r = min(b, r)
c = r-l+1
if c >= 1:
  print(c)
else:
  print(0)

Solution (Python):






N,M = map(int, input().split())
lr = []
l_max = 1
r_min = N
for _ in range(M):
    lr.append(list(map(int, input().split())))
    l_max = max(l_max, lr[-1][0])
    r_min = min(r_min, lr[-1][1])

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

Solution (Python):
N, M = map(int, input().split())
A = []
for i in range(M):
    L, R = map(int, input().split())
    if i == 0:
        Lmax = L
        Rmin = R
    else:
        Lmax = max(L, Lmax)
        Rmin = min(R, Rmin)





if Rmin-Lmax < 0:
    print(0)
else:
    print(Rmin-Lmax+1)

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

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

Solution (Python):
def resolve():
    N, M = list(map(int, input().split()))
    interval = []
    l, r = 0, 10**5
    for i in range(M):
        L, R = list(map(int, input().split()))
        l = max(l, L)
        r = min(r, R)

    interval = set(list(range(l, r + 1)))
    interval = interval & set(range(1, N + 1))

    print(len(interval))

    return


resolve()

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

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

Solution (Python):
N, M = map(int, input().split())
MIN = 0
MAX = 110000
        
for i in range(M):
    L, R = map(int, input().split())
    MIN = max(MIN, L)
    MAX = min(MAX, R)
    

ans = MAX-MIN+1 if MAX >= MIN else 0
print(ans)

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

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

Solution (Python):
N, M = map(int, input().split())
max_L, min_R = 1, N
for i in range(M):
    L, R = map(int, input().split())
    if L > max_L:
        max_L = L
    if R < min_R:
        min_R = R
ans = min_R - max_L + 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 = min(R) - max(L) + 1
if ans < 0:
    ans = 0
print(ans)

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

left = 1
right = n

for i in range(m):
    a, b = map(int, input().split())
    left = max(left, a)
    right = min(right,b)
    
print(max(right - left +1 , 0))

Solution (Python):

import sys
from collections import defaultdict, deque




def solve():
    n, m = [int(x) for x in input().split()]
    a, b = 0, 1e9
    for _ in range(m):
        l, r = [int(x) for x in input().split()]
        a = max(a, l)
        b = min(b, r)
    print(max(0, b - a + 1))
        
t = 1

for case in range(1,t+1):
    ans = solve()



"""

1 4
7
RRRR
1 1 0 1
----
4

1 4
47
RCRR
1 1 0 1
----
-1

2 2
10
RR
RR
1 7
4 1
----
4

2 2
10
RR
RR
7 4
7 4
----
-1

3 3
8
RCR
RRC
RRR
1 1 1
1 1 1
1 3 1


3 3
1
RCR
RRC
RRR
1 1 1
1 1 1
1 3 1


"""

Solution (Python):
n,m=map(int,input().split())
card =[list(map(int,input().split())) for i in range(m)]
r = card[0][0]
R = card[0][1]
for i in range(m):
    if  r < card[i][0]:
        r = card[i][0]
for i in range(m):
    if card[i][1] < R:
        R = card[i][1]
ans = max(0,R - r + 1)
print(ans)

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

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

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




L = a[0][0]
R = a[0][1]

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

R = min(R,n)
if R >= L :
  print(R-L+1)
else:
  print(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(right-left+1, 0))

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

minG=1
maxG=N
for i in range(M):
    L,R=map(int,input().split())
    if L>minG:
        minG=L
    if R<maxG:
        maxG=R

if minG<=maxG:
    print(maxG-minG+1)
else:
    print(0)

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

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

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

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)








n,m=n1()
LR=n3(m)

m=0
M=10**10
for l,r in LR:
    m=max(m,l)
    M=min(M,r)

p0(M>=m,M-m+1,0)

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)








n,m=n1()
LR=n3(m)

m=0
M=10**10
for l,r in LR:
    m=max(m,l)
    M=min(M,r)
ans=M-m

p0(M>=m,M-m+1,0)

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

Solution (Python):
import sys
import math
from collections import Counter
import itertools
import fractions
import datetime
from decimal import Decimal



def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())

def LI(): return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'


p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")



N,M=MI()
LR=LLI(M)

maxL=0
minR=11111111111111111

for lr in LR:
    maxL=max(maxL, lr[0])
    minR=min(minR, lr[1])    
            
print(max(minR-maxL+1, 0))

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N, M = map(int, input().split())
    L_max, R_min = 0, 10**100
    for _ in range(M):
        l, r = map(int, input().split())
        L_max = max(L_max, l)
        R_min = min(R_min, r)
    delta = R_min - L_max + 1
    if delta <= 0:
        delta = 0
    print(delta)
main()

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

for i in range(0,m):
  a,b=map(int,input().split())
  if mn<a:
    mn=a
  if mx >b:
    mx=b

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

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

memo = [0]*(N+1)

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

memo_sum = memo[0]
for i in range(1, N):
    memo_sum += memo[i]
    memo[i] = memo_sum

ans = 0
for a in memo:
    if a == M:
        ans += 1

print(ans)

Solution (Python):
n, m = map(int, input().split())
apple = []
for _ in range(m):
    apple.append(list(map(int, input().split())))
cat = []
dog = []
for i in apple:
    cat.append(i[0])
    dog.append(i[1])

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

Solution (Python):

from collections import deque
n,m = map(int,input().split())
gate = deque()
for _ in range(m):
    l,r = map(int,input().split())
    if len(gate) == 0:
        gate.append([l,r])
        
    else:
        check = gate.pop()
        
        if check[0] <= l:
            check[0] = l
            
        if check[1] >= r:
            check[1] = r
            
        gate.append(check)
     
result = gate.pop()
if result[1] < result[0]:
    print(0)
else:
    print(result[1]-result[0]+1)

Solution (Python):
import numpy as np
def solve():
    N,M = map(int, input().split())
    LR = np.array([list(map(int, input().split())) for _ in range(M)])
    Lmax = np.max(LR[:,0])
    Rmin = np.min(LR[:,1])
    ans = Rmin - Lmax + 1 if Rmin - Lmax >=0 else 0
    print(ans)





if __name__ == '__main__':
    solve()

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

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

Solution (Python):
import sys

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

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

print(ans)

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

Solution (Python):
N,M = map(int,input().split())
s = [0]*(N+1)
for i in range(M):
  l,r = list(map(int,input().split()))
  s[l] += 1
  if r+1 != N+1:
    s[r+1] -= 1
ruiseki = [0]*(N+1)
cnt = 0
for i in range(1,N+1):
  ruiseki[i] += ruiseki[i-1] + s[i]
  if ruiseki[i] == M:
    cnt +=1
print(cnt)

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)

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

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

MIN = 0
MAX = float('inf')

for i in range(M):
    MIN = max(MIN, int(gate[i][0]))
    MAX = min(MAX, int(gate[i][1]))

print(max(MAX - MIN + 1, 0))

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

c_min = 1
c_max = 10 ** 5
for i in range(M):
    LM = list(map(int, input().split()))
    c_min = max(c_min, LM[0])
    c_max = min(c_max, LM[1])

if c_min > c_max:
    print(0)
else:
    print(c_max - c_min + 1)

Solution (Python):
n,m = map(int,input().split())
ls = [i for i in range(m)]
rs = [i for i in range(m)]
cnt = 0
for _ in range(m):
    ls[_],rs[_] = map(int,input().split())

maxl = max(ls)
minr = min(rs)

for nn in range(1,n+1):
    if maxl <= nn and minr >= nn:
        cnt += 1
print(cnt)

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

max_L = []
min_R = []

cnt = 0
for i in range(m):
    L,R = map(int,input().split())
    max_L.append(L)
    min_R.append(R)
if max(max_L) <= min(min_R):
    print(min(min_R)-max(max_L)+1)
else:
    print(0)

Solution (Python):
N, M = map(int, input().split())
Lmax = 0
Rmin = 10**5 + 1
for n in range(M):
  L, R = map(int, input().split())
  if L > Lmax:
    Lmax = L
  if R < Rmin:
    Rmin = R

gate = Rmin - Lmax + 1
if gate < 0:
  print("0")
else:
  print(gate)

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)
ansl = max(L)
ansr = min(R)
print(0 if ansl > ansr else ansr-ansl+1)

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

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

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

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

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

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

left = -1
right = n + 1

for _ in range(m):
    l, r = map(int, input().split())
    if r < left or l > right:
        print(0)
        sys.exit()
    left = max(left, l)
    right = min(right, r)

print(right - left + 1)

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

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

print(max(0, ansR - ansL + 1))

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

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=r_min-l_max+1
if ans>=0:
    print(ans)
else:
    print(0)

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

if __name__ == '__main__':
    main()

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

print(max(0,b-a+1))

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

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

Solution (Python):
from collections import Counter
import numpy as np

n, m = map(int, input().split())
data = [0] * m

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


data = np.array(data)
ll = data[:, 0]
rr = data[:, 1]

ans = np.min(rr) - max(ll) + 1
ans = max(0, ans)
print(ans)

'''
n, m = map(int, input().split())
tar = [0] * (1 + 10 ** 5)
tar = np.array(tar)

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



count = Counter(tar)
ans = count[m]
print(ans)

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

ll, rr = map(int, input().split())
for i in range(m - 1):
    l, r = map(int, input().split())

    
    if (l <= ll):
      pass
    elif (ll < l )

ans = max(ll - rr, 0)
print(ans)

'''

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

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

    start_index = 1
    last_index = N
    for _ in range(M):
        pp, ss = map(int, input().split())
        start_index = max(start_index, pp)
        last_index = min(last_index, ss)

    print(max(0, last_index - start_index + 1))


resolve()

Solution (Python):





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



p=1
left=1
right=N
for m in range(M):
    l,r = map(int,input().split())
    if left<=l and l<=right:
        left=l
    if left<=r and r<=right:
        right=r

    if right<l or r<left:
        p=0

if not p:
    print(p)
    exit()
print(right-left+1)

Solution (Python):
N,M = map(int,input().split())
L_list = []
R_list = []
count = 0
min_mn=float('inf')
max_mn=0
for i in range(M):
    L,R=map(int,input().split())
    min_mn = min(min_mn,R)
    max_mn = max(max_mn,L)

for i in range(1,N+1):
    if(i>=max_mn and i<=min_mn):
        count += 1
print(count)

Solution (Python):
N,M = map(int,input().split())
L_list = []
R_list = []
count = 0
min_mn=float('inf')
max_mn=0
for i in range(M):
    L,R=map(int,input().split())
    min_mn = min(min_mn,R)
    max_mn = max(max_mn,L)

for i in range(1,N+1):
    if(i>=max_mn and i<=min_mn):
        count += 1
print(count)

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

l = [L[0], R[0]] 
for i in range(1, M):
    if L[i] > l[0]:
        l[0] = L[i]
    if R[i] < l[1]:
        l[1] = R[i]

if l[0] <= l[1]:
    ans = l[1] - l[0] + 1
else:
    ans = 0
print(ans)

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

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

Solution (Python):

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

mi, ma = 0, 10**5

for i in LR:
    mi = max(mi, i[0])
    ma = min(ma, i[1])

if ma-mi < 0:
    print(0)
else:
    print(ma-mi + 1)

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

for _ in range(m):
  a, b = map(int, input().split())
  l.append(a)
  r.append(b)
  
l_max = max(l)
r_min = min(r)

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

Solution (Python):
n,m=map(int,input().split())
lr=[]
ma=1
mb=n

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

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

l_max=1
r_min=n

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

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

Solution (Python):
n,m = map(int, input().split())
lr = [0, 10**6]
for _ in range(m):
  t = sorted(list(map(int, input().split())))
  lr[0] = max(lr[0], t[0])
  lr[1] = min(lr[1], t[1])

ans = lr[1] - lr[0] + 1
if ans < 0:
  print(0)
else:
  print(ans)

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

mi = 1
ma = N

for _ in range(M):
	a,b = map(int,input().split())
	mi = max(a,mi)
	ma = min(b,ma)

print(max(0,ma-mi+1))

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

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

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


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


    

L = [x[0] for x in LR]
R = [x[1] for x in LR]


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

Solution (Python):


N , M =(int(a) for a in input().split())
num = []

for i in  range(M) :
    L,R =(int(a) for a in input().split())
    num.append((L , R))

a = 0
b = 10 ** 5
for i in range(M) :
    a = max(a , num[i][0])
    b = min(b , num[i][1])

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

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

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

Solution (Python):


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

min_, max_ = 0, 10**5
for i in range(m):
    a, b = list(map(int, input().split()))

    min_ = max(min_, a)
    max_ = min(max_, b)

print(max(max_-min_+1, 0))

Solution (Python):
def main():
    n, m = map(int, input().split())
    left = 1
    right = n
    for _ in range(m):
        l, r = map(int, input().split())
        left = max(left, l)
        right = min(right, r)
        
    print(max(0, right - left + 1))
   
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys

input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    Lmax = 0; Rmin = N+1
    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)
        
if __name__ == "__main__":
    main()

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

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

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

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

l=[]
r=[]
for i in range(M):
    l.append(L[i][0])
    r.append(L[i][1])
a=min(r)-max(l)+1
if a>=0:
    print(a)
else:
    print(0)

Solution (Python):
N,M=map(int,input().split())
LR=[list(map(int, input().split())) for _ in range(M)]
left=0
right=N+1
ans=0
for i in range(M):
	left=max(left,LR[i][0])
	right=min(right,LR[i][1])
ans=right-left+1
if ans<0:
	print(0)
else:
	print(ans)

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

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

tmp = LI()
n,m = [tmp.pop(0) for _ in range(2)]

l = []
r = []

for i in range(m):
    tmp = LI()
    l.append(tmp.pop(0))
    r.append(tmp.pop(0))

ll = l[0]
rr = r[0]
for i in range(2):
    for j in range(m):
        if i == 0:
            if ll < l[j]:
                ll = l[j]
        else:
            if r[j] < rr:
                rr = r[j]

if rr<ll:
    print(0)
else:
    k = rr-ll+1
    if n<k:
        print(n)
    else:
        print(k)

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

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

Solution (Python):
import sys




from collections import deque

from fractions  import gcd

input=sys.stdin.readline
n,m=map(int,input().split())
lm=1
rm=n
for i in range(m):
    l,r=map(int,input().split())
    if l>lm:
        lm=l
    if r<rm:
        rm=r
ans=rm-lm+1 if rm>=lm else 0
print(ans)

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

left = 1
right = N+1
for i in range(M):
  left = max(left, datas[i][0])
  right = min(right, datas[i][1])

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

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

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

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

l= L[len(L)-1]
r = R[0]

cnt =0 

for i in range(l,r+1):
    cnt+=1

print(cnt)

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

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

Solution (Python):
"""
i番目のゲートは LiからRiまでのIDカードのうちどれか1枚を持っていれば通過できます
"""

N, M = map(int, input().split())
left = 0
right =float("inf")

for i in range(M):
    L, R = map(int, input().split())
    if left<L:
        left = L
    if right > R:
        right = R

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

Solution (Python):
(N, M), *LR, = [map(int, s.split()) for s in open(0)]
card_l = 1
card_r = N
for l, r in LR:
    card_l = max(l, card_l)
    card_r = min(r, card_r)
print(max(0, card_r - card_l + 1))