CodeEff/ECCO huggingface dataset:
# Original solution
#coding:utf-8
n, m = list(map(int, input().split()))
l, r = 0, 10**6
for i in range(m):
    a, b = list(map(int, input().split()))
    l = max(l, a)
    r = min(r, b)
print((r - l  + 1 if l <= r else 0))

# Optimized solution
# coding: utf-8
N, M = list(map(int, input().split()))
L, R = 0, 10**6
card = []
for i in range(M):
    l, r = list(map(int, input().split()))
    L = max(L, l)
    R = min(R, r)
print((R - L + 1 if L <= R else 0))
# Original solution
card, gate = list(map(int, input().split()))
l, r = list(map(int, input().split()))
ans = {i for i in range(l, r+1)}
for i in range(gate-1):
  l, r = list(map(int, input().split()))
  ans = ans & {i for i in range(l, r+1)}
print((len(ans)))

# Optimized solution
card, gate = list(map(int, input().split()))
max_l = 0
min_r = 100000
for i in range(gate):
  l, r = list(map(int, input().split()))
  max_l = max(max_l, l)
  min_r = min(min_r, r)
print((len(list(range(max_l, min_r+1)))))
# Original solution
N, M = list(map(int, input().split()))
card = set(list(range(1, N+1)))
for _ in range(M):
  l, r = list(map(int, input().split()))
  gate = set(list(range(l, r+1)))
  card &= gate
print((len(card)))

# 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)
  
max_L   = max(L)
min_R = min(R)
print((len(list(range(max_L, min_R+1)))))
# Original solution
n,m = [int(s) for s in input().split(' ')]

ans = []
anss = None
for i in range(m):
    l,r = [int(s) for s in input().split(' ')]
    temp = set(i for i in range(l,r+1))
    ans.append(temp)
    if i == 0:
        anss = ans[0]
    else:
        anss &= ans[i] 
print((len(anss)))

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

ans = []
minn = 10000000
maxx = 0
for i in range(m):
    l,r = [int(s) for s in input().split(' ')]
    maxx = max(maxx,l)
    minn = min(minn,r)
if minn - maxx < 0:
    print((0))
else:
    print((minn - maxx+1))
# Original solution
n,m=list(map(int,input().split()))
l=[list(map(int,input().split())) for i in range(m)]
nums=[x+1 for x in range(n)]
cnt=0
ans=0
for i in nums:
  cnt=0
  for t in l:
    if(t[0]<=i<=t[1]):
      cnt+=1
  if(cnt==len(l)):
    ans+=1
print(ans)

# Optimized solution
n,m=list(map(int,input().split()))
l=[list(map(int,input().split())) for i in range(m)]
nums=[x+1 for x in range(n)]
ans=0
r=[0,100000]
for i in l:
  r[0]=max(r[0],i[0])
  r[1]=min(r[1],i[1])
for i in nums:
  if(r[0]<=i<=r[1]):
    ans+=1
print(ans)
# 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 j in range(l,r+1):
    a[j-1] += 1
print((a.count(m)))

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

a=0
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+1 >= 0:
  print((b-a+1))
else:
  print((0))
# Original solution
n, m = list(map(int, input().split()))

a=0
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+1 >= 0:
  print((b-a+1))
else:
  print((0))

# Optimized solution
n, m = list(map(int, input().split()))
l = 1
r = 10**5
for _ in range(m):
  l_, r_ = list(map(int, input().split()))
  l = max(l, l_)
  r = min(r, r_)
  
if r>=l:
  print((r-l+1))
else:
  print((0))
# Original solution
n , m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(m)]

d = [0]*n
for i in range(m):
  for j in range(lr[i][0]-1, lr[i][1]):
    d[j] += 1
    
print((d.count(m)))

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

mi = 0
ma = n
for i in range(m):
  mi = max(mi, lr[i][0])
  ma = min(ma, lr[i][1])
  
  
if ma-mi>=0:
  print((ma-mi+1))
else:
  print((0))
# Original solution
n , m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(m)]

mi = 0
ma = n
for i in range(m):
  mi = max(mi, lr[i][0])
  ma = min(ma, lr[i][1])
  
  
if ma-mi>=0:
  print((ma-mi+1))
else:
  print((0))

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

for i in range(M):
    tmp = list(map(int, input().split()))
    if i == 0:
        ans = [x for x in range(tmp[0], tmp[1]+1)]
    else:
        cur = [x for x in range(tmp[0], tmp[1]+1)]
        ans = [x for x in cur if x in ans]

