CodeEff/ECCO huggingface dataset:
# Original solution
N, M = list(map(int, input().split()))

LR = []
for _ in range(M):
    l, r = list(map(int, input().split()))
    LR.append((l, r))
    
cards = LR[:]
master_ids = set(range(cards[0][0], cards[0][1]+1))
for l, r in cards:
    ids = set(range(l, r+1))
    master_ids = master_ids & ids
    if not master_ids:
        break
    
ans = len(master_ids)
print(ans)


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

LR = []
for _ in range(M):
    l, r = list(map(int, input().split()))
    LR.append((l, r))
    
cards = LR[:]

import sys
INT_MAX = sys.maxsize

left_id = 0
right_id = INT_MAX
for l, r in cards:
    left_id = max(left_id, l)
    right_id = min(right_id, r)
    
if left_id <= right_id:
    ans = right_id + 1 - left_id
else:
    ans = 0
print(ans)
# Original solution
n, m = list(map(int, input().split()))
Authority = [list(map(int, input().split())) for _ in range(m)]
cards = set(range(1, n + 1))

for i in range(m):
    gate = set(range(Authority[i][0], Authority[i][1] + 1))
    cards &= gate
print((len(cards)))


# Optimized solution
n, m = list(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])))
# Original solution
N,M=(int(i) for i in input().split())
gate=[]
for i in range(M):
    gate.append([int(i) for i in input().split()])
    gate[i].append(gate[i][1]-gate[i][0])
gate=sorted(gate,key=lambda x:x[2])

answer=0
for i in range(N+1):
    ok=1
    for l,r,p in gate:
        if l<=i and i<=r:
            pass
        else:
            ok=0
            break
    if ok==1:
        answer+=1
print(answer)

# Optimized solution
N,M=(int(i) for i in input().split())
gate=[]
for i in range(M):
    gate.append([int(i) for i in input().split()])
    
maxl=max(gate,key=lambda x:x[0])[0]
minr=min(gate,key=lambda x:x[1])[1]
if maxl<=minr:
    print((minr-maxl+1))
else:
    print((0))
# Original solution
n, m = [int(i) for i in input().split()]

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

counter = 0
for i in range(n):
    if sum(x[i]) == m:
        counter += 1

print(counter)

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

ll = []
rr = []
for i in range(m):
    l, r = [int(i) for i in input().split()]
    ll.append(l)
    rr.append(r)

ans = min(rr) - max(ll) + 1
if ans < 0:
    ans = 0
print(ans)
# Original solution
n,m = list(map(int,input().split()))

a = {}
for i in range(m):
    l,r = list(map(int,input().split()))

    for j in range(l,r+1):
        if not j in a:
            a[j] = 1
        else:
            a[j] += 1

count = 0
for key in list(a.keys()):
    if a[key] == m:  
        count +=1                

print(count)

# Optimized solution
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 l > max:
        print((0))
        exit(0)

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

print((len(ans)))


# Optimized solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))
ans = -1
if m > 1:
    for i in range(m-1):
        _l, _r = list(map(int, input().split()))
        if (l < _l <= r):
            l = _l
        if (l <= _r < r):
            r = _r
        if _r < l or r < _l:
            ans = 0
            print(ans)
            break

if ans == -1:
    print((r-l+1))
# Original solution
card_num, gate_num = list(map(int, input().split()))
table = [0] * card_num
count = 0


for i in range(gate_num):
    start_num, end_num = list(map(int, input().split()))
    for j in range(start_num-1, end_num):
        table[j] += 1

for l in range(card_num):
    if table[l] == gate_num:
        count += 1

print(count)

# Optimized solution
# 先頭の最大と最後尾の最小を求める
card_num, gate_num = list(map(int, input().split()))
lmax, rmin = list(map(int, input().split())) # 最初の入力がlの最大とrの最小
for i in range(1, gate_num):
    l, r = list(map(int, input().split()))
    lmax = max(lmax, l) # lmaxにlmaxとlの大きい方
    rmin = min(rmin, r) # rminにrminとrの小さい方
