CodeEff/ECCO huggingface dataset:
# Original solution
# coding: utf-8
import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
l_list = []
r_list = []
for i in range(M):
    l, r = lr()
    l_list.append(l)
    r_list.append(r)

ans = set()
for i in range(M):
    cards = sorted(l_list[i:] + r_list[:i + 1])
    cards = set(range(cards[0], cards[-1] + 1, 1))
    if (len(ans) == 0):
        ans = cards
        continue

    ans = cards & ans

print((len(ans)))


# Optimized solution
# coding: utf-8
import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
l_list = []
r_list = []
for i in range(M):
    l, r = lr()
    l_list.append(l)
    r_list.append(r)

# ans = set()
# left = l_list[M - 1]
# right = r_list[0]
tl_list = []
tr_list = []
for i in range(M):
    tmp_left = l_list[i]
    tmp_right = r_list[i]
    tl_list.append(tmp_left)
    tr_list.append(tmp_right)
left = max(tl_list)
right = min(tr_list)
# cards = sorted(l_list[i:] + r_list[:i + 1])
# cards = set(range(cards[0], cards[-1] + 1, 1))
# if (len(ans) == 0):
#     ans = cards
#     continue

# ans = cards & ans

ans = right - left + 1
if ans <= 0:
    print((0))
else:
    print(ans)
# Original solution
N,M = list(map(int,input().split()))
A = [list(map(int,input().split())) for i in range(M)]

l = -1
r = float("inf")

for x,y in A :
    if l < x :
        l = x
    if y < r :
        r = y
print((max(0,r-l+1)))


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

left = max(L)
right = min(R)

ans = max(0, right-left+1)

print(ans)
# Original solution
#<127>

N, M = list(map(int, input().split()))
gate = [0] * N
#rang = []
for m in range(M):
    L, R = list(map(int, input().split()))
    for x in range(L, R + 1):
        gate[x - 1] += 1
        #print(gate)

print((gate.count(M)))

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

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

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

max_left = 1
min_right = n

for i in range(m):
    left, right = list(map(int, input().split()))
    max_left = max(max_left, left)
    min_right = min(min_right, right)

print((max(min_right - max_left + 1, 0)))
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m = list(map(int, input().split()))
ans = None
for i in range(m):
    l, r = list(map(int, input().split()))
    card = set(range(l, r + 1))
    if ans is None:
        ans = card
    else:
        ans = ans.intersection(card)

print((len(ans)))


# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m = list(map(int, input().split()))
l_key = []
r_key = []
for i in range(m):
    l, r = list(map(int, input().split()))
    l_key.append(l)
    r_key.append(r)

print((max(0, min(r_key) - max(l_key) + 1)))
# 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(N):
  tmp = 0
  for L, R in LR:
    if L-1 <= i <= R-1:
      tmp += 1
    else:
      break
    if tmp == M:
      cnt += 1
      break
print(cnt)

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

Lma = 0
Rmi = N + 1
for L, R in LR:
  Lma = max(L, Lma)
  Rmi = min(R, Rmi)

print((max(0, Rmi - Lma + 1)))
# Original solution
n, m = list(map(int, input().split()))
ans = set(range(1, n + 1))
for _ in range(m):
    l, r = list(map(int, input().split()))
    st = set(range(l, r + 1))
    ans &= st
print((len(ans)))


# Optimized solution
def solve2():
    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)
    if L <= R:
        print((R - L + 1))
    else:
        print((0))
solve2()
# Original solution
n,m = list(map(int, input().split()))
ns = set(i+1 for i in range(n))
for i in range(m):
    l, r = list(map(int, input().split()))
    ns &= set(j for j in range(l, r+1))
print((len(ns)))

# Optimized solution
n,m = list(map(int, input().split()))
ns = set(i+1 for i in range(n))
l_min = 0
r_max = n
for i in range(m):
    l, r = list(map(int, input().split()))
    l_min = max(l_min, l)
    r_max = min(r_max, r)