print((len(ans)))

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

for i in range(M):
    tmp = list(map(int, input().split()))
    if L<tmp[0]:
        L = tmp[0]
    if R>tmp[1]:
        R = tmp[1]

ans = [x for x in range(L, R+1)]

print((len(ans)))
# Original solution
N, M = list(map(int, input().split()))
L, R = list(map(int, input().split()))
l1 = list(range(L, R+1))

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

# Optimized solution
N, M = list(map(int, input().split()))
start = []
end = []
for i in range(M):
    L, R = list(map(int, input().split()))
    start.append(L)
    end.append(R)
start_max = max(start)
end_min = min(end)
print((max(0, end_min - start_max + 1)))
# Original solution
n,m = list(map(int,input().split()))
card = [int(0) 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] += 1
#print(card)
cnt = 0
for i in range(n+1):
    if card[i] == m:
        cnt += 1
print(cnt)

# Optimized solution
n,m = list(map(int,input().split()))
min_card,max_card = 0,n
for i in range(m):
    l,r = list(map(int,input().split()))
    min_card = max(min_card,l)
    max_card = min(max_card,r)
#print(min_card,max_card)
if min_card<=max_card:
    print((max_card-min_card+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()))

a = [0]*M

for j in range(M):
    a[j] = set(range(L[j],R[j]+1))

origin = set(range(1,N+1))
for k in range(M):
    origin &= a[k]

print((len(origin)))

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

min = L[0]
max = R[0]

for j in range(1,M):
    if min < L[j]:
        min = L[j]
    if max > R[j]:
        max = R[j]
if max-min+1 >= 0:
	print((max-min+1))
else:
  print((0))
# Original 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)
cnt = 0
for i in range(1,n+1):
    for j in range(m):
        if l[j]>i or r[j]<i:
            break
    else:
        cnt+=1
print(cnt)

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

LRs = [list(map(int, input().split())) for i in range(m)]
LRs = [set(range(LRs[i][0], LRs[i][1] + 1)) for i in range(m)]

for i in range(m):
    if i == 0:
        union = LRs[i]
    else:
        union = union & LRs[i]

print((len(union)))

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

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

under = 1
top = 10**6+10
for i in range(m):
    if LRs[i][0] > under:
        under = LRs[i][0]
    if LRs[i][1] < top:
        top = LRs[i][1]

if not top >= under:
    print((0))
else:

    print((top - under + 1))
# Original solution
n,m=list(map(int,input().split()))
set0=set(range(1,n+1))
for i in range(m):
  l,r=list(map(int,input().split()))
  id_set=set(range(l,r+1))
  set0=set0&id_set
print((len(set0)))

# Optimized solution
n,m=list(map(int,input().split()))
l0,r0=1,n
for i in range(m):
  l,r=list(map(int,input().split()))
  if l>r0 or r<l0:
    print((0))
    exit()
  else:
    l0=max(l0,l)
    r0=min(r0,r)
print((r0-l0+1))
# Original solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))
card = set(range(l, r+1))
for i in range(1, m):
    l, r = list(map(int, input().split()))
    card = card.intersection(set(range(l, r+1)))

print((len(card)))

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

min_id = 1
max_id = n
for i in range(m):
    l, r = list(map(int, input().split()))

    if min_id < l <= max_id:
        min_id = l
    elif max_id < l:
        print((0))
        sys.exit()

    if min_id <= r < max_id:
        max_id = r
    elif r < min_id:
        print((0))
        sys.exit()

print((max_id - min_id + 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()))

ALL = [0] * N

for i in range(M):
  for j in range(L[i]-1, R[i]):
    ALL[j] += 1

print((ALL.count(M)))  

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

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

# for i in range(n):
#     nums.append(i + 1)

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

    if i == 0:
        for j in range(l, r + 1):
            nums.append(j)

    nums = list([x for x in nums if l <= x <= r])

    if len(nums) == 0:
        print((0))
        sys.exit()

print((len(nums)))

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

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

lmax = 0
mmin = 10**5

for i in range(m):
    lmax = max(lmax, lr[i][0])
    mmin = min(mmin, lr[i][1])

print((max(0, mmin - lmax + 1)))
# Original solution
n,m = list(map(int,input().split()))
key = []
for i in range(1,n+1):
    key.append(i)
ans = []
for i in range(m):
    ls = []
    L,R = list(map(int,input().split()))
    for j in range(L,R+1):
        ls.append(j)
    key = (set(ls)&set(key))
    
print((len(key)))
    


