CodeEff/ECCO huggingface dataset:
# 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()))
ans = set(range(1, N+1))
l_max = 1
r_min = N

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 = [int(i) for i in input().split()]
lr = [[int(i) for i in input().split()] for j in range(m)]
l = max(x[0] for x in lr)
r = min(x[1] for x in lr)
print((max(0, r - l + 1)))


# 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(l, lmax)
    rmin = min(r, rmin)
print((max(rmin - lmax + 1, 0)))
# Original solution
N, M = list(map(int, input().split()))

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


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

if m == 1:
  l, r = list(map(int, input().split()))
  print((r-l+1))
else:
  l, r = list(map(int, input().split()))
  ans = list(range(l, r+1))
  for _ in range(m-1):
    l, r = list(map(int, input().split()))
    lr_list = list(range(l, r+1))
    ans = [a for a in ans if lr_list.count(a) == 1]
    if len(ans) == 0:
      break
  print((len(ans)))

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

if m == 1:
  l, r = list(map(int, input().split()))
  print((r-l+1))
else:
  ls, rs = [], []
  for _ in range(m):
    l, r = list(map(int, input().split()))
    ls.append(l)
    rs.append(r)
  ans = min(rs) - max(ls) + 1
  if ans > 0:
    print(ans)
  else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(M)]

ans = set(list(range(1, N + 1)))
for lr in LR:
    l, r = lr
    ans = ans & set(list(range(l, r + 1)))
print((len(list(ans))))

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

A, B = 1, N
for lr in LR:
    L, R = lr
    if R < A or B < L:
        print((0))
        exit()

    if A <= L <= R <= B:
        A, B = L, R
    elif L <= A <= R <= B:
        B = R
    elif A <= L <= B <= R:
        A = L
print((B - A + 1))
# Original solution
N, M = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(M)]

ans = set(list(range(1, N + 1)))
for lr in LR:
    l, r = lr
    ans = ans & set(list(range(l, r + 1)))
print((len(list(ans))))


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

A, B = 1, N
for lr in LR:
    L, R = lr
    if R < A or B < L:
        print((0))
        exit()

    if A <= L <= R <= B:
        A, B = L, R
    elif L <= A <= R <= B:
        B = R
    elif A <= L <= B <= R:
        A = L
print((B - A + 1))
# Original solution
import collections
N,M=list(map(int,input().split()))
L=[]
R=[]
for i in range(M):
  l,r=[int(i) for i in input().split()]
  L.append(l)
  R.append(r)
LR=[]
for i in range(M):
  for j in range(L[i],R[i]+1):
    LR.append(j)
LR=collections.Counter(LR)
LR=LR.most_common()
LR=dict(LR)
cnt=0
for i in list(LR.values()):
  if i==M:
    cnt+=1
print(cnt)

# Optimized solution
N,M=list(map(int,input().split()))
L=[]
R=[]
for i in range(M):
  l,r=[int(i) for i in input().split()]
  L.append(l)
  R.append(r)
Lmax=max(L)
Rmin=min(R)
if Rmin-Lmax>=0:
  print((Rmin-Lmax+1))
else:
  print((0))
# Original solution
n, m = list(map(int, input().split()))
li = [list(map(int, input().split())) for _ in range(m)] #n行に１つずつ入力される整数
num_li = [idx for idx in range(10**5+1)]
for l, r in li:
    temp_li = [i for i in range(l, r+1)]
    num_li = set(temp_li) & set(num_li)
print((len(num_li)))

# Optimized solution
n, m = list(map(int, input().split()))
li = [list(map(int, input().split())) for _ in range(m)] #n行に１つずつ入力される整数
l_li = []
r_li = []
for l, r in li:
    l_li.append(l)
    r_li.append(r)
print((max(min(r_li) - max(l_li) +1, 0)))
# Original solution
def i_input(): return int(eval(input()))
def s_input(): return list(map(int,input().split()))
def l_input(): return list(map(int, input().split()))

N, M = s_input()
L, R = [], []

cards = []
for i in range(M):
    Li, Ri = s_input()
    L.extend([Li])
    R.extend([Ri])
    cards.extend(list(range(Li, Ri+1)))

cards = set(cards)

ng_cards = set()
for card in cards:
    for i in range(M):
        if card < L[i] or card > R[i]:
            ng_cards.add(card)
            break

print((len(cards - ng_cards)))