print((max(0, rmin-lmax+1))) # 0かrmin-lmax+1の大きい方を出力
# Original solution
n,m=list(map(int,input().split()))
D=[list(map(int,input().split())) for i in range(m)]

I=set(i for i in range(1,n+1))

for i in range(m):
  I &= set(i for i in range(D[i][0],D[i][1]+1))
print((len(I)))  

# Optimized solution
n,m=list(map(int,input().split()))
D=[list(map(int,input().split())) for i in range(m)]
maxI=n
minI=1
for i in range(m):
  maxI=min(maxI,D[i][1])
  minI=max(minI,D[i][0])
print((max(maxI-minI+1,0)))
# Original solution
def set_range(x, y):
    return {t+x for t in range(y-x)}

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

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

S = set_range(1, N+1)
for m in range(M):
    S = S.intersection(set_range(L[m], R[m]+1))
    
print((len(S)))

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

for m in range(M):
    L[m], R[m] = list(map(int, input().split()))
    
print((max(0,min(R)-max(L)+1)))
# Original solution
import sys
input=sys.stdin.readline

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

g = set(i for i in range(1,N+1))
for i in range(M):
    l,r = list(map(int, input().split()))
    t = set(i for i in range(l,r+1))
    g = g & t

print((len(g)))


# Optimized solution
import sys
input=sys.stdin.readline

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

l,r = 1,N
for i in range(M):
    l1,r1 = list(map(int, input().split()))
    if l1>l:
        l = l1
    if r1<r:
        r = r1
if r-l<0:
    print((0))
else:
    print((r-l+1))
# Original solution
# ABC127C - Prison
n, m = list(map(int, input().rstrip().split()))
lst = [list(map(int, input().rstrip().split())) for _ in range(m)]
gates = [{j for j in range(i[0], i[1] + 1)} for i in lst]
ans = gates[0]
for i in gates[1:]:
    ans = ans & i
print((len(ans)))

# Optimized solution
# ABC127C - Prison
n, m = list(map(int, input().rstrip().split()))
a, b = list(map(int, input().rstrip().split()))
ans = {i for i in range(a, b + 1)}
for _ in range(m - 1):
    a, b = list(map(int, input().rstrip().split()))
    ans = ans & {i for i in range(a, b + 1)}
print((len(ans)))
# Original solution
# ABC127C - Prison
n, m = list(map(int, input().rstrip().split()))
lst = [list(map(int, input().rstrip().split())) for _ in range(m)]
ans = 0
l, r = lst[0][0], lst[0][1]
for i in lst[1:]:
    l = max(l, i[0])
    r = min(r, i[1])
for i in range(1, n + 1):
    if l <= i <= r:
        ans += 1
print(ans)

# Optimized solution
# ABC127C - Prison
# l-max < i < r-min -> count i
n, m = list(map(int, input().rstrip().split()))
lst = [list(map(int, input().rstrip().split())) for _ in range(m)]
l, r = lst[0][0], lst[0][1]
for i in lst[1:]:
    l = max(l, i[0])
    r = min(r, i[1])
print((max(0, r - l + 1)))
# Original solution
tmp = input().split()
N = int(tmp[0])
M = int(tmp[1])

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

count = 0
for i in range(1, N+1):
  flag = True
  for j in range(M):
    if (cardlist[j][0] > i or cardlist[j][1] < i):
      flag = False
      break
  
  if (flag):
    count += 1

print(count)

# Optimized solution
tmp = input().split()
N = int(tmp[0])
M = int(tmp[1])

max = N
min = 0
for i in range(M):
  inputcard = list(map(int, input().split()))
  if min < inputcard[0]:
    min = inputcard[0]
  
  if max > inputcard[1]:
    max = inputcard[1]
  
