CodeEff/ECCO huggingface dataset:
# Original solution
def resolve():
    n, m = list(map(int, input().split()))
    lst = [list(map(int, input().split())) for _ in range(m)]

    ans = set(range(lst[0][0], lst[0][1] + 1))
    for l in lst[1:]:
        ans = ans.intersection(set(range(l[0], l[1] + 1)))

    print((len(ans)))


resolve()


# Optimized solution
def resolve():
    n, m = list(map(int, input().split()))
    lst = [list(map(int, input().split())) for _ in range(m)]

    prev = lst[0]
    for new in lst[1:]:
        prev = [max(prev[0], new[0]), min(prev[1], new[1])]

    print((len(list(range(prev[0], prev[1]+1)))))


resolve()
# Original solution
import math

def main():
    n, m = [int(x) for x in input().split(" ")]
    gates = []
    mi = n
    ma = 0

    for x in range(m):
        l, r = [int(x) for x in input().split(" ")]
        gates.append((l, r))

    for gate in gates:
        # print(gate)
        if ma <= gate[0]:
            ma = gate[0]

        if mi >= gate[1]:
            mi = gate[1]

    res = 0
    if mi < ma:
        res = 0
    else:
        res = mi - ma + 1

    print(res)

if __name__ == "__main__":
    main()

# Optimized solution
import math

def main():
    n, m = [int(x) for x in input().split(" ")]
    gates = []
    mi = n
    ma = 0

    for x in range(m):
        l, r = [int(x) for x in input().split(" ")]
        gates.append((l, r))

    mi = gates[0][1]
    ma = gates[0][0]

    for gate in gates:
        # print(gate)
        if ma <= gate[0]:
            ma = gate[0]

        if mi >= gate[1]:
            mi = gate[1]

    res = 0
    if mi < ma:
        res = 0
    else:
        res = mi - ma + 1

    print(res)

if __name__ == "__main__":
    main()
# Original solution
N, M = list(map(int, input().split()))
LR = [tuple(map(int, input().split())) for _ in range(M)]
print((len(set.intersection(*[set(range(l, r+1)) for l, r in LR]))))


# Optimized solution
N, M = list(map(int, input().split()))
LR = list(list(map(int, input().split())) for _ in range(M))
L, R = list(map(list, list(zip(*LR))))
print((max(0, min(R)-max(L)+1)))
# Original solution
N, M = list(map(int, input().split()))
LR = [list(map(int,input().split())) for _ in range(M)]

ans = [1] * (N+1)
for L,R in LR:
    ans[:L] = [0] * L
    ans[R+1:] = [0] * (N+1-R)

print((ans.count(1)))

# Optimized solution
N, M = list(map(int, input().split()))

L = []
R = []
for i in range(M):
    l,r = list(map(int,input().split()))
    L.append(l)
    R.append(r)

if min(R)-max(L)+1 <= 0:
    print((0))
else:
    print((min(R)-max(L)+1))
# Original solution
N, M = list(map(int, input().split()))
c = [tuple(map(int, input().split())) for _ in range(M)]
nums = []
for i in range(0, M):
    mi = list(range(c[i][0], c[i][1]+1))
    nums.append(mi)
ans = set(nums[0])
dim = nums[1:]
print((len(ans.intersection(*dim))))

# Optimized solution
N, M = list(map(int, input().split()))
c = [tuple(map(int, input().split())) for _ in range(M)]
right = N
left = 1
for i in range(0, M):
    left = max(left, c[i][0])
    right = min(right, c[i][1])
print((len(list(range(left, right+1)))))
# Original solution
n, m= list(map(int, input().split()))

gates = [list(map(int, input().split())) for _ in range(m)]
count=0
for i in range(1, n+1):
    tf = True
    for j in gates:
        if j[0] > i or j[1] < i:
            tf = False
            break
        
    if tf:
        count+=1

print(count)


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

gates = [list(map(int, input().split())) for _ in range(m)]
up = min([i[1] for i in gates])
down = max([i[0] for i in gates])


if up >= down:
    print((up-down+1))
else:
    print((0))
# Original solution
N, M = [int(x) for x in input().split()]
def check(n, LR):
  return 1 if all(l <= n <=r for l, r in LR) else 0

