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

cnt = 0
for i in range(1,n+1):
    cnt_1 = 0
    for j in range(m):
        if i < x[j][0] or i > x[j][1]:
            cnt_1 = 1
            break

    if cnt_1 == 0:
        cnt += 1

print(cnt)

# Optimized solution
n,m = list(map(int,input().split()))
l = []
r = []
for i in range(m):
    l_x,r_x = list(map(int,input().split()))
    l.append(l_x)
    r.append(r_x)

if min(r[0::])-max(l[0::])+1 >= 1:
    print((min(r[0::])-max(l[0::])+1))
else:
    print((0))
# Original solution
# -*- coding: utf-8 -*-

inputs = input().split(' ')
N = int(inputs[0])
M = int(inputs[1])

inputs = input().split(' ')
L = int(inputs[0])
R = int(inputs[1])
res = {x for x in range(L, R+1)}
for _ in range(M-1):
    inputs = input().split(' ')
    L = int(inputs[0])
    R = int(inputs[1])
    tmp = {x for x in range(L, R+1)}
    res = res & tmp

print((len(res)))

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

inputs = input().split(' ')
N = int(inputs[0])
M = int(inputs[1])

maxL = 1
minR = 100000
for _ in range(M):
    inputs = input().split(' ')
    L = int(inputs[0])
    R = int(inputs[1])
    if L > maxL:
        maxL = L
    if R < minR:
        minR = R

if minR < maxL:
    print((0))
else:
    print((minR - maxL + 1))
# Original solution
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

# n,mが10^5なのでO(n)は間に合わない
ans = 0
for i in range(1,n+1):
    flg = True
    for j in range(m):
        if i < l[j][0] or i > l[j][1]:
            flg = False
    if (flg):
        ans+=1
print(ans)




# Optimized solution
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

# n,mが10^5なのでO(n^2)は間に合わない
"""
ans = 0
for i in range(1,n+1):
    flg = True
    for j in range(m):
        if i < l[j][0] or i > l[j][1]:
            flg = False
    if (flg):
        ans+=1
print(ans)
"""

#lをO(m)で走査して上限、下限をあらかじめ確定しておく
up = 10**5+1
lo = 0

for i in range(m):
    if lo < l[i][0]:
        lo = l[i][0]
    if  up > l[i][1]:
        up = l[i][1]

ans = 0
for i in range(1,n+1):
    if i >= lo and i <= up:
        ans+=1
print(ans)
# Original solution
n, m = list(map(int, input().split()))
gate_cond = [[int(x) for x in input().split()] for _ in range(m)]
each_valid_keys = [[x for x in range(l[0],l[1]+1)] for l in gate_cond]
all_valid_key = each_valid_keys[0]
for keys in each_valid_keys[1:]:
    all_valid_key = list(set(all_valid_key) & set(keys))
print((len(all_valid_key)))

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

gate_cond = [[int(x) for x in input().split()] for _ in range(m)]
max_under_th = max([l[0] for l in gate_cond])
min_upper_th = min([l[1] for l in gate_cond])
if min_upper_th - max_under_th >= 0:
    print((min_upper_th - max_under_th + 1))
else:
    print((0))
# Original solution
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)

l = max(left)
r = min(right)

if l <= r:
    print((r-l+1))
else:
    print((0)) 

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

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

if left <= right:
    print((right-left+1))
else:
    print((0))
# Original solution
# -*- coding utf-8 -*-
N, M = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(M)]

ids = [1] * N
for i in range(M):
    ids[:LR[i][0]-1] = [0] * len(ids[:LR[i][0]-1])
    ids[(LR[i][1]):] = [0] * len(ids[(LR[i][1]):])

print((sum(ids)))


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

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

max_L, min_R = 0, N+1
for i in range(M):
    L, R = list(map(int, input().split()))
    max_L = max(max_L, L)
    min_R = min(min_R, R)

ans = max(min_R - (max_L - 1), 0)

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

for i in range(N+1):
    if max(L)<=i<=min(R):
        cnt+=1
print(cnt)

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

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

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

test = set([j for j in range(l,r+1)])

