CodeEff/ECCO huggingface dataset:
# Original solution
import collections
n,m=list(map(int,input().split()))
l=[[]*i for i in range(m)]
a=set()
for i in range(m):
  s,e=list(map(int,input().split()))
  for j in range(s,e+1):
    l[i].append(j)
for i in range(m):
  if i==0:
    a=set(l[i])
  else:
    a=a&set(l[i])
print((len(a)))

# Optimized solution
n,m=list(map(int,input().split()))
l,r=0,1000000
for i in range(m):
  a,b=list(map(int,input().split()))
  l=max(l,a)
  r=min(r,b)
print((len(list(range(l,r+1)))))
# Original solution
N, M=list(map(int, input().split()))

for i in range(M):
  L,R=list(map(int, input().split()))
  if i==0:
    LR=set(list(range(L,R+1)))
  else:
    LR=LR&set(list(range(L,R+1)))
print((len(LR)))

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

a=1
b=N
for i in range(M):
  L,R=list(map(int, input().split()))
  a=max(a,L)
  b=min(b,R)
  
if b>=a:
  print((b-a+1))
else:
  print((0))
# Original solution
import sys
sys.setrecursionlimit(200000)
n, m = list(map(int, input().split()))
def cutterl(l, lst) :
    if lst == [] :
        return []
    elif lst[0] < l :
        return cutterl(l, lst[1:])
    else :
        return lst
def cutterr(r, lst) :
    if lst == [] :
        return []
    elif r < lst[-1] :
        return cutterr(r, lst[:-1])
    else :
        return lst
nlist = list([x + 1 for x in range(n)])
llist=[]
rlist=[]
for i in range(m) :
    l, r = list(map(int, input().split()))
    llist.append(l)
    rlist.append(r)
nlist = cutterl(max(llist), nlist)
nlist = cutterr(min(rlist), nlist)
print((len(nlist)))

# Optimized solution
n, m = list(map(int, input().split()))
llist = []
rlist = []
for i in range(m) :
    l, r = list(map(int, input().split()))
    llist.append(l)
    rlist.append(r)
ll = max(llist)
rr = min(rlist)
print((max(0, min(rr, n)-ll+1)))
# Original solution
N,M= list(map(int, input().split()))
li=[]
for i in range(N):
    li.append(0)
for i in range(M):
    L,R= list(map(int, input().split()))
    for t in range(L-1,R):
        li[t] +=1
print((li.count(M)))


# Optimized solution
N,M= list(map(int, input().split()))
L,R= list(map(int, input().split()))
flag = True
for i in range(M-1):
    a,b= list(map(int, input().split()))
    if a >= L and b <= R:
        L = a
        R = b
    elif L <= b and R >= b:
        R = b
    elif L <= a and R >= a:
        L = a
    elif a < L and R < b:
        L = L
    else:
        flag = False
        break
if flag:
 print((R-L+1))
else:
    print((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()))
count = 0
for i in range(N):
	count += 1
	for j in range(M):
		if i+1 < L[j] or i+1 > R[j]:
			count -= 1
			break
print(count)

# 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()))
s = 1
e = N
for i in range(M):
	if s < L[i]:
		s = L[i]
	if e > R[i]:
		e = R[i]
if s > e:
	print((0))
else:
	print((e-s+1))
# Original solution
L,M=list(map(int,input().split()))
a=[0]*L
for i in range(M):
    b,c=list(map(int,input().split()))
    for j in range(b-1,c):
        a[j]+=1
print((a.count(M)))

# Optimized solution
L,M=list(map(int,input().split()))
a=[0]*L
x=[]
y=[]
for i in range(M):
    b,c=list(map(int,input().split()))
    x.append(b)
    y.append(c)
if max(x)<=min(y):
    print((-max(x)+min(y)+1))
else:
    print((0))
# Original solution
N,M=list(map(int,input().split()))
list1=[list(map(int,input().split())) for i in range(M)]
list2=list(range(N+1))
ans=set(range(N+1))
for i in range(M):
    lr=set(list2[list1[i][0]:list1[i][1]+1])
    #print(lr)
    ans=ans&lr
#print(ans)
print((len(ans)))


# Optimized solution
N,M=list(map(int,input().split()))
list1=[list(map(int,input().split())) for i in range(M)]
start=[]
end=[]
for i in range(M):
    start.append(list1[i][0])
    end.append(list1[i][1])
stmax=max(start)
#print(stmax)
enmax=min(end)
#print(enmax)
if enmax>=stmax:
    print((enmax-stmax+1))