if (max - min + 1 < 0):
  print((0))
else:
  print((max - min + 1))
# Original solution
N, M = list(map(int, input().split()))
master = set(range(1, N+1))

for _ in range(M):
    L, R = list(map(int, input().split()))
    master = master & set(range(L, R+1))

print((len(master)))

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

for _ in range(M):
    L, R = list(map(int, input().split()))
    lb = max(lb, L)
    rb = min(rb, R)

print((rb-lb+1 if lb <= rb else 0))
# Original solution
n,m = list(map(int,input().split()))
card = [0]*n
for i in range(m):
    l,r = list(map(int,input().split()))
    l -= 1
    card[l:r] = list([x+1 for x in card[l:r]])
print((card.count(m)))

# Optimized solution
n,m = list(map(int,input().split()))
card = [0]*n
l,r = 1,n
for i in range(m):
    tmp_l,tmp_r = list(map(int,input().split()))
    if l < tmp_l:
        l=tmp_l
    if r > tmp_r:
        r = tmp_r
ans = 0
if r-l>=0:
    ans = r-l+1
print(ans)
# Original solution
n,m = list(map(int, input().split()))

for m_i in range(m):
  
  l,r = list(map(int, input().split()))
  if m_i==0:
    ans = set(range(l, r+1))
  else:
    ans &= set(range(l, r+1))
    
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)
    
print((min(R)-max(L)+1 if min(R)-max(L)>=0 else 0))
# Original solution
#n: id card
#m: gate
n, m = list(map(int, input().split()))
data = [0 for i in range(n+1)]
ans = 0

for i in range(m):
    l, r = list(map(int, input().split()))
    for j in range(l, r+1):
        data[j] += 1
        if data[j] == m:
            ans += 1

print(ans)

# Optimized solution
#n: id card
#m: gate
n, m = list(map(int, input().split()))
data = [0 for i in range(n+1)]

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

for i in range(n):
    data[i+1] += data[i]

print((data.count(m)))
# Original solution
N, M = [int(x) for x in input().split()]
LR = [[int(x) for x in input().split()] for i in range(M)]
LR = [list(range(x,y+1)) for (x,y) in LR]
res = set(LR[0])
for x in LR[1:]:
    # print(res)
    # print(x)
    res = res.intersection(set(x))
    # print(res)
print((len(res)))

# Optimized solution
N, M = [int(x) for x in input().split()]
L = 1
R = N
for i in range(M):
    L_,R_ = [int(x) for x in input().split()]
    L = max(L, L_)
    R = min(R, R_)

print((max((R-L + 1), 0)))
# Original solution
n, m = input().split()
N = int(n)
M = int(m)


s = set([])
card = N
for i in range(M):
    l, r = input().split()
    L = int(l)
    R = int(r)

    for j in range(N):
        if L <= j+1 and j+1 <= R:
            pass
        else:
            s.add(j+1)

print((N - len(list(s))))


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

listL = []
listR = []
card = N
for i in range(M):
    l, r = input().split()
    L = int(l)
    R = int(r)

    listL.append(L)
    listR.append(R)

maxL = max(listL)
minR = min(listR)

if minR < maxL:
    result = 0
else:
    result = minR - maxL + 1
print(result)
# Original solution
N, M  = list(map(int, input().split()))
L = []
R = []

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

cnt = 0
for n in range(N):
    counter = 0
    for m in range(M):
        if (n + 1) == R[m] or (L[m] <= (n + 1) and (n + 1) <= R[m]):
            counter += 1
            if counter == M:
                cnt += 1
        else:
            break
print(cnt)


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

for m in range(M):
    L_in, R_in = list(map(int, input().split()))
    L.append(L_in)
    R.append(R_in)

if min(R) >= max(L):
    print((min(R) - max(L) + 1))
else:
    print((0))
