CodeEff/ECCO huggingface dataset:
# Original solution
n ,m = list(map(int, input().split()))
L_R = []
for i in range(m):
    l, r = list(map(int, input().split()))
    tmp = set()
    for i in range(l, r + 1):
        tmp.add(i)
    L_R.append(tmp)

result = L_R[0]
for target_set in L_R:
    result = result.intersection(target_set)

print((len(result)))

# Optimized solution
n ,m = list(map(int, input().split()))
result = set()
first = True
for i in range(m):
    l, r = list(map(int, input().split()))
    tmp = set()
    for i in range(l, r + 1):
        tmp.add(i)
    if first:
        result = tmp
        # print("tmp {}".format(tmp))
        first = False
    else:
        result = result.intersection(tmp)
        # print("result {} tmp {}".format(result, tmp))

print((len(result)))
# Original solution
n ,m = list(map(int, input().split()))
result = set()
tmp = set()
first = True
for i in range(m):
    l, r = list(map(int, input().split()))
    tmp = {i for i in range(l, r + 1)}
    if first:
        result = tmp
        first = False
    else:
        result = result.intersection(tmp)

print((len(result)))

# 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 max(L) <= min(R):
    print((min(R) - max(L) + 1))
else:
    print((0))
# Original solution
N,M = list(map(int, input().split()))

first,last = list(map(int,input().split(" ")))
set_cnt = set()
for i in range(first,last+1):
  set_cnt.add(i)


for m in range(M-1):
  _first, _last = list(map(int,input().split(" ")))
  _set_cnt = set()
  for _ in range(_first,_last+1):
    _set_cnt.add(_)
  
  set_cnt = set_cnt & _set_cnt

print((len(set_cnt)))

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

for m in range(M-1):
  _first, _last = list(map(int,input().split(" ")))
  min_set_cnt = max(min_set_cnt,_first)
  max_set_cnt = min(max_set_cnt,_last)


ans = max(max_set_cnt - min_set_cnt + 1, 0)

print(ans)
# Original solution
N,M=list(map(int,input().split()))
x=set([int(x) for x in range(1,N+1)])
for i in range(M):
    L,R=list(map(int,input().split()))
    y=set(range(L,R+1))
    x=x & y
print((len(x)))

# 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)
a=max(l)
b=min(r)
x=list(range(a,b+1))
print((len(x)))
# Original solution
# -*- coding: utf-8 -*-

N, M = list(map(int, input().split()))
_count = [0]*N

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

print(_count.count(M))

# Optimized solution
# -*- coding: utf-8 -*-

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

for i in range(M):
    L, R = list(map(int, input().split()))
    if _L < L:
        _L = L
    if R < _R:
        _R = R 

print(str(max(_R-_L+1,0)))
# Original solution
n, m = list(map(int, input().split()))
c = [0]*(n+1)
for i in range(m):
    l, r = list(map(int, input().split()))
    for j in range(l, r+1):
        c[j] += 1

ans = 0
for ci in c:
    if ci==m:
        ans+=1
print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
c = [0]*(n+1)
L, R = [], []
for i in range(m):
    l, r = list(map(int, input().split()))
    L.append(l)
    R.append(r)
maxL = max(L)
minR = min(R)
print((max(minR+1-maxL, 0)))
# Original solution
N,M = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(M)]
ans = [0 for _ in range(N)]
for i in range(M):
    start = LR[i][0]
    end = LR[i][1]
    for j in range(start-1, end):
        ans[j] += 1
cnt = 0
for k in ans:
    if k == M:
        cnt += 1

print(cnt)

# Optimized solution
N,M = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(M)]
L1 = []
R1 = []
for i in range(M):
    L1.append(LR[i][0])
    R1.append(LR[i][1])
if min(R1)>=max(L1):
    print((min(R1)-max(L1)+1))
else:
    print((0))
# Original solution
import sys
N, M = list(map(int, input().split()))