# Optimized solution
n,m = list(map(int,input().split()))
key = []
for i in range(1,n+1):
    key.append(i)
    
ls = [0,100000]
for i in range(m):
    L,R = list(map(int,input().split()))
    if L > ls[0]:
        ls[0] = L
    if R < ls[1]:
        ls[1] = R
    
ans = []    
for i in range(ls[0],ls[1]+1):
    ans.append(i)
    
print((len(set(ans)&set(key))))
# Original solution
n ,m = list(map(int,input().split()))
s = set()
for i in range(m):
    l,r = list(map(int, input().split()))
    tmp = {i for i in range(l,r+1)}
    if not s:
        s = s | tmp
        continue
    s = s&tmp
print((len(s)))

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

L, R = 0, float("inf")

for _ in range(M):
    l, r = list(map(int, input().split()))
    if l > L:
        L = l
    if r < R:
        R = r

count = 0
if L <= R:
    for i in range(1, N+1):
        if L <= i <= R:
            count += 1

print(count)

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

L, R = 0, float("inf")

for _ in range(M):
    l, r = list(map(int, input().split()))
    if l > L:
        L = l
    if r < R:
        R = r

if L <= R:
    print((R - L + 1))
else:
    print((0))
# Original solution
n, m = list(map(int,input().split()))

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

rg2  = set(range(1, n + 1))
ans = rg2 & s

print((len(ans)))


# Optimized solution
n, m = list(map(int,input().split()))
lsl = []
lsr = []
for i in range(m):
    l, r = list(map(int,input().split()))
    lsl.append(l)
    lsr.append(r)
lmax = max(lsl)
rmin = min(lsr)

if lmax > rmin:
    print((0))
else:
    ans1 = set(range(1,n + 1))
    ans2 = set(range(lmax,rmin + 1))
    ans = ans1 & ans2
    print((len(ans)))
# Original solution
import sys, math, collections, heapq, itertools
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    N, M = list(map(int, line_input()))
    gate = [0 for n in range(N+1)]
    for i in range(M):
        l, r = list(map(int, line_input()))
        gate[l-1] += 1
        gate[r] -= 1
    ans = (1 if gate[0] == M else 0)
    for i in range(1, N):
        gate[i] = gate[i] + gate[i-1]
        if gate[i] == M: ans += 1
    print(ans)
    return 0
  
if __name__ == "__main__":
    solve()

# Optimized solution
import sys

def solve():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    count = [0] * (N + 1)
    for _ in range(M):
        l, r = list(map(int, input().split()))
        count[l - 1] += 1
        count[r] -= 1
    ans = (1 if count[0] == M else 0)
    for i in range(1, N):
        count[i] += count[i - 1]
        if count[i] == M: ans += 1
    print(ans)

    return 0

if __name__ =="__main__":
    solve()
# Original solution
N, M = list(map(int, input().split()))
gate = [ list(map(int, input().split())) for i in range(M) ]

nopass = 0
for i in range(1, N+1):
	for j in gate:
		if j[0] > i or j[1] < i:
			nopass += 1
			break
print((N - nopass))

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

max_L = max([ i[0] for i in gate])
min_R = min([ i[1] for i in gate])
ans = 0
if max_L > min_R:
	ans = 0
else:
	ans = min_R - max_L + 1
print(ans)
# Original solution
N, M = [int(x) for x in input().split(" ")]
L, R = [], []
for i in range(M):
    l, r = [int(x) for x in input().split(" ")]
    L.append(l - 1)
    R.append(r - 1)

pass_key = set([i for i in range(N)])
for i in range(M):
    if len(pass_key) == 0:
        print((0))
        exit(0)
    else:
        pass_key = pass_key & set([ID for ID in range(L[i], R[i] + 1)])

print((len(pass_key)))


# Optimized solution
N, M = [int(x) for x in input().split(" ")]
L, R = [], []

for i in range(M):
    l, r = [int(x) for x in input().split(" ")]
    L.append(l - 1)
    R.append(r - 1)

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

count = R_min - L_max + 1 if R_min - L_max >= 0 else 0
print(count)
# Original solution
n, m = list(map(int, input().split()))
lrs = [ list(map(int, input().split())) for _ in range(m)]

cards = set([i for i in range(lrs[0][0], lrs[0][1]+1)])
for lr in lrs[1:]:
    tmp = set([i for i in range(lr[0], lr[1]+1)])
    cards.intersection_update(tmp)
    

print((len(cards)))
        
    

# Optimized solution
n, m = list(map(int, input().split()))
lrs = [ list(map(int, input().split())) for _ in range(m)]