# Optimized solution
def i_input(): return int(eval(input()))
def s_input(): return list(map(int,input().split()))
def l_input(): return list(map(int, input().split()))

N, M = s_input()
L, R = [], []

for i in range(M):
    Li, Ri = s_input()
    L.extend([Li])
    R.extend([Ri])

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

num = R_min - L_max

if num >= 0:
    print((num + 1))
else:
    print((0))
# Original solution
n, m = list(map(int, input().split()))
a = [list(map(int, input().split())) for i in range(m)]

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


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

left = []
right = []
for i in range(m):
    a, b = list(map(int, input().split()))
    left.append(a)
    right.append(b)

if min(right) >= max(left):
    print((min(right) - max(left) + 1))
else:
    print((0))
# Original solution
N,M=list(map(int,input().split()))
A=[]
for I in range(M):
	x,y=list(map(int,input().split()))
	A.append(list(range(x,y+1)))
F=set(A[0]).intersection(*A[1:])
print((len(F)))

# Optimized solution
N,M=list(map(int,input().split()))
L=[]
R=[]
for I in range(M):
	x,y=list(map(int,input().split()))
	L.append(x)
	R.append(y+1)
F=list(range(max(L),min(R)))
print((len(F)))
# Original solution
N, M = list(map(int, input().split()))
L_R = [list(map(int, input().split())) for i in range(M)]

L = [L_R[i][0] for i in range(M)]
R = [L_R[i][1] for i in range(M)]

ans = [j+1 for j in range(N)]
for i in range(M):
    ans = [ans[j] for j in range(len(ans)) if L[i] <= ans[j] and ans[j] <= R[i]]
print((len(ans)))

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

L = [L_R[i][0] for i in range(M)]
R = [L_R[i][1] for i in range(M)]

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

if L_max <= R_min:
    ans = R_min - L_max + 1
else:
    ans = 0

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

# 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)
big=min(r)
small=max(l)
print((big-small+1 if big-small>=0 else 0))
# Original solution
N,M=list(map(int,input().split()))
cards=[1]*N

for i in range(M):
    l,r=list(map(int,input().split()))
    if l!=1:
        cards[:l-1]=[0]*(l-1)
    if r!=N:
        cards[r:]=[0]*(N-r)
    
print((sum(cards)))

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


for i in range(M):
    l,r=list(map(int,input().split()))
    cards_l=max(cards_l,l)
    cards_r=min(cards_r,r)
    #print(r,cards_r)
print((max(0,cards_r-cards_l+1)))
# Original solution
N, M = list(map(int, input().split()))

haba = list()
max_L = 0
min_R = 10 ** 5 + 1
for i in range(M):
    L, R = list(map(int, input().split()))
    haba.append([L, R])
    max_L = max(max_L, L)
    min_R = min(min_R, R)

min_haba = set(range(max_L, min_R + 1))
ids = set()
for L, M in haba:
    h = set(range(L, R + 1))
    ids = ids.union(h.intersection(min_haba))

print((len(ids)))

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

haba = list()
max_L = 0
min_R = 10 ** 5 + 1
for i in range(M):
    L, R = list(map(int, input().split()))
    haba.append([L, R])
    max_L = max(max_L, L)
    min_R = min(min_R, R)

print((max(min_R + 1 - max_L, 0)))
# Original solution
N,M=[int(x) for x in input().rstrip().split()]
l=[]
for i in range(M):
    b=input().rstrip().split()
    l.append(b)
count=0
ans=0
for i in range(1,N+1):
    for x,y in l:
        if int(x)<=i and i<=int(y):
            count+=1
        else:
            break
        if count==M:
            ans+=1

    count=0
print(ans)







# Optimized solution
N,M=[int(x) for x in input().rstrip().split()]
l=[]
r=[]
for i in range(M):
    b=input().rstrip().split()
    l.append(int(b[0]))
    r.append(int(b[1]))

L=max(l)
R=min(r)

if R<L:
    print((0))
else:

    print((R-L+1))
# Original solution
n,m=list(map(int,input().split()))
lr=[list(map(int,input().split())) for _ in range(m)]
card=[0]*n

for i in range(m):
    l,r=lr[i]
    for j in range(l-1,r):
        card[j]+=1
print((card.count(m)))