ans = {i for i in range(1, N + 1)}
for line in sys.stdin:  # much faster than input
    L, R = list(map(int, line.split()))
    allowed = {i for i in range(L, R + 1)}
    ans = ans & allowed

print((len(ans)))


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

Lmax = 1
Rmin = N
for line in sys.stdin:  # much faster than input
    L, R = list(map(int, line.split()))
    Lmax = max(L, Lmax)
    Rmin = min(R, Rmin)

print((max(0, Rmin - Lmax + 1)))
# Original solution
L, R = [[], []]
N, M = list(map(int, input().split()))
for i in range(M):
  l, r = list(map(int, input().split()))
  L.append(l)
  R.append(r)

count = 0
for i in range(1,N+1):
  count = min(R)-max(L)+1
  
print(('%d' % count))

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

count = 0
minR = min(R)
maxL = max(L)
if minR < maxL:
  count = 0
else:
  for i in range(maxL, minR+1):
    if i <= N:
      count = count + 1
      
print(('%d' % count))
# 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()))

count = 0
for i in range(1,n+1):
	
	if max(l) <= i <= min(r):
		count += 1
			
print(count)
		

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

count = 0

max_l = max(l)
min_r = min(r)

for i in range(1,n+1):
	
	if max_l <= i <= min_r:
		count += 1
			
print(count)
# Original 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)

res = 0
for i in range(1, n+1):
    if max(L) <= i <= min(R):
        res += 1
print(res)

# 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)


res = min(R) - max(L) + 1
if res < 0:
    print((0))
else:
    print(res)
# Original solution
n,m = list(map(int,input().split()))
a = {i for i in range(1,n+1)}
for _ in range(m):
  l,r = list(map(int,input().split()))
  temp = list(range(l,r+1))
  a = a.intersection(temp)
print((len(a)))


# Optimized solution
n,m = list(map(int,input().split()))
a_min = 1
a_max = n
for _ in range(m):
  l,r = list(map(int,input().split()))
  a_min = max(a_min,l)
  a_max = min(a_max,r)
if a_min>a_max:
  print((0))
else:
  print((a_max-a_min+1))
# Original solution
n, m = list(map(int, input().split()))
init_set = None

for i in range(m):
  l,m = list(map(int, input().split()))
  tmp_set = set([i for i in range(l,m+1)])
  if init_set is None:
    init_set = tmp_set
  else:
    init_set = init_set.intersection(tmp_set)
print((min(n,len(init_set))))

# Optimized solution
n, m = list(map(int, input().split()))
low = 0
high = 100001

for i in range(m):
  l,m = list(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)))
# Original solution
N, M = list(map(int, input().split()))
gate = [0] * N
maxL, minR = 0, N
for i in range(M):
    L, R = list(map(int, input().split()))
    maxL = max(maxL, L-1)
    minR = min(minR, R-1)
    for j in range(maxL, minR+1):
        gate[j] += 1

count = 0
for k in gate:
    if k == M:
        count += 1
print(count)


# Optimized solution
N, M = list(map(int, input().split()))
gate = [0] * N
maxL, minR = 0, N
for i in range(M):
    L, R = list(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))
# Original solution
n, m = [int(x) for x in input().split(' ')]
l = []
r = []
for i in range(m):
    l_tmp, r_tmp = [int(x) for x in input().split(' ')]
    l.append(l_tmp)
    r.append(r_tmp)

result = 0
card_id = l[0]
for j in range(n):
    for locked_l, locked_r in zip(l, r):
        if not (locked_l <= card_id + j <= locked_r):
            break
    else:   
        result += 1

print(result)


# Optimized solution
n, m = [int(x) for x in input().split(' ')]
l = []
r = []
for i in range(m):
    l_tmp, r_tmp = [int(x) for x in input().split(' ')]
    l.append(l_tmp)
    r.append(r_tmp)

l_max = max(l)
r_min = min(r)