for i in range(1, m):
    l,r=list(map(int, input().split()))
    test_1 = set([j for j in range(l,r+1)])
    test = test & test_1
print((len(list(test))))

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

list_left = []
list_right = []
for i in range(m):
    l,r=list(map(int, input().split()))
    list_left.append(l)
    list_right.append(r)
test=[i for i in range(max(list_left),min(list_right)+1)]
print((len(test)))
# Original solution
N,M=list(map(int,input().split()))

gate=[i for i in range(N)]
l,r=list(map(int,input().split()))
ans=gate[l-1:r]

for i in range(M-1):
  l,r=list(map(int,input().split()))
  new_ans=gate[l-1:r]
  ans=list(set(ans)&set(new_ans))
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)
L.sort(reverse=True)
R.sort()

if R[0]>=L[0]:
  print((R[0]-L[0]+1))
else:
  print((0))
# Original solution
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import collections
N,M = [int(x) for x in input().split()]
idls =[]
for i in range(N):
    idls.append(i+1)
ans =[]
LR =[]
for i in range (M):
    L,R = [int(x) for x in input().split()]
    LR.append((L,R))

for i in range (M):
    
    iidls = idls[LR[i][0]-1:LR[i][1]]
    ans+=iidls
c= collections.Counter(ans)

a = c.most_common()

count = 0
for i in a:
    if i[1] == M:
        count+= 1
       

print(count)







# Optimized solution
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import collections
N,M = [int(x) for x in input().split()]
ans =[0]*N
for i in range (M):
    L,R = [int(x) for x in input().split()]
    ans[L-1:R] = [x+1 for x in ans[L-1:R]]

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

       

print(count)
# Original solution
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import collections
N,M = [int(x) for x in input().split()]
ans =[0]*N
for i in range (M):
    L,R = [int(x) for x in input().split()]
    ans[L-1:R] = [x+1 for x in ans[L-1:R]]



       

print((ans.count(M)))







# Optimized solution
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N,M = [int(x) for x in input().split()]
Llist =[]
Rlist =[]
for i in range (M):
    L,R = [int(x) for x in input().split()]
    Llist.append(L)
    Rlist.append(R)
 
lmax = max(Llist)
Rmax = min(Rlist)

if lmax > Rmax:
    print((0))
else:
    print((Rmax-lmax+1))
# Original solution
n, m = list(map(int, input().split()))

card = list(range(1, n+1))
delete = set()

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

card = set(card)
print((len(card^delete)))

# 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

print((max(ans, 0)))
# Original solution
n, m = list(map(int, input().split(" ")))
r = [""] * m
for c in range(m):
    r[c] = list(map(int, input().split(" ")))
ac = 0
for c in range(n):
    num = c + 1
    for co in range(m):
        if r[co][0] <= num <= r[co][1]:
            pass
        else:
            break
    else:
        ac += 1
print(ac)

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

L_max = max(L)
R_min = min(R)

ans = R_min - L_max
ans += 1

if ans < 0:
    print((0))
else:
    print(ans)
# Original solution
n, m = list(map(int,input().split()))
 
for i in range(m):
    s,t = list(map(int,input().split()))
    if i == 0:
        ans2 = [s,t+1]
    try:
        ans2 = [j for j in range(max(min(ans2),s),min(max(ans2)+1,t+1)) if s <= j <= t]
    except:
        break

print((len(ans2)))


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

left_max = -1
right_min = n+1

for i in range(m):
    s,t = list(map(int,input().split()))
    left_max = max(left_max,s)
    right_min = min(right_min, t)
    
print((max(0, right_min - left_max +1)))
# Original solution
# -*- coding: utf-8 -*-
n,m = list(map(int, input().split()))
# 文字列の入力
# 出力
gate = [[0,0]]*m
for i in range(m):
    gate[i]=input().split()

cnt = 0
c = 0
for j in range(n):
    j = j+1
    for i in range(m):
        if j >= int(gate[i][0]) and int(gate[i][1]) >= j:
            c += 1
    if c == m:
        cnt = cnt+1
    c = 0

print(cnt)

# Optimized solution
# -*- coding: utf-8 -*-
n,m = list(map(int, input().split()))
# 文字列の入力
# 出力
gate = [[0,0]]*m
for i in range(m):
    gate[i]=input().split()