# Optimized solution
n,m=list(map(int,input().split()))
l=1
r=n
for i in range(m):
    ll,rr = list(map(int,input().split()))
    l=max(l,ll)
    r=min(r,rr)
if l<=r:
    print((r-l+1))
else:
    print((0))
# Original solution
n,m = list(map(int, input().split()))
s = {_ for _ in range(1,n+1)}
for i in range(m):
  l,r = list(map(int, input().split()))
  s &= {_ for _ in range(l,r+1)}
print((len(s)))

# Optimized solution
n,m = list(map(int, input().split()))
left = set()
right = set()
for i in range(m):
  l,r = list(map(int, input().split()))
  left.add(l)
  right.add(r)
mini = max(left)
maxi = min(right)
print((len(list(range(mini,maxi+1)))))
# Original solution
n,m = (int(x) for x in input().split())
l = [0]*m
r = [0]*m
hoge = [m]*n
for i in range(m):
    l[i],r[i] = (int(x) for x in input().split())
    
for i in range(m):
    for f in range(l[i]-1,r[i]):
        hoge[f] -= 1
count = 0
for j in hoge:
    if j == 0:
        count+= 1

print(count)


# Optimized solution
n, m = (int(x) for x in input().split())
l = [0]*m
r = [0]*m
for i in range(m):
    l[i], r[i] = [int(x) for x in input().split()]
lmax = max(l)
rmin = min(r)
if rmin >= lmax:
    print((rmin-lmax+1))
else:
    print((0))
# Original solution
n,m=list(map(int,input().split()))
a=[0]*n
for i in range(m):
  l,r=list(map(int,input().split()))
  for k in range(l-1,r):
    a[k]+=1
ans=0
for i in range(n):
  if a[i]==m:
    ans+=1
print(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()))
  if l>low:
    low=l
  if r<high:
    high=r
if high>=low:
  print((high-low+1))
else:
  print((0))
# Original solution
from sys import stdin
n, m = list(map(int, stdin.readline().split()))
ans = set(range(1, n+1))
for i in range(m):
    l, r = list(map(int, stdin.readline().split()))
    ans = list([x for x in ans if l<=x<=r])
print((len(ans)))

# Optimized solution
from sys import stdin
n, m = list(map(int, stdin.readline().split()))
mn = 1
mx = n

for i in range(m):
    l, r = list(map(int, stdin.readline().split()))
    mn = max(mn, l)
    mx = min(mx, r)
print((max(0, mx-mn+1)))
# 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()))
result = 0

for i in range(1, N+1):
  for j in range(M):
    if not(L[j] <= i <= R[j]):
      break
    if j == M-1:
      result += 1

print(result)

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

for i in range(N):
    if max(L) <= i+1 <= min(R):
        ans += 1
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)
L_max = max(L)
L_min = min(R)

for i in range(N):
    if L_max <= i+1 <= L_min:
        ans += 1
print(ans)
# Original solution
import math
s = input().split()
n=int(s[0])
m=int(s[1])
d={}
for i in range(1,n+1):d[i]=0
for i in range(m):
    s = list(map(int,input().split()))
    for j in range(s[0],s[1]+1):
        d[j]+=1
print((list(d.values()).count(m)))


# Optimized solution
import math
s = input().split()
n=int(s[0])
m=int(s[1])
l=0
r=float('inf')
for i in range(m):
    s = list(map(int,input().split()))
    l=max(l,s[0])
    r=min(r,s[1])
a=r-l
if a>=0:
    print((a+1))
else:
    print((0))
# Original solution
import sys

N, M = list(map(int, sys.stdin.readline().split()))

L = []
R = []

for i in range(M):
    temp_l, temp_r = list(map(int, sys.stdin.readline().split()))
    L.append(temp_l)
    R.append(temp_r)

master_list = list(range(1, N+1))
master_set = set(master_list)

ans = master_set
for i in range(M):
    current_set = set(list(range(L[i], R[i]+1)))
    ans = ans & current_set

print((len(ans)))

# Optimized solution
import sys

N, M = list(map(int, sys.stdin.readline().split()))

L = []
R = []

for i in range(M):
    temp_l, temp_r = list(map(int, sys.stdin.readline().split()))
    L.append(temp_l)
    R.append(temp_r)

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

if temp > 0:
    print(temp)
else:
    print((0))
# Original solution
from collections import Counter
n,m = list(map(int, input().split()))
lr = []
for i in range(m):
    l,r = list(map(int, input().split()))
    lr += list(range(l,r+1))