cards = [lrs[0][0], lrs[0][1]] # [min, max]

for lr in lrs[1:]:
    if (lr[1] < cards[0]) or (lr[0] > cards[1]):
        print((0))
        exit()

    cards[0] = max(cards[0], lr[0])
    cards[1] = min(cards[1], lr[1])
    

print((cards[1] - cards[0] + 1))
# Original solution
N, M = list(map(int, input().split()))
id = [0 for i in range(N)]

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

print((id.count(M)))

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

max_l = 0
min_r = 100000
for _ in range(M):
  l, r = list(map(int, input().split()))
  if max_l < l:
    max_l = l
  if min_r > r:
    min_r = r

id = min_r-max_l+1
print((0 if id <0 else id))
# 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):
    flg = 1
    if i+1 < max(L) or i+1 > min(R):
            flg = 0
    ans += flg
print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
L = 0
R = N
ans = 0
for i in range(M):
    l, r = list(map(int, input().split()))
    if l > L:
        L = l
    if r < R:
        R = r
for i in range(N):
    flg = 1
    if i+1 < L or i+1 > R:
            flg = 0
    ans += flg
print(ans)
# Original solution
# -*- coding: utf-8 -*-
n, m = list(map(int, input().split()))

lst1 = [i for i in range(1, n + 1)]

for i in range(m):
  l, r = list(map(int, input().split()))
  lst = [l + j for j in range(r - l + 1)]
  lst1 = lst if len(lst1) == 0 else list(set(lst1) & set(lst))

print((len(lst1)))


# Optimized solution
# -*- coding: utf-8 -*-
n, m = list(map(int, input().split()))
max_value, min_value = -1, -1
for i in range(m):
  tmp_min_value, tmp_max_value = list(map(int, input().split()))
  min_value = max([min_value, tmp_min_value]) if min_value >= 0 else tmp_min_value
  max_value = min([max_value, tmp_max_value]) if max_value >= 0 else tmp_max_value

min_value = min_value if min_value <= n else n
max_value = max_value if max_value <= n else n

ret = max_value - min_value + 1
if ret < 0:
  ret = 0
print(ret)
# Original solution
N, M = list(map(int, input().split()))

a = {i for i in range(1, N+1)}
for i in range(M):
    L, R = list(map(int, input().split()))
    # print(a, set([i for i in range(L, R+1)]))
    a = a & {i for i in range(L, R+1)}
    # print(a, set([i for i in range(L, R+1)]))

print((len(a)))

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

a = {i for i in range(1, N+1)}
L_list = []
R_list = []
for i in range(M):
    L, R = list(map(int, input().split()))
    L_list.append(L)
    R_list.append(R)
    # print(a, set([i for i in range(L, R+1)]))
    # a = a & {i for i in range(L, R+1)}
    # print(a, set([i for i in range(L, R+1)]))

print((max(min(R_list) - max(L_list) + 1, 0)))
# Original solution
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    acc = [0] * (N+1)
    for _ in range(M):
        left, right = list(map(int, input().split()))
        acc[left-1] += 1
        acc[right] -= 1

    cnt = 0
    for a in accumulate(acc):
        if a == M:
            cnt += 1

    return cnt


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


# Optimized solution
import sys

def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    L = 0
    R = 10**5 + 1
    for _ in range(M):
        left, right = list(map(int, input().split()))
        L = max(L, left)
        R = min(R, right)

    return max(R - L + 1, 0)


if __name__ == '__main__':
    print((main()))
# Original solution
n,m=(int(i) for i in input().split())
mylist=[0 for i in range(100010)]

for i in range(m):
    a,b=(int(i) for i in input().split())
    for j in range(a,b+1):
      mylist[j]+=1

print((mylist.count(m)))

# Optimized solution
n,m=(int(i) for i in input().split())
list_a=[]
list_b=[]
for i in range(m):
  a,b=(int(i) for i in input().split())
  list_a.append(a)
  list_b.append(b)
  
max_a=max(list_a)
min_b=min(list_b)

if min_b-max_a+1>=0:
  print((min_b-max_a+1))
else:
  print((0))
# Original solution
N,M=list(map(int,input().split()))
set1=set()
set2=set()
L,R=list(map(int,input().split()))
for i in range(L,R+1):
    set1.add(i)
min=min(set1)
max=max(set1)+1
for i in range(M-1):
    L,R=list(map(int,input().split()))
    if min <= L:
        min=L
    if max >= R+1:
        max=R+1
    for j in range(min,max):
        set2.add(j)
    set1=set1&set2
    set2.clear()
