CodeEff/ECCO huggingface dataset:
# Original solution
N, M = list(map(int, input().split()))
for i in range(M):
    if i==0:
        L, R = list(map(int, input().split()))
        s = set(range(L,R+1))
        S = s
    else:
        L, R = list(map(int, input().split()))
        s = set(range(L,R+1))
        S = S & s
print((len(S)))

# Optimized solution
N, M = list(map(int, input().split()))
f = 0
for i in range(M):
    if i==0:
        L1, R1 = list(map(int, input().split()))
    else:
        L2, R2 = list(map(int, input().split()))
        if R1 < L2 or L1 > R2:
            print((0))
            f+=1
            break
        elif L2<L1 and R1<R2:
            continue
        elif L1<L2 and R2<R1:
            L1 = L2
            R1 = R2
        elif L2<L1 and R2<R1:
            R1 = R2
        elif L1<L2 and R1<R2:
            L1 = L2
        else:
            continue
s = set(range(L1,R1+1))
if f!=1:
    print((len(s)))
# Original solution
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    result = set([i for i in range(int(1e5 + 1))])
    # print(result)
    for i in range(M):
        l_to_r = {j for j in range(L[i], R[i] + 1)}
        result = result & l_to_r
        # print(result)
    print((len(result)))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)


if __name__ == "__main__":
    main()


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


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    L = max(L)
    R = min(R)

    if L <= R:
        print((R - L + 1))
    elif L > R:
        print((0))

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)


if __name__ == "__main__":
    main()
# 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 &= set(range(l, r+1))

print((len(s)))

# Optimized solution
n, m = list(map(int, input().split()))
l, r = 1, n
for i in range(m):
    li, ri = list(map(int, input().split()))
    if ri<l or r<li:
        print((0))
        exit()
    if l<=li and ri<=r:
        l, r = li, ri
    elif l<=ri<=r:
        r = ri
    elif l<=li<=r:
        l = li
    else:
        continue

print((r-l+1))
# Original solution
n, m = list(map(int, input().split()))

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

cnt = 0
for i in range(1, n+1):
    for l, r in lr:
        if not l <= i <= r:
            break
    else:
        cnt += 1

print(cnt)

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

if max_l <= min_r:
    print((min_r - max_l + 1))
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))

count = 0
ans=[]
for i in range(M):
    l, r = list(map(int, input().split()))
    if count == 0:
        for j in range(l, r+1):
            ans.append(j)
    else:
        tmp = []
        for j in range(l, r+1):
            tmp.append(j)
        ans = list(set(tmp) & set(ans))
    count += 1

print((len(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)

maxl = max(L)
minr = min(R)

ans = minr - maxl + 1
if ans > 0:
    print(ans)
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))
L = [0] * M
R = [0] * M
LR = []
for i in range(M):
  L, R = list(map(int, input().split()))
  LR.append(list(range(L, R+1)))
ans = set(LR[0])
for i in LR:
  ans = ans & set(i)
print((len(ans)))

# Optimized solution
N, M = list(map(int, input().split()))
L = [0] * M
R = [0] * M
L, R = list(map(int, input().split()))
ans = set(list(range(L, R+1)))
for i in range(M-1):
  L, R = list(map(int, input().split()))
  ans = ans & set(list(range(L, R+1)))
print((len(ans)))
# Original solution
N, M = list(map(int, input().split()))
L = [0] * M
R = [0] * M
L, R = list(map(int, input().split()))
ans = set(list(range(L, R+1)))
for i in range(M-1):
  L, R = list(map(int, input().split()))
  ans = ans & set(list(range(L, R+1)))