print((max(r_max - l_min+1, 0)))
# Original solution
n, m = list(map(int, input().split()))
lr_list = list()
for _ in range(m):
    lr_list.append(list(map(int, input().split())))

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

max_l = 0
min_r = 1e6

for l, r in lr_list:
    # print(f"{card_list=}")
    # print(f"{max_l=}, {min_r=}")
    if l > max_l or r < min_r:
        max_l = l
        min_r = r
        new_card_list = []
        for card in card_list:
            if card >= max_l and card <= min_r:
                new_card_list.append(card)
        card_list = new_card_list


print((len(card_list)))

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

min_r = min(r_list)
max_l = max(l_list)
if max_l <= min_r:
    print((min_r - max_l + 1))
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))
li = {i+1 for i in range(N)}
# print(li)
box = []
for _ in range(M):
    l, r = list(map(int, input().split()))
    # print(l, r)
    for i in li:
        # print(i)
        if l <= i and i <= r:
            box.append(i)
    li = list(set(li) & set(box))
    box.clear()
    # print(li)
print((len(li)))


# Optimized solution
N, M = list(map(int, input().split()))
l, r = list(map(int, input().split()))
for _ in range(M-1):
    new_l, new_r = list(map(int, input().split()))
    if l < new_l:
        l = new_l
    if new_r < r:
        r = new_r

score = r-l+1
if score > 0:
    print(score)
else:
    print((0))
# Original solution
list_l = []
list_R = []
ID,gate = list(map(int,input().split()))
s = 0
t = 0
for i in range(gate):
  l,r = list(map(int,input().split()))
  list_l.append(l)
  list_R.append(r)

for i in range(ID+1):
  for j in range(gate):
    if list_l[j] <= i <= list_R[j]:
      s += 1
    else:
      None
  if s == gate:
    t += 1
  s = 0
print(t)


# Optimized solution
list_l = []
list_R = []
ID,gate = list(map(int,input().split()))
s = 0
for i in range(gate):
  l,r = list(map(int,input().split()))
  list_l.append(l)
  list_R.append(r)
a = max(list_l)
b = min(list_R)

for i in range(ID+1):
  if a <= i <= b:
    s += 1
  else:
    None
print(s)
# Original solution
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for _ in range(m)]
ans = list(range(lr[0][0], lr[0][1]+1))
for l, r in lr:
    # print(ans, set(range(l, r+1)))
    ans = list(set(ans) & set(range(l, r+1)))
print((len(ans)))


# Optimized solution
import sys
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for _ in range(m)]
min_gate = 0
max_gate = sys.maxsize
for l, r in lr:
    min_gate = max(min_gate, l)
    max_gate = min(max_gate, r)
print((max(0, max_gate - min_gate + 1)))
# 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_list.append(A)
    R_list.append(B)

counter = 0
for j in range(min(L_list),(max(R_list) + 1)):
    if j >= max(L_list) and j <= min(R_list):
        counter += 1

print(counter)

# 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_list.append(A)
    R_list.append(B)

if max(L_list) > min(R_list):
    print((0))
else:
    print((min(R_list) - max(L_list) + 1))
# Original solution
n,m=list(map(int,input().split()))
ll=[];rl=[];count=0;ans=0
count1=0;count2=0
for i in range(m):
    l,r=list(map(int,input().split()))
    ll.append(l);rl.append(r)
for o in range(n):
    count1+=1
    for i in range(m):

        if ll[i]<=o+1<=rl[i]:
            count2+=1
            
            if count2==m:
                ans+=1
    count2=0
print(ans)

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

for i in range(m):
    l,r=list(map(int,input().split()))
    ll.append(l,);rl.append(r)
    
tans=min(rl)-max(ll)+1
if tans>=0:print(tans)
else:print((0))
# Original solution
import math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(eval(input()))
def FLOAT(): return float(eval(input()))
MOD = 10**9+7

n,m = MAP()
l,r = MAP()
res = set(range(l,r+1))
for i in range(m-1):
    l,r = MAP()
    res = res & set(range(l,r+1))

print((len(res)))

# Optimized solution
import math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(eval(input()))
def FLOAT(): return float(eval(input()))
MOD = 10**9+7