else:
    print((0))
# Original solution
n, m = list(map(int, input().split()))
l1, r1 = list(map(int, input().split()))
li = set(list(range(l1, r1+1)))

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

# Optimized solution
a,b= list(map(int,input().split()))
x, y= list(zip(*[list(map(int,input().split())) for i in range(b)]))
f=  min(y) - max(x) 
if f< 0 :
  print((0))
else :
  print((min(y) - max(x) +1))
# Original solution
a,b= list(map(int,input().split()))
x, y= list(zip(*[list(map(int,input().split())) for i in range(b)]))
f=  min(y) - max(x) 
if f< 0 :
  print((0))
else :
  print((min(y) - max(x) +1))


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

for i in range(m):
    l, r = list(map(int, input().split()))
    low = max(low, l)
    high = min(high, r)

print((high-low+1 if low <= high else 0))
# Original solution
n,m=list(map(int,input().split()))
c=list()

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

s=(list([x[0] for x in c]))
b=(list([x[1] for x in c]))

maxs=max(s)
minb=min(b)

if minb<maxs:
    print('0')
else:
    ans=minb-maxs+1
    print(ans)

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

if y-x<0:
    print('0')
else:
    print((y-x+1))
# Original solution
N,M = list(map(int,input().split()))
def inc(a,b):
    h = []*(a-b)
    for p in range(a,b+1):
        if p <= b:
            h.append(p)
    return set(h)
for p in range(M):
    L,M  = list(map(int,input().split()))
    if p == 0:
        ls = inc(L,M)
    else:
        ls = ls & inc(L,M)

print((len(ls)))


# Optimized solution
N,M = list(map(int,input().split()))
ls_a = []
ls_b = []
for p in range(M):
    L,R  = list(map(int,input().split()))
    ls_a.append(L)
    ls_b.append(R)
ans = min(ls_b)-max(ls_a)+1
if ans < 0:
    print((0))
else:
    print(ans)
# Original solution
n,m=list(map(int,input().split()))
lr=[[int(i) for i in input().split()] for i in range(m)]
cnt=[i for i in range(lr[0][0],lr[0][1]+1)]
ans=len(cnt)
for i in range(m):
    cnt=list(set([i for i in range(lr[i][0],lr[i][1]+1)])&set(cnt))
    ans=len(cnt)
    if len(cnt)==0:
        ans=0
        break
print(ans)

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

# Optimized solution
N, M = list(map(int, input().split()))
minNum = 1
maxNum = N
for lr in range(M):
  l, r = list(map(int, input().split()))
  if minNum <= l and r <= maxNum:
    minNum = l
    maxNum = r
  elif minNum <= l <= maxNum and maxNum <= r:
    minNum = l
  elif l <= minNum and minNum <= r <= maxNum:
    maxNum = r
  elif r < minNum or maxNum < l:
    print("0")
    exit()
#  print(l,r,minNum,maxNum)
print((maxNum - minNum +1))
# Original solution
n, m = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(m)]
ans =[0] * (n + 1)

for l, r in LR:
  ans[l - 1] += 1
  ans[r] -= 1
  
accu =[0] * (n + 1)
for i in range(n):
  accu[i + 1] += ans[i] + accu[i]
  
ans = accu.count(m)

print(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)
  
ans = max(0, min(R) - max(L) + 1)

print(ans)
# Original solution
N, M = list(map(int, input().split()))
path = set([n for n in range(1, N + 1)])
for _ in range(M):
    l, r = list(map(int, input().split()))
    pi = set([n for n in range(l, r + 1)])
    path &= pi
print((len(path)))

# Optimized solution
N, M = list(map(int, input().split()))
l, r = 1, 10 ** 5

for _ in range(M):
    li, ri = list(map(int, input().split()))
    l = max(l, li)
    r = min(r, ri)

ans = r - l + 1 if r >= l else 0
print(ans)
# Original solution
S = []
N, M = input().split(" ")
for m in range(int(M)):
  a, b = input().split(" ")
  S.append(set([i for i in range(int(a),int(b)+1)]))
if M == 1:
  print((len(S[0])))
else:
  sets = S[0]
  for i in range(1,int(M)):
    sets = sets & S[i]
  print((len(sets)))

# Optimized solution
N, M = input().split(" ")
if M == 1:
    a, b = input().split(" ")
    print((int(b)-int(a)+1))
else:    
    a, b = input().split(" ")
    sets = set([i for i in range(int(a),int(b)+1)])
    for m in range(int(M)-1):
        a, b = input().split(" ")
        sets = sets & set([i for i in range(int(a),int(b)+1)])
    print((len(sets)))