print((len(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)
ans = list(range(max(L),min(R)+1))
print((len(ans)))
# Original solution
N,M=list(map(int,input().split()))
L=[0]
R=[0]
ans=0

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

for i in range(1,N+1):
    flg=1
    for j in range(1,M+1):
        if i >= L[j] and i <= R[j]:
            continue
        flg=0

    if flg==1:
        ans+=1
print(ans)


# Optimized solution
N,M=list(map(int,input().split()))
Lm=0
Rm=N
ans=0

for i in range(M):
    Lt,Rt=list(map(int,input().split()))
    if Lm < Lt:
        Lm = Lt
    if Rm > Rt:
        Rm = Rt
if Rm < Lm:
    print((0))
else:
    print((Rm-Lm+1))
# Original solution
# C - Prison

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

print((len(ls)))


# Optimized solution
# C - Prison

N, M = list(map(int, input().split()))
left = 0
right = 10 ** 5
for _ in range(M):
    l, r = list(map(int, input().split()))
    left, right = max(left, l), min(right, r)

print((len([i for i in range(left, right + 1)]) if left <= right else 0))
# Original solution
n, m = [int(x) for x in input().split(' ')]
l, r = [int(x) for x in input().split(' ')]
res = set(range(l, r+1))

for i in range(int(m-1)):
    l, r = [int(x) for x in input().split(' ')]
    res &= set(range(l, r+1))

print((len(res)))


# Optimized solution
n, m = [int(x) for x in input().split(' ')]
lr = [(int(l), int(r)) for l, r in [input().split(' ') for _ in range(m)]]

l = max([x[0] for x in lr])
r = min([x[1] for x in lr])

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

# Optimized solution
N,M=list(map(int,input().split()))
l=0
r=float("Inf")
for i in range(M):
    L,R=list(map(int,input().split()))
    l=max(l,L)
    r=min(r,R)
print((max(r-l+1,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
for i in range(1,N+1):
  flag=0
  for j in range(M):
    if i<l[j] or i>r[j]:
      flag=1
  if flag==0:
    a+=1
print(a)   

# Optimized solution
n,m=list(map(int,input().split()))
x=1;y=n
for _ in range(m):
  l,r=list(map(int,input().split()))
  if x<l:
    x=l
  if y>r:
    y=r
print((max(y-x+1,0)))
# Original solution
n,m = list(map(int,input().split()))

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

for i in range(m-1):
    tmp_l,tmp_r = list(map(int,input().split()))
    tmp = set(range(tmp_l,tmp_r+1))
    ans = ans&tmp

print((len(ans)))  

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

ans_l = 0
ans_r = 100001

for i in range(m):
    tmp_l,tmp_r = list(map(int,input().split()))
    if ans_l < tmp_l:
        ans_l = tmp_l
    if tmp_r < ans_r:
        ans_r = tmp_r
        
if ans_l <= ans_r:
    print((ans_r-ans_l+1))
else:
    print((0))
# Original solution
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
l = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]

listT = []
for t in l:
  listT = listT + t

s = min(list(set(listT)))
e = max(list(set(listT)))
cnt = 0
for i in range(s, e+1):
  isOk = True
  for start, end in l:
    if i < start or i > end:
      isOk = False
      break
  if isOk == True:
    cnt = cnt + 1

print(cnt)

# Optimized solution
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
l = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]

sMax = 0
eMin = 0
for s, e in l:
  if sMax < s or sMax == 0:
    sMax = s
  if eMin > e or eMin == 0:
    eMin = e

if eMin - sMax >= 0:
  print((eMin - sMax + 1))
else:
  print((0))
# Original solution
N, M = list(map(int,input().split()))
A = [0] * (N+1)
for i in range(M):
    L,R = list(map(int,input().split()))
    for i in range(L,R+1):
        A[i] += 1
print((sum([1 for a in A if a == M])))

# 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(0,min(Rs)-max(Ls)+1)))
# Original solution
n,m = list(map(int,input().split()))
l,r = 1,n
for i in range(m):
    l_tmp,r_tmp = list(map(int,input().split()))
    if l_tmp > l:
        l = l_tmp
    if r_tmp < r:
        r = r_tmp
if l>r:
    print((0))
else:
    print((r-l+1))

# Optimized solution
def main():
    n,m = list(map(int,input().split()))
    l = 0
    r = float('inf')
    for i in range(m):
        l_,r_ = list(map(int,input().split()))
        if l<l_:
            l = l_
        if r_<r:
            r = r_
    if r-l>=0:
        print((r-l+1))
    else:
        print((0))

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

count=0
judge=0

for i in range(1,N+1):
    judge=0
    for lr in LR:
        if lr[0]<=i<=lr[1]:
            judge+=1
    if judge==M:
        count+=1

print(count)

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

l=1
r=N

for i in LR:
    #print(i)
    if i[0]>l:
        l=i[0]
    if i[1]<r:
        r=i[1]

if r>=l:
    print((r-l+1))
else:
    print((0))
# Original solution
n, m = list(map(int, input().split()))
l1, r1 = list(map(int, input().split()))
num = list(range(1, n+1))
ans = set(num[l1-1:r1])
for i in range(2, m+1):
    l, r = list(map(int, input().split()))
    can_pass = set(num[l-1:r1])
    ans = ans & can_pass
    if len(ans) == 0:
        break
print((len(ans)))

# Optimized solution
n, m = list(map(int, input().split()))
l, r = list(map(int, input().split()))
for i in range(m-1):
    l_c, r_c = list(map(int, input().split())) 
    l = max(l, l_c)
    r = min(r, r_c)
print((max(r-l+1, 0)))
# Original solution
n,m = list(map(int,input().split()))
lr = [list(map(int,input().split())) for _ in range(m)]

li = [0]*n

for i in lr:
    for j in range(i[0]-1,i[1]):
        li[j] += 1
    

print((li.count(m)))


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

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

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

print((ll.count(m)))


# Optimized solution
n, m = list(map(int, input().split()))
ll = [0 for i in range(n+1)]
l_max = 0
r_min = 1000000
for _ in range(m):
    l, r = list(map(int, input().split()))
    if l > l_max :
        l_max = l
    if r < r_min:
        r_min = r
if l_max <= r_min:
    print((r_min - l_max + 1))
else:
    print((0))
# Original solution
n, m = list(map(int,input().split()))
cards = [1 for _ in range(n)]
for _ in range(m):
    l,r= [int(x) - 1 for x in input().split()]
    if 0 < l:    
        cards[:l] = [0 for _ in range(l)]
    if r < n-1:
        cards[r+1:] = [0 for _ in range(n-1 -r)]
print((sum(cards)))

# Optimized solution
n, m = list(map(int,input().split()))
cards = [1 for _ in range(n)]
L = 0
R = n-1
for _ in range(m):
    l,r= [int(x) - 1 for x in input().split()]
    L = max(L,l)
    R = min(R,r)
    
print((max(0,R-L+1)))
# Original solution
N,M=list(map(int,input().split()))
G=[]
a=[]
S=[]
for i in range(M):
    l,r=list(map(int,input().split()))
    a=[x for x in range(l,r+1)]
    G.append(a)

G_diff=set(G[0])
for i in range(1,M):
    G_diff=set(G_diff).intersection(set(G[i]))

if G_diff:
    print((len(G_diff)))
else:
    print((0))

# 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)
max_l=max(L)
min_r=min(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()))
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,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))
# 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,m = list(map(int,input().split()))
Low = []
Right = []
for i in range(m):
  l,r = list(map(int,input().split()))
  Low.append(l)
  Right.append(r)
