CodeEff/ECCO huggingface dataset:
# Original solution
n,m = list(map(int, input().split()))
l, r = [], []
c = 0

a = []
for _ in range(n):
    a.append(0)

for _ in range(m):
    l,r = list(map(int, input().split()))
    for i in range(l-1, r):
        a[i] += 1
        
for i in range(n):
    if a[i] == m:
        c += 1

print(c)


# Optimized solution
n,m = list(map(int, input().split()))
l, r = [], []
x = 0
c = 0

a = []
for _ in range(n):
    a.append(0)

for _ in range(m):
    l,r = list(map(int, input().split()))
    a[l-1] += 1
    if r != n:
        a[r] -= 1

for i in range(n):
    x += a[i]
    if(x == m):
        c += 1

print(c)
# Original solution
n,m = list(map(int,input().split()))
l = [0]*m
r = [0]*m
ans = 0
for i in range(m):
    l[i],r[i] = list(map(int,input().split()))

for i in range(n):

    for j in range(m):
        
        if  not l[j]<=i+1<=r[j]:
            break
        if j==m-1:
            ans += 1
            
print(ans)

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

Min = max(l)
Max = min(r)
for i in range(n):
    if Min<=i+1<=Max:
        ans += 1
print(ans)
# Original solution
n, m = list(map(int, input().split(" ")))
cards = [0] * n

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

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

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

s, e = 1, n
for i in range(m):
    l, r = list(map(int, input().split(" ")))
    if s < l: s = l
    if e > r: e = r

print((e-s+1 if s <= e else 0))
# Original solution
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n,m =na()
ls = [i for i in range(1,n+1)]
for i in range(m):
    l, r = na()
    tmp = [j for j in range(l, r+1)]
    ls = list(set(ls) & set(tmp))

print((len(ls)))


# Optimized solution
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n,m =na()
ls = 1
rs = n
for i in range(m):
    l, r = na()
    ls = max(ls, l)
    rs = min(rs, r)
if ls > rs:
    print((0))
else:
    print((rs - ls + 1))
# Original solution
n,m= list(map(int,input().split()))
for i in range(m):
    a,b = list(map(int,input().split()))
    if i == 0:
        l = [a+j for j in range(b-a+1)]
    l = list(set([a+j for j in range(b-a+1)]) & set(l))
print((len(set(l))))

# Optimized solution
n,m = list(map(int, input().split()))
left = 0
right = 10**5
for _ in range(m):
    l,r = list(map(int,input().split()))
    left = max(left, l)
    right = min(right,r)
print((max(0,right-left+1)))
# Original solution
N, M = (int(x) for x in input().split())
L, R = [None]*M, [None]*M
for i in range(M):
    L[i], R[i] = (int(x) for x in input().split())

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

print((len(tmp)))


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

tmp = [L[0], R[0]]
for j in range(1, M):
    if tmp[0] < L[j]:
        tmp[0] = L[j]
    if R[j] < tmp[1]:
        tmp[1] = R[j]
    if tmp[0] > tmp[1]:
        print((0))
        exit()

print((tmp[1]-tmp[0]+1))
# Original solution
n, m = list(map(int, input().split(' ')))
s = []
for i in range(m):
    l, r = list(map(int, input().split(' ')))
    a = set(j for j in range(l, r+1))
    s.append(a)
result = s[0]
for i in range(len(s)-1):
    result = result & s[i+1]
print((len(list(result))))

# Optimized solution
n, m = list(map(int, input().split(' ')))
for i in range(m):
    l, r = list(map(int, input().split(' ')))
    a = set(j for j in range(l, r+1))
    if i == 0:
        result = a
    else:
        result = result & a
print((len(list(result))))
# Original solution
n, m = list(map(int, input().split(' ')))
for i in range(m):
    l, r = list(map(int, input().split(' ')))
    a = set(j for j in range(l, r+1))
    if i == 0:
        result = a
    else:
        result = result & a
print((len(list(result))))

# Optimized solution
n, m = list(map(int, input().split(' ')))
l_ = []
r_ = []
for i in range(m):
    l,r = list(map(int, input().split(' ')))
    l_ += [l]
    r_ += [r]