result = 0
if l_max <= r_min:
    result = r_min - l_max + 1
print(result)
# Original solution
N,M=list(map(int,input().split(" ")))
List=[input().split(" ") for _ in range(M)]

Ans=0
for i in range(1,N+1):
    count=0
    for j in range(M):
        if int(List[j][0])<=i<=int(List[j][1]):
            count+=1
    if count==M:
        Ans+=1

print(Ans)

# Optimized solution
n, m = list(map(int, input().split()))
 
R, L = [], []
for i in range(m):
    l, r = list(map(int, input().split()))
    R.append(r)
    L.append(l)
  
print((max(min(R) - max(L) + 1, 0)))
# Original solution
n, m = list(map(int, input().split()))
gates = []
count = 0
for gate in range(m):
    gates.append(list(map(int, input().split())))
for card in range(n):
    card+=1
    token = True
    for gate in range(m):
        if (card < gates[gate][0] or gates[gate][1] < card):
            token = False
            break
    if token:
        count +=1
print(count)

# Optimized solution
n, m = list(map(int, input().split()))
gates = []
for i in range(m):
    gates.append(list(map(int, input().split())))
candidate = [1, n]
for gate in gates:
    if candidate[0] < gate[0]:
        candidate[0] = gate[0]
    if gate[1] < candidate[1]:
        candidate[1] = gate[1]
if (candidate[1] >= candidate[0]):
    print((candidate[1] - candidate[0] + 1))
else:
    print((0))
# Original solution
import sys
input = sys.stdin.readline


def prison():
    N, M = (int(_) for _ in input().split())
    id_l = set(range(1, N+1))
    for _ in range(M):
        L, R = (int(_) for _ in input().split())
        id_l = id_l & set(range(L, R+1))
    return len(id_l)


print((prison()))

# Optimized solution
def prison():
    N, M = (int(i) for i in input().split())
    L_max = 0
    R_min = float("inf")
    for _ in range(M):
        L, R = (int(i) for i in input().split())
        L_max = max(L, L_max)
        R_min = min(R, R_min)
        result = max(R_min - L_max + 1, 0)
    return result


print((prison()))
# Original solution
n,m = list(map(int,input().split()))
A =[]
for _ in range(m):
    l,r = list(map(int,input().split()))
    tmp = [i for i in range(l,r+1)]
    A += tmp

from collections import Counter
cnt = Counter(A)
ans = []

for k,v in list(cnt.items()):
    if v == m:
        ans.append(k)
print((len(ans)))


# 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)
tmp = min(R)-max(L)+1

if tmp >= 1:
    print(tmp)
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))

master_card = set()
L0, R0 = list(map(int, input().split()))
master_card |= set(range(L0, R0 + 1))

for i in range(1, M):
    Li, Ri = list(map(int, input().split()))
    master_card &= set(range(Li, Ri + 1))

print((len(master_card)))


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

shita = 1
ue = 100000
for i in range(0, M):
    Li, Ri = list(map(int, input().split()))
    if shita < Li:
        shita = Li
    if Ri < ue:
        ue = Ri

print((max(ue - shita + 1, 0)))
# Original solution
# coding: utf-8
# Your code here!

N,M = list(map(int,input().split()))
L,R = list(map(int,input().split()))
curSet = set(range(L,R+1))

for i in range(2,M+1):
    L,R = list(map(int,input().split()))
    curVec = set(range(L,R+1))

    curSet = curSet.intersection(curVec)
    
print((len(curSet)))

# Optimized solution
# coding: utf-8
# Your code here!

N,M = list(map(int,input().split()))
L = [0]*M
R = [0]*M
#Li,Ri = map(int,input().split())

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

validID = min(R)-max(L)+1

if (validID>0):
    print(validID)
else:
    print("0")
# Original solution
n, m = list(map(int, input().split()))
def ip(l1, l2):
  ans = []
  if len(l1) != len(l2):
    print('length mismatch')
  else:
    for i in range(len(l1)):
      ans.append(l1[i]*l2[i])
    return ans