LR = []
for _ in range(M):
  LR.append([int(x) for x in input().split()])

print((sum(check(n, LR) for n in range(1, N+1))))


# Optimized solution
N, M = [int(x) for x in input().split()]

def check(n, LR):
    return 1 if all(l <= n <=r for l, r in LR) else 0

L = 0
R = N
for _ in range(M):
    l, r = [int(x) for x in input().split()]
    if L < l:
        L = l
    if r < R:
        R = r

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


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

    gate = []

    for _ in range(m):
        ngate = list(map(int, input().split()))
        gate.append(ngate)
    

    result = 0
    for i in range(1, n + 1):
        for j in range(m):
            if gate[j][0] <= i and i <= gate[j][1]:
                if j == len(gate) - 1:
                    result += 1
                continue
            else:
                break

    
    return result


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

# Optimized solution


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

    A = []
    B = []

    for _ in range(m):
        a, b = list(map(int, input().split()))
        A.append(a)
        B.append(b)

         
    l = max(A)
    r = min(B)
    #print(l, r)

    if r >= l:
        return r - l + 1
    else:
        return 0

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



"""
for i in range(1, n + 1):
        for j in range(m):
            if gate[j][0] <= i and i <= gate[j][1]:
                if j == len(gate) - 1:
                    result += 1
                continue
            else:
                break
"""
# Original solution


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

    A = []
    B = []

    for _ in range(m):
        a, b = list(map(int, input().split()))
        A.append(a)
        B.append(b)

         
    l = max(A)
    r = min(B)
    #print(l, r)

    if r >= l:
        return r - l + 1
    else:
        return 0

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



"""
for i in range(1, n + 1):
        for j in range(m):
            if gate[j][0] <= i and i <= gate[j][1]:
                if j == len(gate) - 1:
                    result += 1
                continue
            else:
                break
"""

# Optimized solution
n, m = list(map(int , input().split()))
# L = []
# R = []

l_max = 0
r_min = 10 ** 6
for _ in range(m):
    l, r = list(map(int, input().split()))
    if l_max < l:
        l_max = l
    if r < r_min:
        r_min = r
if l_max <= r_min:
    print((r_min - l_max + 1))
else:
    print((0))
# Original solution
import sys
 
n=0
m=0
id_list=[]

n, m  = list(map(int, input().split()))
for i in range(0, m):
  gate_min, gate_max = list(map(int, input().split()))
  id_list.append([gate_min, gate_max])
 

result = 0
for i in range(1, n+1):
  is_passed = True
  for gate in id_list:
    if not((i >= gate[0]) and (i <= gate[1])) :
      is_passed = False
      break

  if is_passed:
    result = result + 1	
print(result)
sys.exit(0)

# Optimized solution
import sys
n=0
m=0

n, m  = list(map(int, input().split()))
_min, _max  = list(map(int, input().split()))
for i in range(1, m):
  gate_min, gate_max = list(map(int, input().split()))
  if (_max > gate_max):
    _max = gate_max
   
  if _min < gate_min:
    _min = gate_min
 
result_list = list([x for x in range(1, n+1) if (_min <= x) and (x <= _max)])
result = len(result_list)

print(result)
sys.exit(0)
# Original solution
n, m = list(map(int, input().split()))
ans = set()
for _ in range(m):
    l, r = list(map(int, input().split()))
    tmp = set(range(l, r+1))
    if ans:
        ans &= tmp
    else:
        ans = tmp

print((len(ans)))

# Optimized solution
n, m = list(map(int, input().split()))
ml, mr = -1, -1
for _ in range(m):
    l, r = list(map(int, input().split()))
    if ml == -1:
        ml = l
        mr = r
    else:
        if ml < l:
            ml = l
        if mr > r:
            mr = r
if mr - ml < 0:
    print((0))
else:
    print((mr - ml + 1))
# Original solution
N, M = list(map(int, input().split()))

gate = []

for i in range(M):
    l, r = list(map(int, input().split()))
    tmp = []
    for j in range(l, r + 1):
        tmp.append(j)
    gate.append(tmp)

gate_sum = set(gate[0])

for i in gate:
    gate_sum = gate_sum & set(i)

print((len(gate_sum)))