l = max(l_)
r = min(r_)
if n < l:
    print((0))
elif n < r:
    r = n
    print((r - l + 1))
elif l > r:
    print((0))
elif l == r:
    print((1))
else:
    print((r - l + 1))
# Original solution
n, m = (int(i) for i in input().split())
ok = [1] * n
for i in range(1,m+1):
  tmp_l, tmp_r = (int(j) for j in input().split())
  filter_array = [1 if tmp_l<=k<=tmp_r else 0 for k in range(1,n+1)]
  ok = [x*y for (x,y) in zip(ok, filter_array)]
print((sum(ok)))

# Optimized solution
n, m = (int(i) for i in input().split())
ok = [1] * n
l, r = (1,n)
for i in range(1,m+1):
  tmp_l, tmp_r = (int(j) for j in input().split())
  if l < tmp_l:
    l = tmp_l
  if r > tmp_r:
    r = tmp_r

print(( r-l+1 if r-l+1>0 else 0 ))
# Original solution
n,m = [int(i) for i in input().split()]

num_set = {i for i in range(n+1)}
for i in range(m):
    s,e = [int(i) for i in input().split()]
    card_set = {i for i in range(s,e+1)}
    num_set = num_set & card_set
    
print((len(num_set)))

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

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

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

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

through_list = []
for i in range(M):
    L,R = list(map(int,input().split()))
    through_list.append([L,R])
through_list.sort()
ans_range = through_list[0]
for i in range(M-1):
    ans_range = [max(ans_range[0],through_list[i+1][0]),min(ans_range[1],through_list[i+1][1])]
ans = ans_range[1]-ans_range[0]+1 
ans = max(0,ans)
print(ans)
# Original solution
n,m=list(map(int,input().split()))
id=[0]*100010
for i in range(m):
    ll,rr=input().split()
    for j in range(int(ll),int(rr)+1):
        id[j]+=1

cnt=0
for i in range(1,n+1):
    if id[i]==m:
        cnt+=1
print(cnt)

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

for i in range(m):
  l, r  = list(map(int, input().split()))
  y = list(range(l, r+1))
  x = list(set(x) & set(y))

print((len(x)))

# Optimized solution
n, m = list(map(int, input().split()))
ls = [0]*m
rs = [0]*m

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

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


# Optimized solution
n, m = list(map(int, input().split()))
l_max = 0
r_min = 10**6
for i in range(m):
    l, r = list(map(int, input().split()))
    l_max = max(l_max, l)
    r_min = min(r_min, r)

if l_max > r_min:
    print((0))
else:
    print((r_min-l_max+1))
# Original solution
n, m = list(map(int, input().split()))
lr = [tuple(map(int, input().split())) for _ in range(m)]

l, r = lr[0]
ans = set()
for k in range(l, r+1):
        ans.add(k)

for i, j in lr[1:]:
    ID = set()
    for k in range(i, j+1):
        ID.add(k)
    ans = ans & ID
print((len(ans)))


# Optimized solution
n, m = list(map(int, input().split()))
lr = [tuple(map(int, input().split())) for _ in range(m)]
l = 1
r = n
for i, j in lr:
    l = max(i, l)
    r = min(j, r)
print((max(r-l+1, 0)))
# Original solution
# ABC 127 c
n, m = [int(el) for el in input().split(' ')]
for i in range(m):
    l, r = [int(el) for el in input().split(' ')]
    if i == 0:
        cards = set(range(l, r + 1))
    else:
        newcards = set(range(l, r + 1))
        cards = cards.intersection(newcards)
print((len(cards)))


# Optimized solution
# ABC 127 c
n, m = [int(el) for el in input().split(' ')]
max_l = 0
min_r = n
for i in range(m):
    l, r = [int(el) for el in input().split(' ')]
    max_l = max(max_l, l)
    min_r = min(min_r, r)