g = []
for i in range(m):
  g.append(list(map(int, input().split())))
l = [1 for i in range(n)]
for i in range(m):
  temp_l = [0 for i in range(n)]
  for j in range(g[i][0]-1, g[i][1]):
    temp_l[j-1] = 1
  l = ip(l, temp_l)
ans = sum(l)
print(ans)
  
  

# Optimized solution
n, m = list(map(int, input().split()))
max_l = 0
min_r = n
for i in range(m):
  l, r = list(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)
# Original solution
N, M = list(map(int, input().split()))
ID = [i for i in range(1, N+1)]
for i in range(M):
    L, R = list(map(int, input().split()))
    r = [i for i in range(L, R+1)]
    ID = set(ID) & set(r)
    if len(ID) == 0:
        print((0))
        exit()
if len(ID) > N:
    print(N)
else:
    print((len(ID)))

# Optimized solution
N, M = list(map(int, input().split()))
LR = [list(map(int,input().split())) for i in range(M)]
max = 1
min = 100000
for i in range(M):
    if max < LR[i][0]:
        max = LR[i][0]
    if min > LR[i][1]:
        min = LR[i][1]
if max <= min:
    print((min - max + 1))
else:
    print((0))
# Original solution
N,M=list(map(int,input().split()))
X=[0]*N
c=0
for i in range(M):
    L,R=list(map(int,input().split()))
    for j in range(L-1,R):
        X[j]+=1
for i in X:
    if i==M:
        c+=1
print(c)

# Optimized solution
N,M=list(map(int,input().split()))
L=[list(map(int,input().split()))for i in range(M)]
r=N
l=0
for i in range(M):
    l=max(l,L[i][0])
    r=min(r,L[i][1])
if r-l+1<0:
    print((0))
else:
    print((r-l+1))
# Original solution
n, m = list(map(int, input().split()))
d = []
for i in range(m):
    a, b = list(map(int, input().split()))
    d.append(list(range(a, b+1)))

b = d[0]
for i in d:
    src_set = set(b)
    tag_set = set(i)
    matched_list = list(src_set & tag_set)

print((len(matched_list)))

# Optimized solution
n, m = list(map(int, input().split()))
a, b = list(map(int, input().split()))
d = list(range(a, b+1))
for i in range(m-1):
    a, b = list(map(int, input().split()))
    src_set = set(d)
    tag_set = set(list(range(a, b+1)))
    d = list(src_set & tag_set)
print((len(d)))
# Original solution
n, m = list(map(int, input().split()))
a, b = list(map(int, input().split()))
d = list(range(a, b+1))
for i in range(m-1):
    a, b = list(map(int, input().split()))
    src_set = set(d)
    tag_set = set(list(range(a, b+1)))
    d = list(src_set & tag_set)
print((len(d)))

# Optimized solution
n,m=list(map(int,input().split()))
max_ = 0
min_ = n+1
for i in range(m):
  a,b=list(map(int, input().split()))
  max_ = max(max_, a)
  min_ = min(min_, b)
#print(max_, min_)
if(min_ < max_):print((0))
else:print((min_-max_+1))
# Original solution
#!/usr/bin/env python3
n, m = list(map(int, input().split()))
li = [0] * (n + 1)
for _ in range(m):
    l, r = [int(x) - 1 for x in input().split()]
    li[0] += 1
    li[l] -= 1
    li[r + 1] += 1
    li[n] -= 1
for i in range(n):
    li[i + 1] += li[i]
print((sum(x <= 0 for x in li[:-1])))


# Optimized solution
n, m = list(map(int, input().split()))
ml, mr = 0, n
for i in range(m):
    l, r = [int(x) - 1 for x in input().split()]
    ml = max(ml, l)
    mr = min(mr, r)
print((max(0, mr - ml + 1)))
# Original solution
N, M = list(map(int, input().split()))
LR = [None]*M