# Optimized solution
N, M = list(map(int, input().split()))

gate = []

l, r = list(map(int, input().split()))


for i in range(M-1):
    tmpl, tmpr = list(map(int, input().split()))
    if l < tmpl:
        l = tmpl
    if tmpr < r:
        r = tmpr


if r - l < 0:
    print((0))
else:
    print((r - l + 1))
# Original solution
N, M = list(map(int, input().split()))

cards = list(range(1, N+1))
for _ in range(M):
    l, r = list(map(int, input().split()))
    cards = set(cards) & set(range(l, r+1))
print((len(cards)))

# Optimized solution
N, M = list(map(int, input().split()))

L, R = [], []
for _ in range(M):
    l, r = list(map(int, input().split()))
    L.append(l)
    R.append(r)
diff = min(R) - max(L) + 1
if diff > 0:
    print(diff)
else:
    print((0))
# Original solution
N, M = list(map(int, input("").split(" ")))
permit = [list(map(int, input("").split(" "))) for i in range(M)]
able = [0 for i in range(N)]
for i,j in permit:
  for k in range(i,j+1):
    able[k-1] += 1
print((able.count(M)))


# Optimized solution
N, M = list(map(int, input("").split(" ")))
permit = [list(map(int, input("").split(" "))) for i in range(M)]
l,r = 1, N
for i,j in permit:
  l = max(l, i)
  r = min(r, j)
print((max(r-l+1,0)))
# Original solution

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

li = []

ans = 0

for i in range(M):
    x,y = list(map(int,input().split()))
    li.append([x,y])

for i in range(1,N+1):
    count = 0
    for j,k in li :
        if j <= i and i <= k :
            count += 1
            if count == len(li):
                ans += 1
        else:
            break

print(ans)


# Optimized solution

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

L = []
R = []

ans = 0

for i in range(M):
    l,r = list(map(int,input().split()))
    L.append(l)
    R.append(r)

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

for i in range(1,N+1):
    if Lmax <= i and i <= Rmin:
        ans += 1

print(ans)
# Original solution
n,m=list(map(int,input().split()))
a=[[int(i) for i in input().split()] for l in range(m)]
l=[int(i) for i in range(1,n+1)]
for x in a:
    l=set(l)&set([int(i) for i in range(x[0],x[1]+1)])
print((len(l)))

# Optimized solution
n,m=list(map(int,input().split()))
a=[[int(i) for i in input().split()] for l in range(m)]
b=0
s=100000000000
for x in a:
    b=max(b,x[0])
    s=min(s,x[1])
print((max(0,s-b+1)))
# Original solution
N,M= list(map(int,input().split()))
gate=[list(map(int,input().split())) for i in range(M)]
count2=0

for i in range(1,N+1):
  count=0
  for j in gate:
    if j[0]<=i<=j[1]:
      count+=1
    else:
      break
  if count==M:
    count2+=1
print(count2)

# Optimized solution
N,M= list(map(int,input().split()))
gate=[list(map(int,input().split())) for i in range(M)]
count2=0

min1=1
max1=N

for j in gate:
  if j[0]>min1:
    min1=j[0]
  if j[1]<max1:
    max1=j[1]
if max1>=min1:
  print((max1-min1+1))
else:
  print((0))
# Original solution
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    lr = [list(map(int, input().rstrip('\n').split())) for _ in range(m)]
    a = [0] * (n + 2)
    for l, r in lr:
        a[l] += 1
        a[r + 1] -= 1
    t = 0
    cnt = 0
    for i in range(1, n + 1):
        t += a[i]
        if t == m:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()


# Optimized solution
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    x = [0] * (n + 2)
    for i in range(m):
        l, r = list(map(int, input().rstrip('\n').split()))
        x[l] += 1
        x[r+1] -= 1
    t = 0
    cnt = 0
    for i in range(n + 2):
        t += x[i]
        if t == m:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()
# Original solution
# coding: utf-8

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

ids = list(range(1, N+1))

for _ in range(M):
    (L, R) = list(map(int, input().split(" ")))
    ids = [i for i in ids if L <= i <= R]
print((len(ids)))


# Optimized solution
# coding: utf-8

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

ids = list(range(1, N+1))