lr = Counter(lr).most_common()
num = 0
for h in range(len(lr)):
    if lr[h][1] == m:
        num += 1
    else:
        break
print(num)

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

# Optimized solution
n,m = list(map(int, input().split()))
mini = 0
maxi = 10**9
for i in range(m):
    l,r = list(map(int, input().split()))
    if mini < l:
        mini = l
    if r < maxi:
        maxi = r
t = maxi -mini + 1
if maxi < mini:
    print((0))
else:
    print(t)
# Original solution
import sys
def input(): return sys.stdin.readline().rstrip()

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

    l, r = list(map(int, input().split()))
    ans = [i for i in range(l, r+1)]
    for _ in range(m-1):
        l, r = list(map(int, input().split()))
        ans = [i for i in ans if l <= i <= r]
    print((len(ans)))

if __name__ == '__main__':
    main()


# Optimized solution
import sys
def input(): return sys.stdin.readline().rstrip()

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

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

if __name__ == '__main__':
    main()
# Original solution
N,M = list(map(int,input().split()))
a,b = list(map(int, input().split()))
former_set = { i for i in range(a,b+1)}
for i in range(M-1):
  a,b =  list(map(int, input().split()))
  tmp_set = { i for i in range(a,b+1)}
  former_set = tmp_set & former_set
print((len(former_set)))

# Optimized solution
N,M = list(map(int,input().split()))
a = list()
b = list()
for i in range(M):
  tmp_a,tmp_b =  list(map(int, input().split()))
  a.append(tmp_a)
  b.append(tmp_b)
print((min(b)-max(a)+1 if min(b)-max(a) >= 0 else 0))
# Original solution
import sys
nm = input().split()
n = int(nm[0])
m = int(nm[1])
lr = [input().split() for i in range(m)]
l = [int(lr[i][0]) for i in range(m)]
r = [int(lr[i][1]) for i in range(m)]

card = [i for i in range(n + 1)]
if m == 1:
  print((len(card[l[0]:r[0]]) + 1))
  sys.exit()
card2 = []
card_duplicate = []
for i in range(m):
  #card2 = card_duplicate
  card2.extend(card[l[i]-1:r[i]])

card_duplicate = [x for x in set(card2) if card2.count(x) == m]

print((len(card_duplicate)))

# Optimized solution
nm = input().split()
n = int(nm[0])
m = int(nm[1])
lr = [input().split() for i in range(m)]
l = [int(lr[i][0]) for i in range(m)]
r = [int(lr[i][1]) for i in range(m)]
a = min(r) -max(l) + 1
if a >= 0:
  print(a)
else:
  print((0))
# Original solution
n, m = [int(x) for x in input().split()]
lst = []
for _ in range(m):
    l, r = [int(x) for x in input().split()]
    lst.append(set(range(l, r+1)))

uni = lst[0].intersection(*lst[1:])
print((len(uni)))

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

l_max = max(l_lst)
r_min = min(r_lst)

if l_max <= r_min:
    num = r_min - l_max + 1
else:
    num = 0
print(num)
# Original solution
# 2019-11-13 08:30:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]

    l, r = [], []
    for _ in range(m):
        li, ri = [int(x) for x in sys.stdin.readline().split()]
        l.append(li)
        r.append(ri)
    
    all_mighty_cards = set(range(1,n+1))
    for i in range(m):
        all_mighty_cards &= set(l[i:] + r[:i+1])
    


    print((max(all_mighty_cards) - min(all_mighty_cards) + 1 if all_mighty_cards else 0))

    

if __name__ == "__main__":
    main()


# Optimized solution
# 2019-11-13 08:30:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]

    l, r = [], []
    for _ in range(m):
        li, ri = [int(x) for x in sys.stdin.readline().split()]
        l.append(li)
        r.append(ri)
    
    print((min(r) - max(l) + 1 if min(r) >= max(l) else 0))



if __name__ == "__main__":
    main()
# Original solution
import sys

n, m, *lr = list(map(int, sys.stdin.read().split()))
lr = list(zip(*[iter(lr)] * 2))

def main():
    cnt = [0] * (n + 1)
    for l, r in lr:
        cnt[l-1] += 1
        cnt[r] -= 1
    
    for i in range(n):
        cnt[i+1] += cnt[i]
    
    res = 0
    for i in range(n):
        res += (cnt[i] == m) & 1
    
    return res