# Original solution
N, M = input().split(" ")
if M == 1:
    a, b = input().split(" ")
    print((int(b)-int(a)+1))
else:    
    a, b = input().split(" ")
    sets = set([i for i in range(int(a),int(b)+1)])
    for m in range(int(M)-1):
        a, b = input().split(" ")
        sets = sets & set([i for i in range(int(a),int(b)+1)])
    print((len(sets)))

# Optimized solution
N, M = input().split(" ")
#S = []
if M == 1:
    a, b = input().split(" ")
    print((int(b)-int(a)+1))
else:    
    a, b = input().split(" ")
    aM, bm = int(a),int(b)
    #sets = set([i for i in range(int(a),int(b)+1)])
    for m in range(int(M)-1):
        a, b = input().split(" ")
        aM = max(aM,int(a))
        bm = min(bm,int(b))
        #sets = sets & set([i for i in range(int(a),int(b)+1)])
    if(bm<aM):
        print((0))
    else:
        print((int(bm)-int(aM)+1))
# Original solution
n,m = list(map(int,input().split()))
l = []
r = []
for i in range(m):
  A, B = list(map(int,input().split()))
  l.append(A)
  r.append(B)
sum = 0
for i in range(1,n+1,1):
  flag = 1
  for j in range(m):
    if i < l[j] or i > r[j]:
      flag = 0
      break
  if flag == 1:
    sum += 1
print(sum)

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

min = 0
max = n
for i in range(m):
  if min < l[i]:
    min = l[i]
  if max > r[i]:
    max = r[i]

if max - min + 1 > 0:
  print((max - min + 1))
else:
  print((0))
# Original solution
import itertools
n,m = list(map(int, input().split()))
d = [0]*n
for i in range(m):
    l,r = list(map(int, input().split()))
    for i in range(l-1,r):
        d[i]+=1
count = 0
for i in d:
    if i == m:
        count+=1       
print(count)

# Optimized solution
n,m = list(map(int, input().split()))
fn = 1
ln = n
for i in range(m):
    l,r = list(map(int, input().split()))
    fn = max(fn,l)
    ln = min(ln,r)    
print((ln-fn+1 if ln-fn>=0 else 0))
# Original solution
n,m = list(map(int, input().split()))
cn = [i for i in range(1,n+1)]
for i in range(m):
    l,r = list(map(int, input().split()))
    lr = [i for i in range(l,r+1)]
    cn = set(cn)&set(lr)
print((len(cn)))

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

print(ans)

# Optimized solution
def main():
    n,m=list(map(int,input().split()))
    CARD=[0]*(n+1)
    for i in range(m):
        L,R=list(map(int,input().split()))
        CARD[L-1]+=1
        CARD[R]-=1
    ans=0
    for i in range(n):
        if CARD[i]==m:ans+=1
        CARD[i+1]=CARD[i+1]+CARD[i]

    print(ans)

if __name__=='__main__':
    main()
# Original solution
def main():
    n,m=list(map(int,input().split()))
    CARD=[0]*(n+1)
    for i in range(m):
        L,R=list(map(int,input().split()))
        CARD[L-1]+=1
        CARD[R]-=1
    ans=0
    for i in range(n):
        if CARD[i]==m:ans+=1
        CARD[i+1]=CARD[i+1]+CARD[i]

    print(ans)

if __name__=='__main__':
    main()

# Optimized solution
def main():
    import sys
    input = sys.stdin.readline

    n,m=list(map(int,input().split()))
    CARD=[0]*(n+1)
    for i in range(m):
        L,R=list(map(int,input().split()))
        CARD[L-1]+=1
        CARD[R]-=1
    ans=0
    for i in range(n):
        if CARD[i]==m:ans+=1
        CARD[i+1]=CARD[i+1]+CARD[i]

    print(ans)

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

# Optimized solution
N, M = list(map(int,input().split()))
li1 = []
li2 = []
for i in range(M):
    L, R = list(map(int,input().split()))
    li1.append([L,R])
    li2.append([R,L])
li1.sort()
li2.sort(reverse=True)
if (li2[M-1][0])<(li1[M-1][0]):
    print("0")
    exit()
print(((li2[M-1][0])-(li1[M-1][0])+1))
# Original solution
n, m = list(map(int, input().split()))
lr = []
for i in range(m):
    lr.append(list(map(int, input().split())))

canPass = [m] * n

for j in range(len(lr)):
    l = lr[j][0] - 1
    r = lr[j][1] - 1
    for k in range(l, r + 1):
        canPass[k] -= 1