L_max = 0
R_min = 10000000000000000000
for _ in range(M):
    (L, R) = list(map(int, input().split(" ")))
    if L > L_max:
        L_max = L
    if R < R_min:
        R_min = R

ids = [i for i in ids if L_max <= i <= R_min]
print((len(ids)))
# Original solution
n, m = list(map(int,input().split()))
s = {}
for i in range(m):
    l, r = list(map(int,input().split()))
    if i == 0:
        s = set(range(l,r+1))
    else:
        s = set(range(l, r+1)) & s
print((len(s)))

# Optimized solution
import heapq
n, m = list(map(int,input().split()))
l = []
r = []
for i in range(m):
    line = list(map(int,input().split()))
    l.append(-line[0])
    r.append(line[1])
heapq.heapify(l)
heapq.heapify(r)
l_max = - heapq.heappop(l)
r_min = heapq.heappop(r)
if l_max > r_min:
    print((0))
else:
    print((r_min-l_max+1))
# Original solution
from functools import reduce
N,M=list(map(int,input().split()))
lst=[]
for i in range(M):
    L,R=list(map(int,input().split()))
    lst.append(set(range(L,R+1)))
print((len(reduce(lambda a,b:a&b,lst))))

# Optimized solution
from functools import reduce
N,M=list(map(int,input().split()))
x=set()
L,R=list(map(int,input().split()))
x=set(range(L,R+1))
for i in range(M-1):
    L,R=list(map(int,input().split()))
    x=x & set(range(L,R+1))
print((len(x)))
# Original solution
from functools import reduce
N,M=list(map(int,input().split()))
x=set()
L,R=list(map(int,input().split()))
x=set(range(L,R+1))
for i in range(M-1):
    L,R=list(map(int,input().split()))
    x=x & set(range(L,R+1))
print((len(x)))    

# Optimized solution
import sys
N,M=list(map(int,input().split()))
_min,_max=list(map(int,input().split()))
for i in range(M-1):
   L,R=list(map(int,input().split()))
   if R<_min or L>_max:
       print((0))
       sys.exit()
   _min,_max=max(_min,L),min(_max,R)
print((_max+1-_min))
# Original solution
n,m = list(map(int, input().split()))
LR = [list(map(int, input().split())) for i in range(m)]
ANS = []
for i in range(m):
  ANS.append(set(range(LR[i][0],LR[i][1]+1)))
  
X = ANS[0]
for i in range(1,m):
  X = X&ANS[i]
  
print((len(X)))

# Optimized solution
n,m = list(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)))
# Original solution
N,M = list(map(int,input().split()))
A = set(n for n in range(1,N+1))

for m in range(M):
  L,R = list(map(int,input().split()))
  B = set(n for n in range(L,R+1))
  A = A&B

print((len(A)))

# Optimized solution
N,M = list(map(int,input().split()))
L1 = 0
R1 = N

for m in range(M):
  L2,R2 = list(map(int,input().split()))
  L1 = max(L1,L2)
  R1 = min(R1,R2)
  
print((max(0,R1-L1+1)))
# Original solution
N,M = list(map(int,input().split()))
L = [0] * M
R = [0] * M
for i in range (0,M):
    L[i],R[i] = list(map(int,input().split()))

S = [set(range(L[i],R[i]+1)) for i in range(0,M)]

temp = S[0]

for i in range(1,M):
    temp = temp & S[i]

print((len(temp)))

# Optimized solution
N,M = list(map(int,input().split()))
L = [0] * M
R = [0] * M
for i in range (0,M):
    L[i],R[i] = list(map(int,input().split()))

print((len(list(range(max(L),min(R)+1)))))
# Original solution
n,m = list(map(int, input().split()))

Answer = [p+1 for p in range(n)]

for i in range(m):
  l,r = list(map(int, input().split()))
  Answer = [q for q in Answer if q >=l and q <= r]
  
print((len(Answer)))  

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

min_value = 1
max_value = n

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

Answer = max(max_value - min_value +1,0)

print(Answer)
# Original solution
N, M = list(map(int,input().split()))


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

set_ans = set(range(int(LR[0][0]),int(LR[0][1]+1)))
for m in range(1,M):
  set_ans = set_ans & set(range(int(LR[m][0]),int(LR[m][1])+1))
  if len(set_ans) == 0:
    break