print((len(set1)))


# Optimized solution
N,M=list(map(int,input().split()))
set1=set()
L,R=list(map(int,input().split()))
for i in range(L,R+1):
    set1.add(i)
min=min(set1)
max=max(set1)
Flag=True
for i in range(M-1):
    L,R=list(map(int,input().split()))
    if R < min:
        Flag=False
        break
    elif L > max:
        Flag=False
        break
    if L >= min and max >= L:
        min = L
    if R >= min and max >= R:
        max = R
if Flag:
    print((max-min+1))
else:
    print((0))
# Original solution
def main(): 


    card_cnt, gate_cnt = list(map(int, input().split()))

    LR = set(range(1, 100000+1))

    for _ in range(gate_cnt):
        L, R = list(map(int, input().split()))
        LR.intersection_update(list(range(L, R+1)))
        if len(LR) == 0:
            break

    print((len(LR)))


if __name__ == "__main__":
    main() 

# Optimized solution
def main(): 


    card_cnt, gate_cnt = list(map(int, input().split()))

    min_v, max_v = list(map(int, input().split()))
    for _ in range(1, gate_cnt):
        L,R = list(map(int, input().split()))
        min_v = max(min_v, L)
        max_v = min(max_v, R)
        if min_v > max_v:
            print((0))
            break
    else:
        print((len(list(range(min_v, max_v+1)))))

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

N,M=list(map(int,input().split()))
LR=[list(map(int,input().split())) for _ in range(M)]
cnt=0
for i in range(1,N+1):
    for l,r in LR:
        if i<l or r<i:
            break
    else:
        cnt+=1
print(cnt)

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

mm=0
mx=10**5
for _ in range(M):
    l,r = list(map(int,input().split()))
    mm = max(mm,l)
    mx= min(mx,r)
print((max(mx-mm+1,0)))
# Original solution
N, M = list(map(int, input().split()))
LR = [list(map(int, input().split())) for _ in range(M)]

s = set(range(LR[0][0], LR[0][1]+1))
for lr in LR:
  s = s.intersection(s, set(range(lr[0], lr[1]+1)))

print((len(s)))

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

minv = 100000
maxv = 1
for lr in LR:
  maxv = max(maxv, lr[0])
  minv = min(minv, lr[1])

print((max(0, minv - maxv + 1)))
# Original solution
n, m = list(map(int,input().split()))
lst = [0 for i in range(n)]
for i in range(m):
    l, r = list(map(int,input().split()))
    for j in range(l - 1, r):
        lst[j] = lst[j] + 1
ans = 0
for i in range(n):
    if(lst[i] == m):
        ans = ans + 1
print(ans)


# Optimized solution
n, m = list(map(int,input().split()))
for i in range(m):
    if (i == 0):
        l, r = list(map(int,input().split()))
    else:
        l2, r2 = list(map(int,input().split()))
        l = max(l, l2)
        r = min(r, r2)
if (l > r):
    print((0))
else:
    print((r - l + 1))
# Original solution
N, M = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(M)]
S2 = [set(range(s[0], s[1] + 1)) for s in S]
S3 = set.intersection(*S2)
print((len(S3)))

#ans = set(range(S[0][0], S[0][1] + 1))
#for s in S[1:] :
#    a = set(range(s[0], s[1] + 1))
#    ans = ans & a
#    if len(ans) == 0 :
#        break
#print(len(ans))

# Optimized solution
import collections
N, M = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(M)]
smax = max([s[0] for s in S])
smin = min([s[1] for s in S])
if smin - smax + 1 >= 0 :
    print((smin - smax + 1))
else :
    print((0))


#S2 = [set(range(s[0], s[1] + 1)) for s in S]
#S3 = set.intersection(*S2)
#print(len(S3))

#ans = set(range(S[0][0], S[0][1] + 1))
#for s in S[1:] :
#    a = set(range(s[0], s[1] + 1))
#    ans = ans & a
#    if len(ans) == 0 :
#        break
#print(len(ans))
# Original solution
n,m=list(map(int,input().split()))
l,r=list(map(int,input().split()))
lis=list(range(l,r+1))
ans=lis
for i in range(m-1):
    l,r=list(map(int,input().split()))
    lis_=list(range(l,r+1))
    ans=list(set(lis)&set(lis_))
print((len(ans)))

# Optimized solution
#不十分だった
n, m = list(map(int, input().split()))
l_max, r_min = 0, 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(r_min - l_max + 1, 0)))