# Original solution
N,M=list(map(int,input().split()))
all=[]
for i in range(M):
    L,R=list(map(int,input().split()))
    ids=[b for b in range(L,R+1)]
    all.append(ids)
for a in range(M):
    ans=set(all[a])&set(all[0])
print((len(ans)))

# Optimized solution
N,M=list(map(int,input().split()))
all=[]
for i in range(M):
    L,R=list(map(int,input().split()))
    if i==0:
        ans=[b for b in range(L,R+1)]
    else:
        ids=[b for b in range(L,R+1)]
        ans=set(ans)&set(ids)
print((len(ans)))
# Original solution
N,M=list(map(int,input().split()))
all=[]
for i in range(M):
    L,R=list(map(int,input().split()))
    if i==0:
        ans=[b for b in range(L,R+1)]
    else:
        ids=[b for b in range(L,R+1)]
        ans=set(ans)&set(ids)
print((len(ans)))

# 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)
ans=min(R)-max(L)+1
if ans<=0:
  	ans=0
print(ans)
# Original solution
import sys
input = sys.stdin.readline

def main():
    N, M = [int(x) for x in input().split()]

    answer = set()
    for i in range(M):
        L, R = [int(x) for x in input().split()]
        if i == 0:
            answer = {x for x in range(L, R + 1)}
        else:
            s = {x for x in range(L, R + 1)}
            answer &= s

    print((len(answer)))




if __name__ == '__main__':
    main()



# Optimized solution
import sys
input = sys.stdin.readline

def main():
    N, M = [int(x) for x in input().split()]

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

    answer = A[0]
    for i in range(1, M):
        if max(answer) < min(A[i]) or max(A[i]) < min(answer):
            print((0))
            exit()

        total = []
        total.extend(answer)
        total.extend(A[i])
        s_total = sorted(total)
        answer = s_total[1:3]

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




if __name__ == '__main__':
    main()
# Original solution
n, m = list(map(int, input().split()))

buf = set(range(1, n+1))
for i in range(m):
    l, r = list(map(int, input().split()))
    buf = buf & set(range(l, r+1))
print((len(buf)))


# Optimized solution
a = 1

n, m = list(map(int, input().split()))
ans_r = 10 ** 5

l_list = []
r_list = []
for i in range(m):
    l, r = list(map(int, input().split()))
    l_list.append(int(l))
    r_list.append(int(r))
for r in r_list:
    if ans_r > r:
        ans_r = r
for l in l_list:
    if a < l:
        a = l

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


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

left, right = list(map(int, input().split()))
ans = set(range(left, right+1))
               
for i in range(m-1):
  left, right = list(map(int, input().split()))
  set_temp = set(range(left, right+1))
  ans = ans & set_temp
print((len(ans)))

# Optimized solution
n, m = list(map(int, input().split()))
min_num = 0
max_num = 10**5
           
for i in range(m):
  min_temp, max_temp = list(map(int, input().split()))
  if min_temp > min_num:
    min_num = min_temp
  if max_temp < max_num:
    max_num = max_temp
print((max(max_num - min_num + 1, 0)))
# Original solution
def resolve():
    N,M = list(map(int,input().split()))
#    LL,RR = [],[]
#    for i in range(M):
#        L,R = map(int,input().split())
#        LL.append(L)
#        RR.append(R)

    ans = 0
#    print()
#    print(N,M)
#    print(LL,RR)
    bool_id = [1]*N
#    print(bool_id)
    for i in range(M): #Gate num
#        print(LL[i],RR[i])
#        l,r = LL[i]-1,RR[i]
        l,r = list(map(int,input().split()))
        #bool_id[l:r] = [True]*(r-l)
        bool_id[:l-1] = [0]*(l-1)
        bool_id[r:] = [0]*(N-r)
#        print(bool_id)
    print((sum(bool_id)))