n,m = MAP()
l,r = [], []
for i in range(m):
    a,b = MAP()
    l.append(a)
    r.append(b)

max_l = max(l)
min_r = min(r)
if min_r<max_l:
    print((0))
    exit()
print((min_r-max_l+1))
# Original solution
k,m=list(map(int,input().split()))
l=[0 for i in range(k+1)]
for i in range(m):
  a,s=list(map(int,input().split()))
  l[a-1]+=1;l[s]-=1
ans=0
f=0
for j in range(k):
  l[j]+=f
  f=l[j]
print((len([1 for i in l if i==m])))

# Optimized solution
n,m=list(map(int,input().split()))
table=[0]*(n+2)
for i in range(m):
    l,r=list(map(int,input().split()))
    table[l]+=1
    table[r+1]-=1
from itertools import accumulate
from collections import Counter as co
var=co((accumulate(table)))

if m not in var:print((0))
else:print((var[m]))
# Original solution
n,m = list(map(int, input().split()))
gatelist = [0] * n
for i in range(m):
    l,r = list(map(int, input().split()))
    for j in range(l-1,r):
        gatelist[j] += 1
cnt = 0
for item in gatelist:
    if item == m:
        cnt +=1
print(cnt)

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

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

card2canopen_dict = {i: True for i in range(1, n+1)}

for n_card in range(1, n+1):
    for i, lr in enumerate(lr_list):
        target_l = lr[0]
        target_r = lr[1]
        if card2canopen_dict[n_card] and target_l <= n_card <= target_r:
            pass
        else:
            card2canopen_dict[n_card] = False
            break

card_list = [key for key, value in list(card2canopen_dict.items()) if value]

ans = len(card_list)
print(ans)


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

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

card2canopen_dict = {i: True for i in range(1, n+1)}

# 重ね合わせの区間を調べれば良い気がする
kukan_min, kukan_max = 1, n

for i, lr in enumerate(lr_list):
    l, r = lr[0], lr[1]
    if l >= kukan_min:
        kukan_min = l
    if r <= kukan_max:
        kukan_max = r

if kukan_max < kukan_min:
    ans = 0
else:
    ans = len(list(range(kukan_min, kukan_max+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)
count=0
for i in range(1,N+1):
  if max(L)<=i<=min(R):
    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)
count=0
if max(L)>min(R):
  print((0))
else:
  print((min(R)-max(L)+1))
# Original solution
import itertools
from collections import Counter


def main():
    N, M = list(map(int, input().split()))
    LR = []
    for _ in range(M):
        L, R = list(map(int, input().split()))
        LR.append(list(range(L, R + 1)))
    LR = list(itertools.chain.from_iterable(LR))
    C = Counter(LR)

    ans = 0
    for c in list(C.values()):
        if c == M:
            ans += 1
    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    N, M = list(map(int, input().split()))
    L = 0
    R = N
    for _ in range(M):
        l, r = list(map(int, input().split()))
        if L < l:
            L = l
        if R > r:
            R = r
    ans = 0
    if L <= R:
        ans = R - L + 1
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
import sys
def input():
    return sys.stdin.readline()[:-1]


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

cards = [0] * (N + 1)

for i in range(M):
    L, R = list(map(int, input().split()))

    for j in range(L, R + 1):
        cards[j] += 1

print((cards.count(M)))


# Optimized solution
import sys
def input():
    return sys.stdin.readline()[:-1]


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

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(r_min - l_max + 1, 0)))
# Original solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))
tmpa = list(map(int, list(range(l, r+1))))
for i in range(m-1):
    l, r = list(map(int, input().split()))
    ret = []
    for j in range(l, r+1):
        if j in tmpa:
            ret.append(j)

    tmpa = ret.copy()

print((len(tmpa)))

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

print((max(min(rs)-max(ls)+1, 0)))
# Original solution
# -*- coding: utf-8 -*-
n, m = list(map(int, input().split()))

pass_id = [0 for i in range(n)]

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

# print(pass_id)
print((pass_id.count(m)))