cnt = 0
min_r = n
max_l = 0
for i in range(m):
    g0 = int(gate[i][0])
    g1 = int(gate[i][1])

    if max_l < g0:
        max_l = g0

    if min_r > g1:
        min_r = g1

for j in range(n):
    j=j+1
    if j >= max_l and min_r >= j:
            cnt += 1

print(cnt)
# Original solution
n,m=list(map(int,input().split()))
ans = set(range(1,n+1))
for i in range(m):
  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()))
ansl = 1
ansr = n
for i in range(m):
  l,r=list(map(int,input().split()))
  ansl = max(ansl,l)
  ansr = min(ansr,r)
print((max(0,ansr-ansl+1)))
# Original solution
n, m = list(map(int, input().split()))
gate = []
for i in range(m):
    l, r = list(map(int, input().split()))
    gate.append({j for j in range(l, r + 1)})
ID = gate[0]
for i in range(1, m):
    ID = ID & gate[i]
print((len(ID)))

# Optimized solution
n, m = list(map(int, input().split()))
gate = []
l, r = list(map(int, input().split()))
ID = {i for i in range(l, r + 1)}
for i in range(1, m):
    l, r = list(map(int, input().split()))
    gate = {j for j in range(l, r + 1)}
    ID = ID & gate
print((len(ID)))
# Original solution
n, m = list(map(int, input().split()))
gate = []
l, r = list(map(int, input().split()))
ID = {i for i in range(l, r + 1)}
for i in range(1, m):
    l, r = list(map(int, input().split()))
    gate = {j for j in range(l, r + 1)}
    ID = ID & gate
print((len(ID)))

# Optimized solution
n, m = list(map(int, input().split()))
gate = []
l, r = list(map(int, input().split()))
for i in range(1, m):
    m, s = list(map(int, input().split()))
    if l < m:
        l = m
    if s < r:
        r = s
print((len([i for i in range(l, r + 1)])))
# Original solution
n, m = list(map(int, input().split()))
gate = []
l, r = list(map(int, input().split()))
for i in range(1, m):
    m, s = list(map(int, input().split()))
    if l < m:
        l = m
    if s < r:
        r = s
print((len([i for i in range(l, r + 1)])))

# Optimized solution
n, m = list(map(int, input().split()))
l, r = [0] * m, [0] * m
for i in range(m):
    l[i], r[i] = list(map(int, input().split()))
print((max(min(r) - max(l) + 1, 0)))
# Original solution
n, m = list(map(int, input().split()))
t = [1] * n
for _ in range(m):
  l, r = list(map(int, input().split()))
  for i in range(0, l - 1):
    t[i] = 0
  for i in range(r, n):
    t[i] = 0
print((sum(t)))


# Optimized solution
n, m = list(map(int, input().split()))
x = 0
y = n - 1
for _ in range(m):
  l, r = list(map(int, input().split()))
  x = max(x, l - 1)
  y = min(y, r - 1)
print((max(y - x + 1, 0)))
# Original solution
N, M = list(map(int, input().split()))

cs = [0] * N
for _ in range(M):
  L, R = list(map(int, input().split()))
  cs[L - 1] += 1
  if R != N:
    cs[R] -= 1

for i in range(1, N):
  cs[i] += cs[i - 1]

print((sum(1 for i in range(N) if cs[i] == M)))

  

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

cs = [0] * N
for _ in range(M):
    L, R = list(map(int, input().split()))
    cs[L - 1] += 1
    if R != N:
        cs[R] -= 1

for i in range(1, N):
    cs[i] += cs[i - 1]

print((cs.count(M)))
# Original solution
N,M=list(map(int,input().split()))
l=[0]*N
for i in range(M):
    L,R=list(map(int,input().split()))
    for j in range(L,R+1):
        l[j-1]+=1

print((l.count(M)))

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

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

for j in range(1, n+1):
    flag = True
    for k in range(m):
        if l[k][0] <= j <= l[k][1]:
            continue
        else:
            flag = False
            break
    if flag:
        ans += 1

print(ans)


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