resolve()

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

    bool_id = [1]*N
    lmin = -1
    rmax = 10**10
    for i in range(M):
        lmin = max(lmin,LL[i])
        rmax = min(rmax,RR[i])
    print((max(rmax-lmin+1,0)))

resolve()
# Original solution
N, M = list(map(int,input().split()))
ID = list(i+1 for i in range(N))

for i in range(M):
  L,R = list(map(int,input().split()))
  ID_new = [j for j in range(L,R+1)]
  ID = set(ID) & set(ID_new)

print((len(ID)))

# Optimized solution
N, M = list(map(int,input().split()))
l=1
r=N
for i 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:
  print(ans)
else:
  print((0))
# Original solution
# -*- coding: utf-8 -*-
# abc127/abc127_c
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, M = i2nn()
    R, L = i2nn()
    ss = set(range(R, L + 1))
    for i in range(1, M):
        R, L = i2nn()
        ss = ss & set(range(R, L + 1))
    print((len(ss)))
    return

main()


# Optimized solution
# -*- coding: utf-8 -*-
# abc127/abc127_c
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))


def main():
    N, M = i2nn()
    L, R = (0, N)
    for i in range(M):
        Li, Ri = i2nn()
        if L < Li:
            L = Li
        if Ri < R:
            R = Ri
    if L > R:
        print((0))
    else:
        print((R - L + 1))
    return

main()
# Original solution
def resolve():
    (n, m) = list(map(int, input().split(" ")))
    results = [0] * n
    for i in range(m):
        o = list(map(int, input().split(" ")))
        if i == 0:
            results[o[0]:o[1]+1] = [1] * (o[1]+1-o[0])
            continue
        for j in range(n):
            if not results[j]:
                continue
            if not (o[0] <= j and j <= o[1]):
                results[j] = 0
                if o[1] < j:
                    break
            if results.count(1) == 0:
                print((0))
                return

    print((results.count(1)))


if '__main__' == __name__:
    resolve()

# Optimized solution
def resolve():
    (n, m) = list(map(int, input().split(" ")))
    
    for i in range(m):
        o = list(map(int, input().split(" ")))
        if i == 0:
            lmax = o[0]
            rmin = o[1]
        else:
            lmax = max(lmax, o[0])
            rmin = min(rmin, o[1])

    print((rmin-lmax+1 if lmax <= rmin else 0))


if '__main__' == __name__:
    resolve()
# Original solution
n, m = [int(i) for i in input().split()]
table = [[int(i) for i in input().split()] for m in range(m)]

a = list(range(table[0][0],table[0][1]+1))
ans = a
for i,t in enumerate(table[1:]):
    if i == 0:
    	b = list(range(t[0], t[1]+1))
    	ans = set(a) & set(b)
    else:
        b = list(range(t[0], t[1]+1))
        ans = ans & set(b)
print((len(ans)))

# Optimized solution
n,m=list(map(int,input().split()))
low=1
high=n
for i in range(m):
	l,r=list(map(int,input().split()))
	low = max(low,l)
	high=min(high,r)
ans=high-low+1
print((ans if ans>=0 else 0))
# Original solution
(N, M) = (int(i) for i in input().split(' '))
cards = set(range(1,N+1))

for i in range(0,M):
    # gate i can be opened by following card set
    (l, r) = (int(j) for j in input().split(' '))
    #print(cards, set(range(l,r+1)))
    cards = cards.intersection(set(range(l,r+1)))

print((len(cards)))
    

# Optimized solution
(N, M) = (int(i) for i in input().split(' '))
(l0,r0) = (1, N)

n = -1
for i in range(0,M):
    # gate i can be opened by following card set
    (l, r) = (int(j) for j in input().split(' '))
    #print(cards, set(range(l,r+1)))
    if l > r0 or r < l0:
        n = 0
        break
    (l0,r0) = (max(l,l0), min(r,r0))

if n == -1:
    n = r0-l0 +1
    
print(n)
# Original solution
N, M= list(map(int, input().split()))