# Optimized solution
# -*- coding: utf-8 -*-
n, m = list(map(int, input().split()))

head = 1
tail = n

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

if head > tail:
    print((0))
else:
    print((tail - head + 1))
# Original solution
N, M = [int(n) for n in input().split()]

check_list = [0] * N

for i in range(M):
    l, r = [int(n) for n in input().split()]
    for j in range(l-1, r):
        check_list[j] += 1

print((check_list.count(M)))

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

l, r = 1, 10**5
for i in range(M):
    l_, r_ = [int(n) for n in input().split()]
    l = max(l, l_)
    r = min(r, r_)

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

p = [0] * (10**5)

for y in gate:
  p[y[0]-1:y[1]] = list([x+1 for x in p[y[0]-1:y[1]]])
  
cnt = 0

  
for z in range(n):
  if p[z] == m:
    cnt += 1
    
print(cnt)

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

minv = gate[0][0]
maxv = gate[0][1]

for x in gate:
  minv = max(minv,x[0])
  maxv = min(maxv,x[1])
  
print((maxv-minv+1 if maxv-minv+1 >= 1 else 0))
# Original solution
import functools 

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

cards = []
for i in range(0, M):
  L, R = list(map(int, input().split()))
  card = [j for j in range(L, R+1)]
  cards.append(card)

found = functools.reduce(lambda x, y: list(set(x) & set(y)), cards)
print((len(found)))

# Optimized solution
N, M = list(map(int, input().split()))
L, R = list(map(int, input().split()))
c = [j for j in range(L, R+1)]

for i in range(0, M-1):
  L, R = list(map(int, input().split()))
  n = [j for j in range(L, R+1)]
  c = list(set(c) & set(n))
  if len(c) == 0:
    break

print((len(c)))
# Original solution
import sys 
input = sys.stdin.readline

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

ok = [i for i in range(n+1) if L<=i and i<=R]
ok2 = []

for i in range(m-1):
  l, r = list(map(int,input().split()))
  for j in ok:
    if not(j < l or r < j):
      ok2.append(j)
  ok = ok2
  ok2 = []
      
print((len(ok)))

# Optimized solution
import sys 
input = sys.stdin.readline

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

for i in range(m):
  l, r = list(map(int,input().split()))
  L.append(l)
  R.append(r)
  
if min(R)-max(L) >= 0:
  print((min(R)-max(L)+1))
else:
  print((0))
# Original solution
N, M = list(map(int,input().split()))
mat = [list(map(int,input().split())) for o in range(M)]
ans = set(j for j in range(mat[0][0], mat[0][-1] + 1))
for i in range(1, M):
    s = set(j for j in range(mat[i][0], mat[i][-1] + 1))
    ans = ans & s
print((len(ans)))


# Optimized solution
N, M = list(map(int,input().split()))
mat = [list(map(int,input().split())) for o in range(M)]
lmax = mat[0][0]
for i in range(M):
    if mat[i][0] > lmax:
        lmax = mat[i][0]
rmin = mat[0][1]
for i in range(M):
    if mat[i][1] < rmin:
        rmin = mat[i][1]

ans = rmin - lmax + 1
if ans >= 1:
    print(ans)
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))
t_list = [[0] * 2 for _ in range(M)]
for i in range(M):
  t_list[i] = list(map(int, input().split()))
ans = 0
a = [j for j in range(t_list[0][0], t_list[0][1]+1)]
for i in range(M):
  b = [j for j in range(t_list[i][0], t_list[i][1]+1)]
  a = list(set(a) & set(b))
ans = len(set(a))
print(ans)

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

#print(N,M)

okList = [1] * N

#print(okList)
#print(type(okList))

for i in range(M):
    L, R = list(map(int, input().split()))
    L -= 1
    if 0 < L:
        okList[0:L] = [0] * L
        #print(okList)

    if R < len(okList):
        okList[R:len(okList)] = [0] * (len(okList) - R)
        #print(okList)

print((sum(okList)))

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