print((len(set_ans)))

# Optimized solution
N, M = list(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))
# Original solution
N, M = [int(n) for n in input().split()]
S = set(range(1, N+1))
for i in range(M):
    l, r = [int(n) for n in input().split()]
    S &= set(range(l, r+1))
print((len(S)))


# Optimized solution
N, M = [int(n) for n in input().split()]
L, R = (1, N)
for i in range(M):
    l, r = [int(n) for n in input().split()]
    L, R = (max(l, L), min(r, R))
print((max(0, R-L+1)))
# Original solution
def main():
    N, M = list(map(int, input().split()))
    #L = [0] * M
    #R = [0] * M
    
    for i in range(M):
        L, R = list(map(int, input().split()))
        A = [j for j in range(L,R+1)]
        if i == 0:
            tmp = set(A)
            continue
        set_A = set(A)
        tmp = set(list(set_A.intersection(tmp)))
    
    print((len(tmp)))



main()

# Optimized solution
def main():
    N, M = list(map(int, input().split()))
    
    left = 0
    right = float('inf')
    
    for i in range(M):
        L, R = list(map(int, input().split()))
        left = max(left, L)
        right = min(right, R)
    
    if left <= right:
        print((right - left + 1))
    else:
        print((0))

main()
# Original solution
N,M = list(map(int,input().split()))
LRlist = [tuple(map(int,input().split())) for _ in range(M)]

min_n = 0
max_n = N
for l,r in LRlist:
    min_n = l if l>min_n else min_n
    max_n = r if r<max_n else max_n

if max_n-min_n >= 0:
    print((max_n-min_n+1))
else:
    print((0))

# Optimized solution
N,M = list(map(int,input().split()))
LRlist = [tuple(map(int,input().split())) for _ in range(M)]

min_n = 0
max_n = N
for l,r in LRlist:
    min_n = l if l>min_n else min_n
    max_n = r if r<max_n else max_n

ans = max_n-min_n+1
print((ans if ans>0 else 0))
# Original solution
N, M = list(map(int, input().split()))
L, R = list(map(int, input().split()))
ans = set(range(L, R + 1))
for i in range(M - 1):
    L, R = list(map(int, input().split()))
    ans &= set(range(L, R + 1))
print((len(ans)))


# Optimized solution
N, M = list(map(int, input().split()))
llim = 0
rlim = N + 1
for _ in range(M):
    L, R = list(map(int, input().split()))
    llim = max(llim, L)
    rlim = min(rlim, R)
print((max(0, rlim + 1 - llim)))
# Original solution
n, m = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(m)] 
e = [list(range(i[0],i[1]+1)) for i in e]

x = set(e[0])
for i in e:
  x = x & set(i)
  
print((len(list(x))))

# Optimized solution
n, m = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(m)] 

start, end = e[0][0], e[0][1]
for i in e: 
  if start < i[0]:
    start = i[0]
  if end > i[1]:
    end = i[1]
  if start > end:
    break

if start > end:
  print((0))
else:
  print((end-start+1))
# Original solution
import collections

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

l = []
l2 = []
count = 0

for i in range(m):
    array = list(map(int, input().split()))
    l.append(array)

for i in range(m):
    for j in range(l[i][0], l[i][1]+1):
        l2.append(j)

c = collections.Counter(l2)
d = sorted(list(c.values()),reverse=True)

for i in d:
    if i == m:
        count += 1

print(count)


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

x = []
y = []

for i in range(m):
    array = list(map(int, input().split()))
    x.append(array[0])
    y.append(array[1])

a = max(x[:])
b = min(y[:])

print((len(list(range(a, b+1)))))
# Original solution
from collections import defaultdict
n, m = list(map(int, input().split()))
card = defaultdict(int)
for i in range(m):
    l, r = list(map(int, input().split()))
    for j in range(l, r+1):
        card[j] += 1
cnt = 0
for i, v in list(card.items()):
    if v == m:
        cnt += 1
print(cnt)


# Optimized solution
from collections import defaultdict
n, m = list(map(int, input().split()))
left = []
right = []
for i in range(m):
    l, r = list(map(int, input().split()))
    left.append(l)
    right.append(r)