L,R = [],[]
ans = [0]*(N+1)
for i in range(M):
    a,b = list(map(int,input().split()))
    
    for j in range(a,b+1):
        ans[j] += 1

count = 0
for i in ans:
    if i == M:
        count += 1

print(count)





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

L,R = 0,100100
ans = [0]*(N+1)
for i in range(M):
    a,b = list(map(int,input().split()))
    L = max(L,a)
    R = min(R,b)
if R - L >= 0:
    print((R-L+1))
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))

C = [1] * N

for i in range(M):
    a, b = list(map(int, input().split()))
    a = a - 1
    if a != 0:
        C[:a] = [0] * a
    if b-1 != len(C):
        C[b:] = [0] * (len(C) - b)

print((sum(C)))


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

L = []
R = []
l = 0
r = 10 ** 6
for i in range(M):
    a,b = list(map(int, input().split()))
    L.append(a)
    R.append(b)

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

ans = r-l+1
if ans > 0:
    print(ans)
else:
    print("0")
# Original solution
n,m= list(map(int,input().split()))
L = []
R = []
for _ in range(m):
  l,r = list(map(int,input().split()))
  L += [l]
  R += [r]
m = min(R)-max(L)+1
if m<0:
  print((0))
else:
  print(m)

# Optimized solution
n,m = list(map(int,input().split()))
ll = []
rr = []
for i in range(m):
  l,r = list(map(int,input().split()))
  ll.append(l)
  rr.append(r)
p = min(rr) - max(ll) + 1
if p < 0:
  print((0))
else:
  print(p)
# Original solution
from collections import Counter
n,m=list(map(int,input().split()))
c=Counter()
for i in range(m):
    l,r=list(map(int,input().split()))
    idcard=list(range(l,r+1))
    c+=Counter(idcard)
cnt=0
for i in range(1,n+1):
    if c[i]==m:
        cnt+=1
print(cnt)

# Optimized solution
n,m=list(map(int,input().split()))
l_max=0
r_min=10**5+1
for i in range(m):
    l,r=list(map(int,input().split()))
    l_max=max(l_max,l)
    r_min=min(r_min,r)
print((max(0,r_min-l_max+1)))
# Original solution
n, m = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    x, y=[int(i) for i in input().split()]
    l.append(x)
    r.append(y)
ans = 0
'''
for i in range(n):
    cnt = 0
    for j in range(m):

        for k in range(l[j], r[j]+1):
            if i + 1 == k:
                cnt += 1
        if cnt == m:
            ans += 1
print(ans)
'''
for i in range(n):
    cnt = 0
    #print('cnt rrr')
    #print('i=' + str(i))
    for j in range(m):
        #print(l[j], end=' ')
        #print(r[j])
        if i + 1 >= l[j] and r[j] >= i + 1:
            cnt += 1
            #print('cnt='+ str(cnt))
        else:
            #print('break')
            break
    if cnt == m:
        ans += 1
print(ans)


# Optimized solution
n, m = list(map(int, input().split()))
maxl = 0
minr = 1000000
for i in range(m):
    l, r = list(map(int, input().split()))
    maxl = max(maxl, l)
    minr = min(minr, r)
if minr < maxl:
    print((0))
else:
    print((minr - maxl + 1))
# Original solution
nm = [int(i) for i in input().split()]
lr = [[int(i) for i in input().split()] for i in range(nm[1])]


a = [{j for j in range(lr[i][0], lr[0][1]+1)} for i in range(nm[1])]
b = set()
for i in range(len(a)):
    if b == set():
        b = a[i]
    b = b & a[i]
print((len(b)))

# Optimized solution
nm = [int(i) for i in input().split()]
lr = [[int(i) for i in input().split()] for i in range(nm[1])]

l = [i[0] for i in lr]
r = [i[1] for i in lr]
print((len(list(range(max(l), min(r)+1)))))