for i in range(M):
    L, R = list(map(int, input().split()))
    if maxL < L:
        maxL = L
    if minR != None and R < minR:
        minR = R
    #print("\t\t\t", maxL, minR)

if maxL <= minR:
    print((minR - maxL + 1))
else:
    print((0))
# Original solution
if __name__ == '__main__':
    N, M = list(map(int, input().split()))

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

    LR1 = LR[0]
    L1 = LR1[0]
    R1 = LR1[1]

    my_list = list(range(L1, R1+1))

    for tmp_LR in LR[1::]:
        tmp_L = tmp_LR[0]
        tmp_R = tmp_LR[1]

        my_list = [e for e in my_list if e >= tmp_L and e <= tmp_R]

    answer = len(my_list)
    print(answer)


# Optimized solution
if __name__ == '__main__':
    N, M = list(map(int, input().split()))

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

    LR1 = LR[0]
    maxL = LR1[0]
    minR = LR1[1]

    for tmp_LR in LR[1::]:
        tmp_L = tmp_LR[0]
        tmp_R = tmp_LR[1]

        if tmp_L >= maxL:
            maxL = tmp_L

        if tmp_R <= minR:
            minR = tmp_R

    my_list = list(range(maxL, minR + 1))

    answer = len(my_list)
    print(answer)
# Original solution
n,m = list(map(int,input().split()))
l = []
r = []
count = 0
def check(a,b,c):
    if a >= max(b) and a <= min(c):
        return 1
    else:
        return 0
for i in range(m):
    l0,r0 = list(map(int,input().split()))
    l.append(l0)
    r.append(r0)
for y in range(n+1):
    count = count + check(y,l,r)
print(count)

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

# Optimized solution
n,m=list(map(int,input().split()))
left=0
right=n
for i in range(m):
    l,r=list(map(int,input().split()))
    left=max(l,left)
    right=min(r,right)
print((max(0,right-left+1)))
# Original solution
N, M = list(map(int, input().split()))
MC = []
for i in range(M):
    L, R = list(map(int, input().split()))
    tmp = [0] * N
    for j in range(L-1, R):
        tmp[j] = 1
    MC.append(tmp)
cnt = 0
for i in range(N):
    mc = True
    for j in range(M):
        if not MC[j][i]:
            mc = False
            break
    if mc:
        cnt += 1
print(cnt)

# Optimized solution
N, M = list(map(int, input().split()))
MC = [1] * N
for i in range(M):
    L, R = list(map(int, input().split()))
    for j in range(N):
        if not L-1 <= j < R:
            MC[j] = 0
print((MC.count(1)))
# Original solution
def main():
    ini_inp=list([int(x) for x in input().split()])
    N=ini_inp[0]
    M=ini_inp[1]

    L=[]
    R=[]
    for i in range(M):
        sec_inp=list([int(x) for x in input().split()])
        L.append(sec_inp[0])
        R.append(sec_inp[1])

    temp=[]
    for i in range(M):
        if i==0:
           temp=set([i for i in range(L[i],R[i]+1)])
        number=set([i for i in range(L[i],R[i]+1)])
        temp=number & temp
        if len(temp)==0:
            break
    print((len(temp)))

if __name__=="__main__":
    main()
        


# Optimized solution
def main():
    ini_inp=list([int(x) for x in input().split()])
    N=ini_inp[0]
    M=ini_inp[1]

    L=[]
    R=[]
    for i in range(M):
        sec_inp=list([int(x) for x in input().split()])
        L.append(sec_inp[0])
        R.append(sec_inp[1])

    low=L[0]
    up=R[0]
    flag=0
    for i in range(1,M):
        if L[i]>up:
            flag=1
            break
        elif L[i]>=low:
            low=L[i]
        elif R[i]<=up:
            up=R[i]

    if flag==0:
        print((up-low+1))
    else:
        print((0))

if __name__=="__main__":
    main()
# Original solution
n, m = list(map(int, input().split()))
card = [0] * n
l = []
r = []

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

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


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


# Optimized solution
from itertools import accumulate

n, m = list(map(int, input().split()))
card = [0] * (n + 1)

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

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