if __name__ == '__main__':
    ans = main()
    print(ans)

# Optimized solution
import sys

n, m, *lr = list(map(int, sys.stdin.read().split()))
l, r = lr[::2], lr[1::2]

def main():
    ans = max(min(r) - max(l) + 1, 0)
    return ans

if __name__ == '__main__':
    ans = main()
    print(ans)
# Original solution
N,M = [int(i) for i in input().split()]
L=[]
R=[]
for n in range(M):
    temp = [int(i) for i in input().split()]
    L.append(temp[0])
    R.append(temp[1])
init_set = set(range(L[0],R[0]+1))
for n in range(M-1):
    now_set = set(range(L[n+1],R[n+1]+1))
    init_set = (init_set & now_set)
print((len(init_set)))

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

ans = 0

for j in range(n):
    flag = 1
    for k in range(m):
        if j < l[k]-1 or j > r[k]-1:
            flag = 0
            break
    if flag == 1:
        ans += 1

print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
l = list()
r = list()
for i in range(m):
    a, b = list(map(int, input().split()))
    l.append(a)
    r.append(b)

if min(r) >= max(l):
    print((min(r) - max(l) + 1))
else:
    print((0))
# Original solution
n,m=list(map(int,input().split()))
s=0
for i in range(m):
  l,r=list(map(int,input().split()))
  temp=list(range(l,r+1))
  if i==0:
    s=set(temp)
  else:
    s&=set(temp)

print((len(s)))


# Optimized solution
n,m=list(map(int,input().split()))
li=[list(map(int,input().split())) for i in range(m)]
maxl=0
minr=1000000
for i in range(m):
  maxl=max(li[i][0],maxl)
  minr=min(minr, li[i][1])
if maxl<=minr:
  print((minr-maxl+1))
else:
  print((0))
# Original solution
n,m=list(map(int,input().split()))
l=[]
for _ in range(m):
    l.append(list(map(int,input().split())))
d=[0]*(n+1)
for i in range(m):
    for j in range(l[i][0],l[i][1]+1):
        d[j]+=1
print((d.count(m)))


# Optimized solution
n,m=list(map(int,input().split()))
l=0;r=10**6
for i in range(m):
    ln,rn=list(map(int,input().split()))
    l=max(l,ln);r=min(r,rn)
if l>r:
    print((0))
else:
    print((r-l+1))
# Original solution
n,m = list(map(int, input().split()))
ans = 0
for i in range(m):
    l,r = list(map(int, input().split()))
    tmp = set(range(l,r+1))
    if i == 0:
        ans = tmp
    else:
        ans = ans & tmp

print((len(ans)))

# Optimized solution
n,m = list(map(int, input().split()))
ans = 0
L = -100000000
R = 100000000
for i in range(m):
    l,r = list(map(int, input().split()))
    if l > L:
        L = l
    if r < R:
        R = r

print((len(list(range(L,R+1)))))
# Original solution
n, m = list(map(int, input().split()))
id = set(i for i in range(1, n + 1))
for i in range(m):
    l, r = list(map(int, input().split()))
    check = set(i for i in range(l, r + 1))
    id = id & check
print((len(id)))

# Optimized solution
n, m = list(map(int, input().split()))
id = set(i for i in range(1, n + 1))
L = []
R = []
for i 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) + 1 > 0 else 0))
# Original 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()))

cnt = 0
LR = list(zip(L,R))
res = set(range(1,N+1))
for l,r in LR:
    res &= set(range(l,r+1))
print((len(res)))

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

a, b = max(L), min(R)
if a<=b:
    print((b-a+1))
else:
    print((0))
# Original solution
def two_input():
    return list(map(int, input().split()))

N, M = two_input()
LR = [list(two_input()) for i in range(M)]

cnt = 0
for i in range(1, N+1):
    cnt += 1
    for j in range(M):
        if LR[j][0] > i or LR[j][1] < i:
            cnt -= 1
            break

print(cnt)

# Optimized solution
def two_input():
    return list(map(int, input().split()))

N, M = two_input()
L, R = [], []
for i in range(M):
    l, r = two_input()
    L.append(l)
    R.append(r)
    
maxl = max(L)
minr = min(R)

ans = 0
for i in range(N):
    if maxl <= i+1 <= minr:
        ans += 1

print(ans)