for i in range(M):
    l, r = list(map(int, input().split()))
    if i==0:
        ans = set(range(l, r+1))
    else:
        ans = ans & set(range(l, r+1))

print((min(len(ans), N)))

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

for i in range(M):
    l, r = map(int, input().split())
    
    if i==0:
        left = l
        right = r

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

print(0) if left>right else print(right-left+1)
# Original solution
N, M = list(map(int, input().split()))
L = [0] * M
R = [0] * M
count = 0
for i in range(M):
    L[i], R[i] = list(map(int, input().split()))
for i in range(N):
    clear = 0
    for j in range(M):
        if L[j] <= i + 1 <= R[j]:
            clear += 1
        else:
            break
    if clear == M:
        count += 1
print(count)


# Optimized solution
N, M = list(map(int, input().split()))
L = [0] * M
R = [0] * M
count = 0
for i in range(M):
    L[i], R[i] = list(map(int, input().split()))
Lmax = max(L)
Rmin = min(R)
if Lmax <= Rmin:
    print((Rmin - Lmax + 1))
else:
    print((0))
# Original solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))
c = set(range(l, r+1))
for _ in range(m-1):
    l, r = list(map(int, input().split()))
    c = c & set(range(l, r+1))
print((len(c)))


# Optimized solution
n, m = list(map(int, input().split()))
tl, tr = 1, n
for _ in range(m):
    l, r = list(map(int, input().split()))
    if tr<l or tl>r:
        tl, tr = 1, 0; break
    else:
        tl = l if tl<l else tl
        tr = r if tr>r else tr
print((tr-tl+1))
# Original solution
n,m=list(map(int,input().split()))
a=[]
te=set(range(1,n+1))
for i in range(m):
    x,y=list(map(int,input().split()))
    tes=set(range(x,y+1))
    te=te&tes
print((len(te)))

# Optimized solution
n,m=list(map(int,input().split()))
l=1;ma=n
te=set(range(1,n+1))
for i in range(m):
    x,y=list(map(int,input().split()))
    l=max(x,l)
    ma=min(ma,y)
print((abs(l-ma)+1 if l<=ma else 0))
# Original solution
import collections

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

def addNum(ans):
    a, b = list(map(int, input().split()))
    for i in range(a, b+1):
        ans.append(i)

for i in range(m):
    addNum(ans)

res = collections.Counter(ans)

count = 0
for i in list(res.values()):
    if i == m:
        count += 1

print(count)

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

for i in range(m):
    a, b = list(map(int, input().split()))
    ans2 = [i for i in range(a, b+1)]
    if ans1 == []:
        ans1 = ans2
    else:
        ans1 = list(set(ans1) & set(ans2))

print((len(ans1)))
# Original solution
n, m = list(map(int, input().split()))
ans1 = []

for i in range(m):
    a, b = list(map(int, input().split()))
    ans2 = [i for i in range(a, b+1)]
    if ans1 == []:
        ans1 = ans2
    else:
        ans1 = list(set(ans1) & set(ans2))

print((len(ans1)))

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

maxNum = 0
minNum = 0

for i in range(m):
    a, b = list(map(int, input().split()))
    if maxNum == 0:
        maxNum = b
    else:
        if maxNum > b:
            maxNum = b

    if minNum == 0:
        minNum = a
    else:
        if minNum < a:
            minNum = a

if maxNum - minNum >= 0:
    print((maxNum - minNum + 1))
else:
    print((0))
# Original solution
n,m = list(map(int,input().split()))
g = [tuple(map(int,input().split())) for i in range(m)]
ans=0
for i in range(1,n+1):
  if all([t[0]<=i and i<=t[1] for t in g]):
    ans+=1
print(ans)

# Optimized solution
n,m = list(map(int,input().split()))
l = []
r = []
for i in range(m):
  t = tuple(map(int,input().split()))
  l.append(t[0])
  r.append(t[1])