L = 1
R = 10**5
ans = 1

for i in range(m):
    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
        break

if ans != 0:
    ans = R - L + 1

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

gates = list(range(1, N + 1))
for m in range(M):
  L, R = list(map(int, input().split()))
  gates = [g for g in gates if L <= g and g <= R]
  if len(gates) < 1:
    break

print((len(gates)))


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

gates = [[1, N]]

for m in range(M):
  L, R = list(map(int, input().split()))
  updated = []
  for gate in gates:
    if L <= gate[0]:
      if gate[0] <= R:
        updated.append([gate[0], min(gate[1], R)])
    else:
      if L <= gate[1]:
        updated.append([L, min(gate[1], R)])
  gates = updated


print((sum([g[1] - g[0] + 1 for g in gates])))
# Original solution
import sys

fin = sys.stdin.readline
N, M = [int(elem) for elem in fin().split()]
conditions = [tuple(int(elem) for elem in fin().split()) for _ in range(M)]

current_set = set(range(1, 100001))
for l, r in conditions:
    new_set = set(range(l, r+1))
    current_set = new_set & current_set

print((len(current_set)))


# Optimized solution
import sys

fin = sys.stdin.readline
N, M = [int(elem) for elem in fin().split()]
conditions = [tuple(int(elem) for elem in fin().split()) for _ in range(M)]

l_list = [x[0] for x in conditions]
r_list = [x[1] for x in conditions]

print((max(0, min(r_list) - max(l_list) + 1)))
# Original solution
N , M = list(map(int,input().split()))
L = []
R = []
cnt = 0
flag = True
for i in range(M):
	l,r = list(map(int,input().split()))
	L.append(l)
	R.append(r)
for i in range(N+1):
	flag = True
	for j in range(M):
		if i < L[j] or i > R[j]:
			flag = False
			break
	if flag == True:
		cnt+=1
print(cnt)

# Optimized solution
N , M = list(map(int,input().split()))
L = []
R = []
cnt = 0
flag = True
for i in range(M):
	l,r = list(map(int,input().split()))
	L.append(l)
	R.append(r)
L_max = max(L)
R_min = min(R)
for i in range(N+1):
	flag = True
	if i >= L_max and i <= R_min:
		cnt+=1
print(cnt)
# Original solution
n, m = list(map(int, input().split()))
lr = []
l1 = []
l2 = []

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

for i in range(1, m):
    for j in range(0, lr[i][1] - lr[i][0]+1):
        l2.append(lr[i][0]+j)
    l1 = list(set(l1) & set(l2))
    l2 = []
    
print((len(l1)))


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

ans = 0
for i in card:
    if len(i) == m:
        ans += 1
print(ans)


# Optimized solution
n,m = list(map(int,input().split()))
card = [ 0 for i in range(n+5) ]
for i in range(m):
    l , r = list(map(int,input().split()))
    card[l] += 1
    card[r+1] -= 1

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

x = max(l)
y = min(r)

p = 0
for i in range(1,n+1):
    if x <= i and i <= y:
        p += 1

print(p)

# Optimized solution
n,m = list(map(int,input().split()))
k = [list(map(int,input().split())) for i in range(m)]
l,r = [list(i) for i in zip(*k)]
x = max(l)
y = min(r)
print((max(y-x+1,0)))
# Original solution
n,m = list(map(int,input().split()))
k = [list(map(int,input().split())) for i in range(m)]
l,r = [list(i) for i in zip(*k)]
x = max(l)
y = min(r)
print((max(y-x+1,0)))

# Optimized solution
n,m = list(map(int,input().split()))
a = [list(map(int,input().split())) for i in range(m)]
l,r = [list(i) for i in zip(*a)]
print((max(0,min(r)-max(l)+1)))
# Original solution
def main():
    n, m = list(map(int, input().split()))
    lr_lst = [list(map(int, input().split())) for _ in range(m)]

    ans = 0

    for i in range(1, n + 1):
        is_able = True
        for j in range(m):
            if not lr_lst[j][0] <= i <= lr_lst[j][1]:
                is_able = False
                break
        if is_able:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    n, m = list(map(int, input().split()))
    lr_lst = [list(map(int, input().split())) for _ in range(m)]
    left = 0
    right = n + 1

    for i in range(m):
        if left < lr_lst[i][0]:
            left = lr_lst[i][0]
        if lr_lst[i][1] < right:
            right = lr_lst[i][1]

    if right - left < 0:
        print((0))
    else:
        print((right - left + 1))