ans = canPass.count(0)
print(ans)


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

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

print((len(ans)))

# Optimized solution
n,m=list(map(int, input().split()))
l_ans=1
r_ans=n
for i in range(m):
    l,r=list(map(int, input().split()))
    l_ans=max(l_ans,l)
    r_ans=min(r_ans,r)

if r_ans>=l_ans:
    print((r_ans-l_ans+1))
else:
    print((0))
# Original solution
n, m = [int(x) for x in input().split()]

key_set = None
for _ in range(m):
    l, r = [int(x) for x in input().split()]
    if not key_set:
        key_set = set(range(l, r + 1))
    else:
        key_set &= set(range(l, r + 1))
print((len(key_set)))

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

l_list = [0] * m
r_list = [0] * m
for i in range(m):
    l_list[i], r_list[i] = [int(x) for x in input().split()]

print((max(min(r_list) - max(l_list) + 1, 0)))
# Original solution
n, m = list(map(int, input().split()))
card=[0 for _ in range(n)]
for i in range(m):
    l, r = [int(x)-1 for x in input().split()]
    for i in range(l,r+1):
        card[i]+=1
print((card.count(m)))


# Optimized solution
n, m = list(map(int, input().split()))
card=[0 for _ in range(n)]
l, r = list(map(int, input().split()))
l-=1
for i in range(m-1):
    li, ri = list(map(int, input().split()))
    li-=1
    if li>r or ri<l:
        l=r
        break
    elif li>=l and ri<=r:
        l=li; r=ri
    elif li<=l and ri>=r:
        pass
    elif li<=r and ri>=r:
        l=li
    elif ri>=l and li<=l:
        r=ri
    else:
        pass
print((r-l))
# Original solution
n, m = list(map(int, input().split()))
card=[0 for _ in range(n)]
l, r = list(map(int, input().split()))
l-=1
for i in range(m-1):
    li, ri = list(map(int, input().split()))
    li-=1
    if li>r or ri<l:
        l=r
        break
    elif li>=l and ri<=r:
        l=li; r=ri
    elif li<=l and ri>=r:
        pass
    elif li<=r and ri>=r:
        l=li
    elif ri>=l and li<=l:
        r=ri
    else:
        pass
print((r-l))


# Optimized solution
n, m = list(map(int, input().split()))
card=[0 for _ in range(n)]
l=[]; r=[]
for i in range(m):
    li, ri = list(map(int, input().split()))
    l.append(li)
    r.append(ri)
print((max(0, min(r)-max(l)+1)))
# Original 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)

s = []
for i in range(M):
    s.append([i for i in range(L[i], R[i]+1)])

a = s[0]
for i in s:
    a = set(a) & set(i)

ans = 0
for i in range(N+1):
    if i in a:
        ans += 1

print(ans)

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

l, r = list(map(int, input().split()))
a = set([i for i in range(l, r+1)])
for i in range(M-1):
    l, r = list(map(int, input().split()))
    a = set(a) & set([i for i in range(l, r+1)])

ans = 0
for i in range(N+1):
    if i in a:
        ans += 1

print(ans)
# Original 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 = 0    
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 = [], []

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()))
LR = [list(map(int, input().split())) for _ in range(M)]

c = [0 for _ in range(N)]
cnt = 0
for i, (l, r) in enumerate(LR):
    for j in range(l - 1, r):
        c[j] += i + 1
    cnt += i + 1

s = [1 if k == cnt else 0 for k in c]

print((sum(s)))


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

left = 1
right = N

for l, r in LR:
    left = max(left, l)
    right = min(right, r)

ans = right - left

print((ans + 1 if ans >= 0 else 0))
# Original solution
N, M = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(M)]

left = 1
right = N

for l, r in LR:
    left = max(left, l)
    right = min(right, r)

ans = right - left

print((ans + 1 if ans >= 0 else 0))


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

left = 1
right = N

for l, r in LR:
    if left < l:
        left = l
    if right > r:
        right = r

ans = right - left

print((ans + 1 if ans >= 0 else 0))
# Original solution
N, M = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(M)]

left = 1
right = N

for l, r in LR:
    if left < l:
        left = l
    if right > r:
        right = r

ans = right - left

print((ans + 1 if ans >= 0 else 0))


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

left = 1
right = N

for _ in range(M):
    l, r = list(map(int, input().split()))

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

ans = right - left

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

left = 1
right = N

for _ in range(M):
    l, r = list(map(int, input().split()))

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

ans = right - left