print((max(min_r - max_l + 1, 0)))
# Original solution
N,M= list(map(int,input().split()))
list_A = [0] * N
for i in range(M):
    A,B = list(map(int,input().split()))
    list_A[A-1:B] = [x+1 for x in list_A[A-1:B]]
print((list_A.count(M)))


# Optimized solution
N,M= list(map(int,input().split()))
end = []
start = []
for i in range(M):
    A,B = list(map(int,input().split()))
    end.append(B)
    start.append(A)
end_min = min(end)
start_max = max(start)
if start_max > end_min:
    print((0))
else:
    print((end_min-start_max+1))
# Original solution
n,m = list(map(int, input().split()))

a = [0]*m
b = [0]*m
for i in range(m):
  l,r = list(map(int, input().split()))
  a[i] = l
  b[i] = r
  
s = 0
for i in range(a[m-1], b[m-1]+1):
  t = 0
  for j in range(m):
    if a[j] <= i and i <= b[j]:
      t += 1
    else:
      break
  if t == m:
    s += 1
print(s)

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

a = [0]*m
b = [0]*m
for i in range(m):
  l,r = list(map(int, input().split()))
  a[i] = l
  b[i] = r

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

nums = list(range(1,n+1))
ans = 0
for i in nums:
  for j in lr:
    if i < j[0] or i > j[1]:
      break
  else:
    ans += 1
print(ans)

# Optimized solution
n,m = list(map(int, input().split()))
lr = [0, 10**6]
for _ in range(m):
  t = sorted(list(map(int, input().split())))
  lr[0] = max(lr[0], t[0])
  lr[1] = min(lr[1], t[1])

ans = lr[1] - lr[0] + 1
if ans < 0:
  print((0))
else:
  print(ans)
# Original solution
n,m = list(map(int, input().split()))
lr = [0, 10**6]
for _ in range(m):
  t = sorted(list(map(int, input().split())))
  lr[0] = max(lr[0], t[0])
  lr[1] = min(lr[1], t[1])

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

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

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

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

if y-x >= 0: print((y-x+1))
else: print((0))
# Original solution
def solve():
    import sys
    n, m, *LR = list(map(int, sys.stdin.read().split()))
    L = LR[::2]
    R = LR[1::2]
    res = set(range(1, n + 1))
    for l, r in zip(L, R):
        x = set(range(l, r + 1))
        res &= x
    print((len(res)))


solve()


# Optimized solution
def solve():
    import sys
    n, m, *LR = list(map(int, sys.stdin.read().split()))
    L = LR[::2]
    R = LR[1::2]
    l = max(L)
    r = min(R)
    print((max(0, r - l + 1)))


solve()
# Original solution
n,m = list(map(int, input().split()))
gates = []
for i in range(m):
    l,r = list(map(int, input().split()))
    gates.append((l-1,r-1))

checker = [0 for i in range(n)]
for i in gates:
    for j in range(i[0],i[1]+1):
        checker[j] += 1

ans = 0
for i in checker:
    if i == m:
        ans += 1

print(ans)


# Optimized solution
n,m = list(map(int, input().split()))
l,r = 0,n
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
import sys
N, M = list(map(int, sys.stdin.readline().split()))
LR = [list(map(int,sys.stdin.readline().split())) for i in range(M)]
B = list(range(0, 10 ** 5 + 1))
for i in LR:
	A = list(range(i[0], i[1] + 1))
	B = set(A) & set(B)
print((len(B)))

# Optimized solution
import sys
N, M = list(map(int, sys.stdin.readline().split()))
LR = sorted([list(map(int,sys.stdin.readline().split())) for i in range(M)])
L = 1
R = 10 ** 5
for i in LR:
    L = max(L, i[0])
    R = min(R, i[1])
if R - L >= 0:
    print((R - L + 1))
else:
    print((0))
# Original solution
#!/usr/bin/env python3

import math

n,m = list(map(int,input().split(" ")))

idl = []
idr = []

for i in range(m):
    a,b = list(map(int,input().split(" ")))
    idl.append(a)
    idr.append(b)

ans  = 0

for i in range(n):
    flg = 0
    for j in range(m):
        if idl[j] <= i+1 <= idr[j]:
            pass
        else:
            flg = 1
    if flg == 0:
        ans += 1