if __name__ == "__main__":
    main()
# Original solution
def main():
    N, M = list(map(int, input().split()))
    A = [0] * (N + 1)
    for _ in range(M):
        L, R = list(map(int, input().split()))
        A[L - 1] += 1
        A[R] -= 1
    c = 0
    ans = 0
    for a in A:
        c += a
        if c == M:
            ans += 1
    print(ans)


main()


# Optimized solution
def main():
    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)
    if min(R) - max(L) >= 0:
        print((min(R) - max(L) + 1))
    else:
        print((0))


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

A = set(x for x in range(1,N+1))
for i in range(M):
  L, R = list(map(int, input().split()))
  A &= {x for x in range(L,R+1)}

print((len(A)))


# 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)
print((max(0, min(R)-max(L)+1)))
# Original solution
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = list(map(int, readline().split()))
LR = [[int(x) for x in readline().split()] for _ in range(M)]
se = set(range(LR[0][0], LR[0][1]+1))
for i in LR:
    sa = set(range(i[0], i[1]+1))
    se = se & sa

print((len(se)))



# Optimized solution
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = list(map(int, readline().split()))
A = [None]*M
B = [None]*M
for i in range(M):
    A[i], B[i] = list(map(int, readline().split()))

ans = min(B) - max(A)

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

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

# Optimized solution
n, m = list(map(int, input().split()))
x, y = 1, n
for _ in range(m):
  a, b = list(map(int, input().split()))
  x = max(a, x)
  y = min(b, y)
print((max(0, y-x+1)))
# Original solution
def resolve():
    N, M = list(map(int, input().split()))
    interval = []
    for i in range(M):
        L, R = list(map(int, input().split()))
        interval.append(list(range(L, R + 1)))
    # print(interval)
    interval = [set(l) for l in interval]
    interval = set.intersection(*interval)

    print((len(set(range(1, N + 1)) & interval)))

    return


resolve()

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

for _ in range(m):
  l,r = list(map(int,input().split()))
  S = set(range(l,r+1))
  A = A & S
print((len(A)))

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

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

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

sum = 0
for id in range(1, N+1):
    for li, ri in zip(L, R):
        if id < li or ri < id:
            break
    else:
        sum += 1

print(sum)


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

sum = 0
maxl = max(L)
minr = min(R)
for id in range(N):
    if maxl <= id and id <= minr:
        sum += 1

print(sum)
# Original solution
n, m = [int(i) for i in input().split()]
l = []
r = []
for _ in range(m):
    i, k = [int(i) for i in input().split()]
    l.append(i)
    r.append(k)

card = [0] * (n+1)
for i, j in zip(l, r):
    for k in range(i, j+1):
        card[k] += 1

print((card.count(m)))


# Optimized solution
n, m = [int(i) for i in input().split()]
l = []
r = []
for _ in range(m):
    i, k = [int(i) for i in input().split()]
    l.append(i)
    r.append(k)

res = min(r) - max(l) + 1 if min(r) >= max(l) else 0
print(res)
# Original solution
n,m=list(map(int,input().split()))
lr=[]
result=0
gates=0
for i in range(m):
  lr.append(input().split())
#print(lr[0])
for k in range(1,n+1):
  gates=0
  for h in range(m):
    if int(k)>=int(lr[h][0]) and int(k)<=int(lr[h][1]):
      gates+=1
  #print(gates,k)
  if gates==m:
    result+=1
    gates=0
    #print('ID')
print(result)

# Optimized solution
n,m=list(map(int,input().split()))
l=1
r=n
lr=[]
for i in range(m):
  lr.append(input().split())
for j in lr:
  if l<int(j[0]):
    l=int(j[0])
  if r>int(j[1]):
    r=int(j[1])
if r>=l:
  print((int(r-l+1)))
else:
  print((0))