print((ans + 1 if ans >= 0 else 0))


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

    left = 1
    right = N

    for _ in range(M):
        l, r = list(map(int, input().split()))

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

    ans = right - left

    print((ans + 1 if ans >= 0 else 0))


main()
# Original solution
n,m = list(map(int,input().split()))
gates = []
res = 0
for i in range(m):
	l,r = list(map(int,input().split()))
	gates.append({ i for i in range(l,r+1)})
r={i for i in range(1,n+1)}

for g in gates :
	r = r & g
print((len(r)))

# Optimized solution
n,m = list(map(int,input().split()))
res={i for i in range(1,n+1)}
for i in range(m):
	l,r = list(map(int,input().split()))
	g = { i for i in range(l,r+1)}
	res = res&g
	if res == {} :
		print((0))
		break
print((len(res)))
# Original solution
n,m = list(map(int,input().split()))
res={i for i in range(1,n+1)}
for i in range(m):
	l,r = list(map(int,input().split()))
	g = { i for i in range(l,r+1)}
	res = res&g
	if res == {} :
		print((0))
		break
print((len(res)))

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

passcard = [i+l[0][0] for i in range(l[0][1] - l[0][0]+1)]
for t in range(n[1]-1):
    passcard = [i for i in passcard if l[t+1][0] <= i <= l[t+1][1]]
    
print((len(passcard)))

# Optimized solution
n = list(map(int,input().split()))
l = [list(map(int,input().split())) for i in range(n[1])]
minp = l[0][0]
maxp = l[0][1]
for t in range(n[1]):
    minp = max(minp,l[t][0])
    maxp = min(maxp,l[t][1])
print((max(0,maxp-minp+1)))
# Original solution
import os, sys, re, math

N, M = [int(s) for s in input().split(' ')]

cards = [True for _ in range(N+1)]
cards[0] = False

for _ in range(M):
    l,r = [int(s) for s in input().split(' ')]
    for i in range(1,l):
        cards[i] = False
    for j in range(r+1,len(cards)):
        cards[j] = False

print((sum(cards)))


# Optimized solution
import os, sys, re, math

N, M = [int(s) for s in input().split(' ')]

min_idx = 1
max_idx = 1e+5

for _ in range(M):
    l,r = [int(s) for s in input().split(' ')]
    min_idx = max(min_idx, l)
    max_idx = min(max_idx, r)
if max_idx >= min_idx:
    print((min(N, int(max_idx - min_idx + 1))))
else:
    print((0))
# Original solution
import os, sys, re, math

N, M = [int(s) for s in input().split(' ')]

min_idx = 1
max_idx = 1e+5

for _ in range(M):
    l,r = [int(s) for s in input().split(' ')]
    min_idx = max(min_idx, l)
    max_idx = min(max_idx, r)
if max_idx >= min_idx:
    print((min(N, int(max_idx - min_idx + 1))))
else:
    print((0))


# Optimized solution
import os, sys, re, math

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

L = 1
R = N

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

ret = max(0, R-L+1)
print(ret)
# Original solution
import collections

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

x = []  
for _ in range(M):
  L,R = list(map(int,input().split()))
  x.extend(list(range(L,R+1)))
    
c = collections.Counter(x)

print((len([i[0] for i in list(c.items()) if i[1] == M])))

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

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

if min(R) >= max(L):
  print((min(R)-max(L)+1))
else:
  print((0))
# Original solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]

clear_list = [1] * N
    
for i in range(M):
    start, end  = [int(x) for x in stdin.readline().rstrip().split()]
    for i in range(1,N+1):
        if i < start or end < i:
            clear_list[i-1] = 0

print((clear_list.count(1)))


# Optimized solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]

clear_list = [1] * N
start = 0
end = 0
ans_start = 0
ans_end = 0
    
for i in range(M):
    start, end  = [int(x) for x in stdin.readline().rstrip().split()]
    if ans_start == 0 or ans_start < start : 
        ans_start = start
    if ans_end == 0 or ans_end > end:
        ans_end = end

if ans_end - ans_start < 0:
    print((0))
else:
    print((ans_end - ans_start + 1 ))
# Original solution
n,m = list(map(int,input().split()))
ans = {}
for _ in range(m):
    l,r = list(map(int,input().split()))
    gate = []
    if ans:
        for i in range(max(l,min(ans)),min(r+1,max(ans))+1):
            gate.append(i)
        ans &= set(gate)
    else:
        for i in range(l,r+1):
            gate.append(i)
        ans = set(gate)
print((len(ans)))

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