print(ans)

# Optimized solution
#!/usr/bin/env python3

import math

n,m = list(map(int,input().split(" ")))

s = 0
t = 10**5+1
for i in range(m):
    a,b = list(map(int,input().split(" ")))
    if s < a:
        s = a
    if b < t:
        t = b
if t < s:
    print((0))
else:
    print((t-s+1))
# Original solution
N, M = list(map(int, input().split()))
keys = set()
for i in range(1, N+1):
    keys.add(i)
for _ in range(M):
    akey = set()
    l, r = list(map(int, input().split()))
    for i in range(l, r+1):
        akey.add(i)
    keys = keys & akey
print((len(keys)))


# Optimized solution
N, M = list(map(int, input().split()))
L, R = list(map(int, input().split()))
if M == 1:
    print((R - L + 1))
    exit()
for _ in range(M-1):
    l, r = list(map(int, input().split()))
    if l <= R and L <= r:
        L = max(L, l)
        R = min(R, r)
    else:
        print((0))
        exit()
print((R - L + 1))
# Original solution
n, m = list(map(int,input().split(" ")))

l, r = list(map(int,input().split(" ")))
s = set(range(l, r+1))

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

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

l_max, r_min = list(map(int,input().split(" ")))

for _ in range(1,m):
    l, r = list(map(int,input().split(" ")))
    
    if l >= l_max:
        l_max = l
        
    if r <= r_min:
        r_min = r


        
print((len(list(range(l_max, r_min+1)))))
# Original solution
def main():
    n, m = list(map(int, input().split()))

    card_list = [0] * n

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

    print((card_list.count(m)))

if __name__ == '__main__':
    main()

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

    card_list = [0] * n

    l_list = []
    r_list = []

    for _ in range(m):
        l, r = list(map(int, input().split()))
        l_list.append(l)
        r_list.append(r)

    max_l = max(l_list)
    min_r = min(r_list)

    print((min_r-max_l+1 if min_r >= max_l else 0))

if __name__ == '__main__':
    main()
# Original solution
import collections
n,m = list(map(int,input().split()))
card = [list(map(int,input().split())) for c in range(m)]
nums = []
for index,c in enumerate(card):
  nums += [i for i in range(c[0],c[1]+1)]
ans = 0
for k,v in list(collections.Counter(nums).items()):
  if v == m:
    ans += 1
print(ans)



# Optimized solution
N,M = list(map(int,input().split()))
L = []
R = []
 
for m in range(M):
  l,r = list(map(int,input().split()))
  L+=[l]
  R+=[r]
 
print((max(0,min(R)-max(L)+1)))
# Original solution
N, M = list(map(int, input().split()))
L = []
R = []
ans = 0
for _ in range(M):
    inp = input().split()
    L.append(int(inp[0]))
    R.append(int(inp[1]))
for k in range(1,N + 1):
    for i,j in zip(L,R):
        if i <= k and k <= j:
            continue
        else:
            break
    else:
        ans += 1
print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
L = []
R = []
ans = 0
for _ in range(M):
    inp = input().split()
    L.append(int(inp[0]))
    R.append(int(inp[1]))
L_max = max(L)
R_min = min(R)
for i in range(L_max,min(R_min,N) + 1):
    ans  += 1
print(ans)
# 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)

if max(left)>min(right):
    print((0))
else:
    print((min(right)-max(left)+1))

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

ans=min(ri)-max(left)+1
if ans<=0:
    ans=0
print(ans)
# Original solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))
key = set([l + i for i in range(r - l + 1)])
for i in range(m - 1):
    l, r = list(map(int, input().split()))
    key &= set([l + i for i in range(r - l + 1)])
print((len(key)))

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

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

ans = 0
for i in range(max,min+1):
    for j in range(m):
        if not((i) >= l[j] and (i) <= r[j]):
            break
        if j == m - 1:
            ans += 1

print(ans)

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

lmax = 0
rmin = 10 ** 5
for i in range(m):
    lmax = max(lmax,l[i])
    rmin = min(rmin,r[i])