if min(right) >= max(left):
    print((min(right) - max(left) + 1))
else:
    print((0))
# Original solution
N,M = list(map(int,input().split()))
lst = [list(map(int,input().split())) for _ in range(M)]

L1,R1 = lst[0]
common = 0
for i in range(L1,R1+1):

    count = 0
    for j in range(1,M):
        Lj,Rj = lst[j]   
        if i < Lj or i > Rj:
            break
        else:
            count += 1

    if count == M-1:
        common += 1

print(common)

# Optimized solution
N,M = list(map(int,input().split()))
maxL = 1
minR = 100000
for i in range(M):
    Li,Ri = list(map(int,input().split()))
    maxL = max(maxL,Li)
    minR = min(minR,Ri)
        
if minR - maxL >= 0:
    print((minR-maxL+1))
else:
    print((0))
# Original solution
N,M = list(map(int,input().split()))
N_List = list(range(1,N+1))
for i in range(M):
    st,sp = list(map(int,input().split()))
    N_List = set(list(range(st,sp + 1))) & set(N_List)
print((len(N_List)))

# Optimized solution
N,M = list(map(int,input().split()))
for i in range(M):
    st,sp = list(map(int,input().split()))
    if i == 0:
        N_List = [st,sp]
    else:
        if st > N_List[0]:
            N_List[0] = st
        if sp < N_List[1]:
            N_List[1] = sp   
print((len(list(range(N_List[0],N_List[1]+1)))))
# Original solution
N, M = list(map(int, input().split()))
cnt = 0
li = []

for i in range(N):
    li.append(M)

# print(li)

for i in range(M):
    L, R = list(map(int, input().split()))
    for j in range(L-1, R):
        li[j] = li[j] - 1
        # print(li)

for i in range(0, len(li)):
    if li[i] == 0:
        cnt += 1

print(cnt)

# Optimized solution
N, M = list(map(int, input().split()))

L = []

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

res = [L[0][0], L[0][1]]

for i in range(M):
    res = [max(res[0], L[i][0]), min(res[1], L[i][1])]

if res[1] < res[0]:
    print((0))
else: 
    print((res[1] - res[0] + 1))
# Original solution
n, m = list(map(int, input().split()))
left, right = 1, n
for _ in range(m):
  l, r = list(map(int, input().split()))
  if l > left:
    left = l
  if r < right:
    right = r
  if left > right:
    print((0))
    quit()
print((right - left + 1))

# Optimized solution
import sys
n, m, *lst = list(map(int, sys.stdin.read().split()))
l = max(lst[::2])
r = min(lst[1::2])
if l > r:
  print((0))
else:
  print((r - l + 1))
# Original solution
_, _, *lst = list(map(int, open(0).read().split()))
l, r = max(lst[::2]), min(lst[1::2])
print((0 if l > r else r - l + 1))

# Optimized solution
def main():
  _, _, *lst = list(map(int, open(0).read().split()))
  l, r = max(lst[::2]), min(lst[1::2])
  print((0 if l > r else r - l + 1))

main()
# Original solution
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(m)]
l = []
r = []
for i in range(m):
  l.append(lr[i][0])
  r.append(lr[i][1])
ans = 0
for i in range(1, n+1):
  if max(l) <= i <= min(r):
    ans += 1
print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(m)]
l = []
r = []
for i in range(m):
  l.append(lr[i][0])
  r.append(lr[i][1])
lm = max(l)
rm = min(r)
ans = 0
for i in range(1, n+1):
  if lm <= i <= rm:
    ans += 1
print(ans)
# Original solution
n, m = list(map(int, input().split()))
ls = []
rs = []
for i in range(m):
    l, r = list(map(int, input().split()))
    ls.append(l)
    rs.append(r)
n_all = 0
for card in range(1,n+1):
    auth = True
    for gate in range(m):
        if not ls[gate] <= card <= rs[gate]:
            auth = False
    if auth == True:
        n_all += 1
print(n_all)

# Optimized solution
n, m = list(map(int, input().split()))
ls = []
rs = []
for i in range(m):
    l, r = list(map(int, input().split()))
    ls.append(l)
    rs.append(r)
n_all = min(rs) - max(ls) + 1
if n_all < 0: n_all =0
print(n_all)