if min(Right) < max(Low):
  print((0))
else:
  print((min(Right)- max(Low) + 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()))

def prison(N,M,L,R):
  num=list(range(L[0],(R[0]+1)))
  for j in range(M):
    num=[i for i in num if L[j]<=i<=R[j]]
  return len(num)
print((prison(N,M,L,R)))

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

def prison(N,M,L,R):
  max_val=min(R)
  min_val=max(L)
  num=max_val-min_val+1
  if num<=0:
    num=0
  return(num)

print((prison(N,M,L,R)))
# Original solution
import copy
n,m = list(map(int,input().split()))
l = []
for i in range(m):
  l.append([int(i) for i in input().split()])
ll = []
for k in range(l[0][0],l[0][1]+1):
  ll.append(k)
for j in range(1,m):
  for i in ll:
    lll = copy.copy(ll)
    if i < l[j][0] or i > l[j][1]:
      lll.remove(i)
    ll = lll
print((len(ll)))

# Optimized solution
n,m = list(map(int,input().split()))
l = []
for i in range(m):
  l.append([int(i) for i in input().split()])
maxL = l[0][0]
minR = l[0][1]
for j in range(1,m):
  if maxL <= l[j][0]:
    maxL = l[j][0]
  if minR >= l[j][1]:
    minR = l[j][1]