if lmax <= rmin:
    print((rmin - lmax + 1))
else:
    print("0")
# Original solution
#C - Prison(ABC127)
N,M=list(map(int, input().split()))
l,r=list(map(int, input().split()))
for i in range(M-1):
    L,R=list(map(int,input().split()))
    l=max(L,l)
    r=min(r,R)
if r-l+1>0:
    print((r-l+1))
else:
    print((0))

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

s=[0]*n

for i in range(m):
    l,r=list(map(int,input().split()))
    x=l
    while x<=r:
        s[x-1]+=1
        x+=1
        
ans=[str(i) for i in s] 
print((ans.count(str(m))))

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

s=[0]*n
left=[]
right=[]
for i in range(m):
    l,r=list(map(int,input().split()))
    left.append(l)
    right.append(r)
    
if min(right)<max(left):
    print((0))
else:
    print((min(right)-max(left)+1))
# Original solution

N, M=list(map(int, input().split()))  #複数数値入力　「A B」みたいなスペース空いた入力のとき
LR=[[int(i) for i in input().split()] for i in range(M)]  # 二次元配列入力　二次元マップみたいな入力のとき

ret = set(range(LR[0][0], LR[0][1]+1))

for i in LR[1:]:
    ret = set(range(i[0], i[1]+1)) & ret

print((len(ret)))
    

# Optimized solution

N, M=list(map(int, input().split()))  #複数数値入力　「A B」みたいなスペース空いた入力のとき
LR=[[int(i) for i in input().split()] for i in range(M)]  # 二次元配列入力　二次元マップみたいな入力のとき

mi, ma = 0, 10**5

for i in LR:
    mi = max(mi, i[0])
    ma = min(ma, i[1])

if ma-mi < 0:
    print((0))
else:
    print((ma-mi + 1))
# Original solution
n,m = list(map(int,input().split()))
l = []
for i in range(m):
    l.append(list(map(int,input().split())))
ls = set(range(1,n+1))
for i in l:
    ls = ls & set(range(i[0],i[1]+1))
print((len(ls)))


# Optimized solution
n,m = list(map(int,input().split()))
l = []
for i in range(m):
    l.append(list(map(int,input().split())))
ans = 0
lmax = 0
rmin = float('inf')
for i in l:
    if i[0]>lmax:
        lmax = i[0]
    if i[1]<rmin:
        rmin = i[1]
if lmax <= rmin:
    ans = rmin - lmax + 1
print(ans)
# Original solution
n,m = list(map(int,input().split()))
li1 = []
for i in range(m):
    a,b = list(map(int,input().split()))
    li2 = []
    for j in range(a,b+1):
        li2.append(j)
    li1.append(set(li2))
for i in range(m):
    li1[0] = li1[0] & li1[i]
print((len(li1[0])))

# Optimized solution
n,m = list(map(int,input().split()))
li,lia = [],[]
for i in range(m):
    a,b = list(map(int,input().split()))
    li.append(a)
    lia.append(b)
if min(lia)-max(li)+1 <= 0:
    print((0))
else:
    print((min(lia)-max(li)+1))
# Original solution
from itertools import islice

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


lst = []
for i in LR:
    lst_ = set(list(range(i[0],i[1]+1)))
    lst.append(lst_)

lst = set.intersection(set(lst[0]), *islice(lst, 1, None))
print((len(lst)))


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

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

if L0 <= R0:
    print((R0-L0+1))
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))
flag = [0] * N
for _ in range(M):
    L, R = list(map(int, input().split()))
    L -= 1
    R -= 1
    for i in range(L, R+1):
        flag[i] += 1
count = 0
for i in range(N):
    if flag[i] == M:
        count += 1
print(count)

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

# Optimized solution
N, M = list(map(int, input().split()))
max_L, min_R = 1, N
for i in range(M):
    L, R = list(map(int, input().split()))
    if L > max_L:
        max_L = L
    if R < min_R:
        min_R = R
ans = min_R - max_L + 1
if ans < 0:
    ans = 0
print(ans)