l,r=max(l),min(r)
if l<=r:
  print((abs(l-r)+1))
else:
  print((0))
# Original solution
N,M = list(map(int, input().split()))
A = []
for i in range(M):
    L, R = list(map(int, input().split()))
    B = list(range(L,R+1))
    A.append(B)
S = set(A[0])
for i in range(M):
    S = set(S) & set(A[i])
print((len(S)))
    

# Optimized solution
N, M = list(map(int, input().split()))
L = 0
R = 10**5 +2
for i in range(M):
    l,r = list(map(int, input().split()))
    if L < l:
        L = l
    if R > r:
        R = r
ans = 0
for i in range(1,N+1):
    if L <= i <= R:
        ans += 1
        
print(ans)
# Original solution
import collections as co
N,M =list(map(int,input().split()))
card=[]
ans=0
for i in range(M):
  st,en=list(map(int,input().split()))
  for cn in range(st,en+1):
    card.append(cn)
vv=co.Counter(card)
for k in range(N+1):
  if M==vv[k]:
    ans+=1
print(ans)


# Optimized solution
N,M =list(map(int,input().split()))
la=1
rm=N
for i in range(M):
  a,b=list(map(int,input().split()))
  la=max(la,a)
  rm=min(rm,b)
print((max(0,rm-la+1)))
# Original solution
n,m = list(map(int, input().split(' ')))
keys = set(range(1,n+1))
for _ in range(m):
	a,b = list(map(int, input().split(' ')))
	keys = keys.intersection(set( range(a,b+1)  ))
	if len(keys) == 0: break
print(len(keys))


# Optimized solution
n,m = list(map(int, 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 = list(map(int, input().split(' ')))
	r = intersection(r, [a,b])
	if len(r) == 0: break
print((r[1] - r[0] + 1) if r else 0)
# Original solution
n,m = list(map(int, 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 = list(map(int, input().split(' ')))
	r = intersection(r, [a,b])
	if len(r) == 0: break
print((r[1] - r[0] + 1) if r else 0)


# Optimized solution
n,m = list(map(int, 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 = list(map(int, input().split(' ')))
	r = intersection(r, [a,b])
	if len(r) == 0: break
print((r[1] - r[0] + 1) if r else 0)
# Original solution
N, M = list(map(int,input().split()))
LR = [list(map(int,input().split())) for i in range(M)]

l = [0] * N
count = 0
for i in range(M):
  for j in range(LR[i][1] - LR[i][0] + 1):
    l[LR[i][0]+j-1] += 1
else:
  for i in l:
    if i == M:
      count += 1
  else:
    print(count)
    

# 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)

max_l = max(L)
min_r = min(R)
print((max(min_r - max_l + 1, 0)))
# Original solution
def main():
	import itertools
	n,m = list(map(int,input().split()))
	l = [list(map(int,input().split())) for i in range(m)]
	# print(n,m)
	# print(l)
	ans = 0
	ans_pre = 0

	for ID in range(1,n+1):
		# print("---ID = {}---".format(ID))
		ans_pre = 0
		for i in range(m):
			# print("-i = {}-".format(i))
			li = l[i][0]
			# print("li = {}".format(li))
			ri = l[i][1]
			# print("ri = {}".format(ri))
			if ID >= li and ID <= ri:
				ans_pre += 1
			# print("ans_pre = {}".format(ans_pre))
		if ans_pre == m:
			ans += 1
	print(ans)
	




if __name__ == '__main__':
	main()

# Optimized solution
def main():
	import itertools
	n,m = list(map(int,input().split()))
	l = [list(map(int,input().split())) for i in range(m)]
	gaterange = []
	#gate1とgate2の共通部分を出す
	li = []
	ri = []
	for i in range(m):
		li.append(l[i][0])
		ri.append(l[i][1])
	print((len(list(range(max(li)-1,min(ri))))))
	




if __name__ == '__main__':
	main()