if maxL <= minR:
  print((minR - maxL + 1))
else:
  print((0))
# Original solution
n, m = list(map(int, input().split()))
gate = []
for i in range(m):
    li, ri = list(map(int, input().split()))
    gate.append([li, ri])

l_inside = list(range(1, n+1))
for i in range(m):
    l_inside = [j for j in l_inside if gate[i][0] <= j and j <= gate[i][1]]

print((len(l_inside)))


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

l_max = 1
r_min = n
for i in range(m):
    li, ri = list(map(int, input().split()))
    if l_max < li:
        l_max = li
    if r_min > ri:
        r_min = ri

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

left = -1
right = N
for _ in range(M):
    tl, tr = list(map(int, input().split()))
    left = max(left, tl)
    right = min(right, tr)

print((max(0, right - left + 1)))


# Optimized solution
N, M = list(map(int, input().split()))
maxL = 1
minR = N
for _ in range(M):
    l, r = list(map(int, input().split()))
    maxL = max(maxL, l)
    minR = min(minR, r)
print((max(minR - maxL + 1, 0)))
# 交差しない場合を考慮する
# Original solution
N, M = list(map(int, input().split()))

left = 1
right = N
# 1-indexed
# [left, right]

for _ in range(M):
    L, R = list(map(int, input().split()))
    if L > left:
        left = L
    if R < right:
        right = R
    if right - left + 1 <= 0:
        break
print((max(0, right - left + 1)))


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

l, r = [], []
for _ in range(m):
    ll, rr = list(map(int, input().split()))
    l.append(ll)
    r.append(rr)

print((max(0, min(r) - max(l) + 1)))
# Original solution
n, m = list(map(int, input().split()))

l, r = [], []
for _ in range(m):
    ll, rr = list(map(int, input().split()))
    l.append(ll)
    r.append(rr)

print((max(0, min(r) - max(l) + 1)))


# Optimized solution
def main():
    import sys

    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())
# Original solution
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(eval(input()))

def MI(): return list(map(int, input().split()))
def MI1(): return list(map(int1, input().split()))

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def logical_and(lst, l, r):
    res = []
    for i in range(l, r+1):
        if i in lst:
            res.append(i)
    return res

def solve():
    n, m = MI()

    l, r = MI()
    ans = list(range(l, r+1))
    for _ in range(m-1):
        l, r = MI()
        ans = logical_and(ans, l, r)
    print((len(ans)))


if __name__ == '__main__':
    solve()


# Optimized solution
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(eval(input()))

def MI(): return list(map(int, input().split()))
def MI1(): return list(map(int1, input().split()))

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def solve():
    n, m = MI()

    maxl, minr = MI()
    for _ in range(m-1):
        l, r = MI()
        if maxl < l:
            maxl = l
        if r < minr:
            minr = r
    if maxl <= minr:
        print((minr - maxl + 1))
    else:
        print((0))

if __name__ == '__main__':
    solve()
# Original solution
n,m = list(map(int,input().split()))
lr = [[] for i in range(m)]
cnt = 0
for _ in range(m):
    l,r = list(map(int,input().split()))
    lr[_].append(l)
    lr[_].append(r)

for nn in range(1,n+1):
    ok = True
    for ran in lr:
        if ran[0] > nn or ran[1] < nn:
            ok = False
    if ok:
        cnt += 1
print(cnt)

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

maxl = max(ls)
minr = min(rs)

for nn in range(1,n+1):
    if maxl <= nn and minr >= nn:
        cnt += 1
print(cnt)
# Original solution
N, M = list(map(int, input().split()))
cards = set(range(1, N+1))
for i in range(M):
    L, R = list(map(int, input().split()))
    cards &= set(range(L, R+1))
print((len(cards)))


# Optimized solution
N, M = list(map(int, input().split()))
maxl = 0
minr = N + 1
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()))


ans = [1] * n
for i in range(m):
    l, r = list(map(int, input().split()))
    ans[:l-1] = [0] * (l - 1)
    ans[r:] = [0] * (n - r)
print((sum(ans)))

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

Left = -1
Right = n + 1
for i in range(m):
    l, r = list(map(int, input().split()))
    Left = max(Left, l)
    Right = min(Right, r)
if Left <= Right:
    ans = Right - Left + 1
else:
    ans = 0
print(ans)
# Original solution
N, M = list(map(int, input().split()))
m = 0b0
exec("m = 0b" + "1"*N)
for i in range(M):
	L, R = list(map(int, input().split()))
	exec("_m = 0b" + "1"*(R - L + 1) + "0"*(N - R))
	m = m & _m
	if m == 0b0 : break

print((bin(m).count("1")))


# 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((max(R - L + 1, 0)))
# Original solution
n,m=list(map(int,input().split()))
L=[list(map(int,input().split())) for _ in range(m)]
 
lm=0
rm=float('inf')
for i in range(m):
      lm=max(L[i][0],lm)
      rm=min(L[i][1],rm)
print((max(0,rm-lm+1)))

# Optimized solution
n,m=list(map(int,input().split()))
ml,mr=0,float('inf')
for i in range(m):
      l,r=list(map(int,input().split()))
      ml=max(l,ml)
      mr=min(r,mr)
print((max(mr-ml+1,0)))
# Original solution
N,M = list(map(int,input().split()))

cards = [x for x in range(N)]

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

print((len(tmp)))


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

cards = [x for x in range(N)]

tmp = cards
Ls = []
Rs = []
for i in range(M):
    L,R = list(map(int,input().split()))
    Ls.append(L - 1)
    Rs.append(R - 1)

print((max(min(Rs) - max(Ls) + 1,0)))
# Original solution
M,N = list(map(int,input().split()))
ans=set(range(1,M+1))
for s in range(N):
  S,Z = list(map(int,input().split()))
  ans =  ans & set(range(S, Z+1))
print((len(ans)))

# Optimized solution
M,N = list(map(int,input().split()))
ans=set(range(1,M+1))
START=0
END=10**5
for s in range(N):
  S,Z = list(map(int,input().split()))
  if START <= S:
    START = S
  if END >= Z:
    END = Z
ans = list(range(START,END+1))
print((len(ans)))
# Original solution
n,m = list(map(int,input().split()))
cnt = set(range(1,n+1))
for i in range(m):
    l,r = list(map(int,input().split()))
    cnt = cnt&set(i for i in range(l,r+1))
print((len(cnt)))

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

# Optimized solution
n,m=list(map(int,input().split()))
a=list(range(1,n+1))
l=0
l1=0
r=0
r1=10**10

for i in range(m):
	l,r=list(map(int,input().split()))
	if l>l1:
		l1=l
	if r<r1:
		r1=r
b=list(range(l1,r1+1))
a=list(set(b) & set(a))
print((len(list(a))))
# Original solution
#coding:utf-8
n, m = list(map(int, input().split()))
l, r = 10**6, 0
ll = []
ans_l = []
tmp_ans = []
for i in range(m):
    a, b = list(map(int, input().split()))
    ans_l = []
    if i == 0:
        tmp_ans = list(range(a, b+1, 1))
    else:
        for j in range(len(tmp_ans)):
            if a <= tmp_ans[j] <= b:
                ans_l.append(tmp_ans[j])
        tmp_ans = ans_l[:]
print((len(set(tmp_ans))))

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