peterverebics/CodeNet_Python_118 huggingface dataset:
def main():
    x = list(map(int, input().split()))
    n = x[0]
    m = x[1]

    common_section = [1, n+1]

    for i in range(m):
        a = list(map(int, input().split()))
        if a[0] > common_section[0]:
            common_section[0] = a[0]
        if a[1] < common_section[1]:
            common_section[1] = a[1]

    answer = common_section[1] - common_section[0] + 1
    print(answer)


if __name__ == "__main__":
    main()
N, M = map(int, input().split())
card = False
for _ in range(M):
    L, R = map(int, input().split())
    if card is True:
        card = card & set([i for i in range(L, R + 1)])
    else:
        card = set([i for i in range(L, R + 1)])
print(len(card))
n,a = map(int,input().split())
m = [tuple(map(int,input().split())) for _ in range(a)]

l,r=m[0][0],m[0][1]

for i in range(1,a):
    l = max(m[i][0],l)
    r = min(m[i][1],r)

print(r-l+1)
N,M = map(int,input().split())
List = [0 for i in range(N)]

for m in range(M):
  L,R = map(int,input().split())
  List[L-1:R] = [1 for i in range(R-L+1)]  
  List[:L-1] = [0 for i in range(L-1)]
  List[R:] = [0 for i in range(N-R)]
  print(List)
  
print(List.count(1))
n, m = map(int, input().split())
left, right = 1, n
for _ in range(m):
  l, r = map(int, input().split())
  if l > left:
    left = l
  if r < right:
    right = r
  if l >= r:
    print(0)
    quit()
print(right - left + 1)
n,m  = map(int, input().split())
l = []
c = []
for i in range(m):
    a,b = map(int, input().split())
    for j in range(a,b+1):
        l.append(j)
    l = sorted(l)
#print(l)
x = 0
y = 0
for k in range(len(l)-1):
    if l[k] ==l[k+1]:
        x+=1
        if x == m-1:
            y += 1
    else:
        x = 0
        continue

if y == 0:
    print(n)
else:
    print(y)
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = list(map(int, sys.stdin.buffer.readline().split()))
    LR = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]
    mi = 1
    mx = N

    for lr in LR:
        l = lr[0]
        r = lr[1]
        mi = max(mi, l)
        mx = min(mx, r)

    print(mx - mi + 1)


if __name__ == '__main__':
    main()
# C - Prison

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

ansl = 1
ansr = n

for i in range(m):
    l, r = map(int, input().split())
    if l>ansl:
        ansl = l
    if r<ansr:
        ansr = r

print(ansr-ansl+1)
f=lambda:map(int,input().split())
n,m=f()
L,R=0,n
for i in range(m):
  l,r=f()
  L=max(L,l)
  R=min(R,r)
print(R-L+1)
n,m = list(map(int, input().split()))
high=10**10
low=0
for i in range(m):
    a,b = list(map(int, input().split()))
    if a==b:
        print(1)
        exit()
    if a>low:
        low=a
    if b<high:
        high=b
if low>high:
    print(0)
else:
    print(high-low+1)
N,M = map(int,input().split())
LR = [list(map(int,input().split())) for i in range(M)]

maxi = 10**6
mini = 0
for lr in LR:
    mini = max(lr[0],mini)
    maxi = min(lr[1],maxi)

if maxi <= N:
    print(maxi-mini+1)
else:
    print(N-maxi+1)
n,m=map(int, input().split())
lr = [map(int, input().split()) for _ in range(m)]
l, r = [list(i) for i in zip(*lr)]
answer = (r[0]  - l[-1]  +1) 
if answer > 0:
    print(answer)
else:
    print("0")
n,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(m)]
print(l)
a=0
z=10**5
for i in range(m):
    a=max(a,l[i][0])
    z=min(z,l[i][1])
print(z-a+1)
n,m=map(int,input().split())
l_max=1
r_min=10000
for i in range(m):
    l,r=map(int,input().split())
    if l>l_max:
        l_max=l
    if r<r_min:
        r_min=r
print(max(0,r_min-l_max+1))
n, m = map(int, input().split())

left = 1
right = n+1

for i in range(m):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)

print(right-left+1)
N, M = map(int, input().split())
L, R = [0]*M, [0]*M
for i in range(M):
    L[i],R[i] = map(int, input().split())

print(min(R)-max(L)+1)
N,M=map(int,input().split())
LR=[list(map(int,input().split())) for i in range(M)]
l=LR[0][0]
r=LR[0][1]
for lr in LR:
    if l<lr[0]:
        l=lr[0]
    if lr[1]<r:
        r=lr[1]
print(r-l+1)
n, m = map(int, input().split())
low = 0
high = 100001

for i in range(m):
  l,m = map(int, input().split())
  low = max(low, l)
  high = min(high, m)
print(min(high-low+1, n))
N,M=map(int,input().split())
L=[]
R=[]
for i in range(M):
  l,r=map(int,input().split())
  L.append(l)
  R.append(r)
count=0
for i in range(1,N+1):
  if min(L)>i or i>max(R):
    break
  else:
    count+=1
print(count)
n,m = map(int,input().split())
lr = [list(map(int,input().split())) for i in range(m)]

max = 0
min = n+1
for i in range(m):
    if lr[i][0] > max :
        max = lr[i][0]
    if lr[i][1] < min :
        min = lr[i][1]

print(min-max+1)
def solve(n, m, LR):
    maxl = 0
    minr = n
    for l, r in LR:
        if maxl < l:
            maxl = l
        if minr > r:
            minr = r
    return minr - maxl + 1

_n, _m = map(int, input().split())
_LR = [list(map(int, input().split())) for _ in range(_m)]
print(solve(_n, _m, _LR))
N, M = map(int, input().split())
idcard = [[int(j) for j in input().split()] for i in range(M)]

L = 0
R = 10**10
for i in idcard:
    L = max(L, i[0])
    R = min(R, i[1])

print(R-L+1)
n, m = map(int, input().split())
l = 0
r = n
for i in range(m):
    lt, rt = map(int, input().split())
    if (lt > l):
        l = lt
    if (rt < r):
        r = rt
print(r-l+1)
N, M = map(int, input().split())
a = []
for i in range(M):
    a.append(list(map(int, input().split())))
b = [0, float("INF")]
for i in range(M):
    b[0] = max(a[i][0], b[0])
    b[1] = min(a[i][1], b[1])
print(b[1] - b[0] + 1)
import sys
n,m = map(int,input().split())
ls = [list(map(int,input().split())) for i in range(m)]
a = ls[0][0]
b = ls[0][1]
for j in range(1,m-1):
    a = max(a,ls[j+1][0])
    b = min(b,ls[j+1][1])
    if a-b<0:
        print(0)
        sys.exit()
print(a-b+1)
import os, sys, re, math

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

minC = 1
maxC = N
for i in range(M):
    (L, R) = [int(n) for n in input().split()]
    if minC <= maxC:
        minC = max(minC, L)
        maxC = min(maxC, R)

print(maxC - minC + 1)
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
ll=sum(l,[])
max_=max(ll[::2])
min_=min(ll[1::2])
print(min_-max_+1)
# coding: utf-8
# Your code here!

N,M = map(int,input().split())
Li,Ri = map(int,input().split())

for i in range(2,M+1):
    L,R = map(int,input().split())
    
    if (L<=Ri and R>=Ri):
        Li = L
    elif (R>=Li and Li>=L):
        Ri = R
    elif (R==Ri and L==Li):
        Li=L
        Ri=R
    else:
        Li=1
        Ri=0
        break
        
print(Ri-Li+1)
import sys
import math
import itertools


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


def main():
    N, M = map(int, input().split())
    L = 0
    R = 10 ** 5 + 1

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

    print(R-L+1)


if __name__ == "__main__":
    main()
n,m = map(int,input().split())
L = [0]*m
R = [0]*m
for i in range(m):
    L[i],R[i] = map(int,input().split())
print(min(R)-max(L)+1)
n, m = map(int, input().split())
cnt = [0] * (n + 2)
cnt_sum = [0] * (n + 1)
for _ in range(m):
    l, r = map(int, input().split())
    cnt[l] += 1
    cnt[r + 1] -= 1
for i in range(1, n + 1):
    cnt_sum[i] = cnt_sum[i - 1] + cnt[i]
print(cnt_sum.count(max(cnt_sum[1:])))
n,m=map(int,input().split())
s=[]
S=[]
for i in range(m):
    l,r=map(int,input().split())
    s.append(l)
    S.append(r)

print(min(S)-max(s)+1)
N, M = map(int, input().split())
LR = [map(int, input().split()) for _ in range(M)]
L, R = [list(i) for i in zip(*LR)]

ans = 0

m = M-1
 
ans = N - m

print(ans)
N,M = map(int,input().split())
min_L = 1
max_R = 1 * 10 ** 5
for i in range(M):
    L,R=map(int,input().split())
    min_L = max(L,min_L)
    max_R = min(R,max_R)

if max_R > min_L :  
    print(max_R - min_L + 1)
else :
    print(0)
def main():
    N, M = map(int, input().split(" "))
    left = 0
    right = 10**5
    for i in range(M):
        a, b = map(int, input().split(" "))
        left = max(left, a)
        right = min(right, b)
    print(right - left + 1)


if __name__ == '__main__':
    main()
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())))

print(min(r_list) - max(l_list) + 1)
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(R-L+1)
N, M = map(int, input().split())
L_list = []
for i in range(M):
    A = list(map(int, input().split()))
    L_list.append(A)
min_range = L_list[0][0]
max_range = L_list[0][1]
for i in range(i, M):
    if L_list[i][0] > min_range:
        min_range = L_list[i][0]
    if L_list[i][1] < max_range:
        max_range = L_list[i][1]
print(max_range-min_range+1) if max_range>=min_range else print(0)
n,m=map(int,input().split())
L,R=1,n
for i in range(m):
  l,r=map(int,input().split())
  L=max(L,l)
  R=min(R,r)
print(R-L+1)
import sys
input=sys.stdin.readline

n,k=map(int,input().split())
L=[0]*(n+1)
for i in range(k):
    a,b=map(int,input().split())
    for j in range(a,b+1):
        L[j]+=1
print(L.count(max(L)))
n,m = map(int,input().split())
card = [0,n+1]

for i in range(m):
    l,r = map(int,input().split())
    card.append(l)
    card.append(r)
    card = sorted(card)
    card = card[1:3]
    
print(card[1]-card[0]+1)
n,m = list(map(int, input().split()))
high=10**10
low=0
for i in range(m):
    a,b = list(map(int, input().split()))
    if a==b:
        print(1)
        exit()
    if a>low:
        low=a
    if b<high:
        high=b
print(high-low+1)
n,m = map(int,input().split())

l = 0
r = (10**5)+1

for i in range(m):
    a,b = map(int,input().split())
    l = max(l,a)
    r = min(r,b)

print(r-l+1)
N, M = map(int, input().split())

L, R = 0, 10 ** 5 + 1
for _ in range(M):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r+1)


result = min(N, (R-L))
print(result)
n,m = map(int,input().split())
ans = [-1,10**6]

for i in range(m):
    tmp = list(map(int,input().split()))
    ans[0] = max(ans[0],min(tmp))
    ans[1] = min(ans[1],max(tmp))

print(ans[1]-ans[0]+1)
n,m = map(int,input().split())
l = [0] * m
r = [0] * m
l_max = 1
r_min = n
for i in range(m):
    l[i], r[i] = map(int,input().split())
    l_max = max(l[i],l_max)
    r_min = min(r[i], r_min)
print(r_min - l_max + 1)
N,M = map(int, input().split())

Lm = -1
Rm = float('inf')

for _ in range(M):
    L,R = map(int, input().split())
    Lm = max(L, Lm)
    Rm = min(R, Rm)

print(Rm - Lm + 1)
N,M=list(map(int,input().split()))
l=1
r=N
for m in range(M):
    l_,r_ = list(map(int,input().split()))
    l = max(l,l_)
    r = min(r,r_)
    
ans = r-l+1

print(ans)
def resolve():
    N, M = map(int, input().split())

    start_index = 0
    last_index = N
    for _ in range(M):
        pp, ss = map(int, input().split())
        start_index = max(start_index, pp)
        last_index = min(last_index, ss)

    print(last_index - start_index + 1)


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

for _ in range(m):
    l,r = map(int,input().split())
    L.append(l)
    R.append(r)
print(min(R)-max(L)+1)
n,m=map(int,input().split())
lr=[list(map(int,input().split())) for _ in range(m)]
print(sorted(lr,key=lambda x:x[1])[0][1]-sorted(lr,key=lambda x:x[0])[-1][0]+1)
N, M = map(int, input().split())

left = 0
right = N

for i in range(M):
    L, R = map(int, input().split())
    left = max(left, L)
    right = min(right, R)

print(right - left + 1)
n, m = map(int, input().split())
l = 0
r = float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    if l < li:
        l = li
    if r > ri:
        r = ri

print(r - l + 1)
N, M = map(int, input().split())
a = [0]*N
for i in range(M):
    L, R = map(int, input().split())
    for b in range(R-L+1):
        a[L+b-1] += 1
count = 0
for c in a:
    if c == 3:
        count+=1
print(count)
N, M = map(int, input().split())
L = []
R = []
for _ in range(M):
  l, r = map(int, input().split())
  L.append(l)
  R.append(r)

l = max(L)
r = min(R)
print(r-l+1)
N,M =list(map(int,input().split()))
la=1
rm=N
for i in range(M):
  a,b=list(map(int,input().split()))
  la=max(la,a)
  rm=min(rm,b)
print(max(0,la-rm+1))
N, M = map(int,input().split())
a = []
b = []
for i in range(M):
    L, R = map(int,input().split())
    a.append(L)
    b.append(R)
a = sorted(a)
b = sorted(b)
A = a[M - 1]
B = b[0]
print(B - A + 1)
n,m = map(int,input().split())
ans = 0
for i in range(m):
  l,r = map(int,input().split())
  d = range(l,r+1)
  for j in range(n):
    if j+1 in d:
      ans += 1
print(ans)
n, m = map(int,input().split())
l = [0]*m
r = [0]*m
for i in range(m):
    l1,r1 = map(int,input().split())
    l[i] = l1
    r[i] = r1
print(r[0] - l[-1] + 1)
N,M=map(int, input().split())
max_L = 0
min_R = N
for i in range(M):
  L,R=map(int, input().split())
  max_L = max([max_L,L])
  min_R = min([min_R,R])

print(min_R-max_L+1)
n,m = map(int,input().split())
#v = list(map(int,input().split()))

#a = [0 for _ in range(n+1)]
ml = -1
mr = 10**6
for i in range(m):
  l,r = map(int,input().split())
  ml = max(ml,l)
  mr = min(mr,r)
  
res = ml - mr + 1
print(res)
n,m = map(int,input().split())
l = [0]*m
r = [0]*m
for i in range(m):
    l[i],r[i] = map(int,input().split())
if min(r)-max(l) < 0:
    print(0)
else:
    print(min(r)-max(l))
n,m=map(int, input().split())
lmax=0
rmin=1000000

for i in range(m):
  l,r=map(int, input().split())
  lmax=max(l, lmax)
  rmin=min(r, rmin)
print(rmin-lmax+1)
N,M = map(int,input().split())
L = [0] * M
R = [0] * M
for i in range(M):
  L[i],R[i]= map(int,input().split()) 

print(min(R)-max(L)+1)
N,M=map(int,input().split())
L_max=0
R_min=10**9
for i in range(M):
  L,R=map(int,input().split())
  L_max=max(L_max,L)
  R_min=min(R_min,R)
print(R_min-L_max+1)
n,m = map(int, input().split())

L = []
R = []

for _ in range(m):
  l,r = map(int, input().split())
  L.append(l)
  R.append(r)
    
print(min(R)-max(L)+1)
n, m = map(int, input().split())

left = 0
right = 10000

for _ in range(m):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)

result = right - left + 1

print(result if result >= 0 else 0)
N,M = map(int, input().split())

L=[0]*M
R=[0]*M
for i in range(M):
  L[i], R[i] = map(int, input().split())

keys = []

for i in range(M):
  keys.append(set(range(L[i],R[i]+1)))
  
common = set(range(1, N+1))
for i in range(M-1):
  common = common & keys[i]
    
print(len(common))
n, m = map(int,input().split())
l, r = map(int,input().split())

for i in range(m-1):
  a, b = map(int,input().split())
  if a > l:
    l = a
  if b < r:
    r = b

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

L = a[0][0]
R = a[0][1]

for i in range(1,m):
  L = max(L,a[i][0])
  R = min(R,a[i][1])

R = min(R,n)

print(R - L + 1)
n, m = map(int, input().split())

L = 1
R = 10**5

for i in range(m):
    l, r = map(int, input().split())
    if L < l < R:
        L = l
    if L < r < R:
        R = r

print(R - L + 1)
#ABC127 C

N,M = map(int,input().split())
x,y = [],[]
C = []
for i in range(M):
    L,R = map(int,input().split())
    x.append(L)
    y.append(R)
    C.append(list(range(L,R+1)))

D = 0
for k in range(N):
    K = 0
    for l in range(M):
        if k in C[l]:
            K += 1
    if K == M:
        D += 1
        
print(D)
n, m = map(int, input().split())
l, r = [0]*m, [0]*m
for i in range(m):
    l[i], r[i] = map(int, input().split())

print(min(r)-max(l)+1)
n, m = list(map(int, input().split()))
l = []
r = []
for i in range(m):
    a, b = list(map(int, input().split()))
    l.append(a-1)
    r.append(b-1)

end = n-1
start = 0

for i in range(m):
    if start <= l[i] and r[i] <= end:
        start = l[i]
        end = r[i]
    elif l[i] < start and r[i] <= end:
        end = r[i]
    elif start <= l[i] and end < r[i]:
        start = l[i]
print(end-start+1)
N, M = list(map(int, input().split()))
L = []
R = []
for _ in range(M):
    l, r = list(map(int, input().split()))
    L += [l]
    R += [r]
print(min(R) - max(L) + 1)
n,m=map(int,input().split())
L=1
R=n
for i in range(m):
  l,r=map(int,input().split())
  L=max(l,L)
  R=min(r,R)
  
print(R-L+1)
n, m = map(int, input().split())
ans = 0
l_max = 0
r_min = 0
for i in range(1,m+1):
  l, r = map(int, input().split())
  if r < r_min:
    r_min = r
  if l_max > l:
    l_max = l
for i in range(1, n+1):
  if i > l_max and i < r_min:
    ans += 1
print(ans)
N, M = map(int, input().split()) 
left = 1
right = N
for _ in range(M):
    L, R = map(int, input().split())
    if left < L:
        left = L
    if R < right:
        right = R
print(right - left + 1)
def main():
    N, M = map(int, input().split())
    left, right = 0, N-1
    for _ in range(M):
        l, r = map(int, input().split())
        left = max(left, l-1)
        right = min(right, r-1)
    
    print(right - left + 1)

if __name__ == "__main__":
    main()
# -*- coding: utf-8 -*-

n,m=map(int, input().split())
l = ["" for _ in range(m)]
r = ["" for _ in range(m)]

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

x = len(range(max(l), min(r)))+1
print(x)
n,m=map(int,input().split())
gate=[list(map(int,input().split())) for i in range(m)]
small=[]
tall=[]
for i in range(m):
    small+=[gate[i][0]]
    tall+=[gate[i][1]]
smallmax=(max(small))
tallmin=(min(tall))
print(tallmin-smallmax+1)
N, M = map(int, input().split())

st = 0
en = N

for _ in range(M):
    l, r = map(int, input().split())
    st = max(st, l)
    en = min(en, r)

print(en - st + 1)
N, M = map(int, input().split())

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

for i in range(M):
    l_i, r_i = map(int, input().split())
    if l_i > l:
        l = l_i
    if r_i < r:
        r = r_i

if (r - l) > N:
    print(N)
else:
    print(r-l+1)
N, M = map(int, input().split())

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

left = 1
right = N

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

print(right - left + 1)
N, M = map(int, input().split())

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

L.sort()
R.sort()

if L[-1] > R[0]:
    print("No")
else:
    print(R[0] - L[-1] + 1)
n, m = map(int, input().split())

begin = 1
end = n
for i in range(m):
    a, b = map(int, input().split())
    if begin > end:
        begin = 0
        end = 0
        break
    if begin <= a:
        if b <= end:
            begin = a
            end = b
        else:
            begin = a
    else:
        if b <= end:
            end = b
        else:
            continue
print(end - begin+1)
n, m = map(int, input().split())
 
left  = 0
right = n
for i in range(m):
  l, r = map( int, input().split() )
  left = max( left, l )
  right = min( right, r )
 
print( right - left + 1 )
N,M=map(int,input().split())
L=[]
R=[]
for i in range (M):
	X,Y=map(int,input().split())
	L.append(X)
	R.append(Y)
print(min(R)-max(L)+1)
N, M = map(int, input().split(' '))
l_val, r_val = map(int, input().split(' '))
for i in range(M-1):
    l, r = map(int, input().split(' '))
    l_val = max(l_val, l)
    r_val = min(r_val, r)
if r_val == l_val:
    print(1)
else:
    print(r_val - l_val + 1)
def main():
    n, m = map(int, input().split())

    card_list = [0] * n

    l_list = []
    r_list = []

    for _ in range(m):
        l, r = 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 __name__ == '__main__':
    main()
#C
n, m = map(int, input().split())
l = []
r = []
for i in range(m):
    lin = list(map(int, input().split()))
    l.append(lin[0])
    r.append(lin[1])
lma = max(l)
rmi = min(r)
print(rmi - lma + 1)
N,M = map(int,input().split())
L = []
R = []
for _ in range(M):
    a,b = map(int,input().split())
    L.append(a)
    R.append(b)


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

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

ans = [1]*n

s = 0
m = n

for i in list:
    if i[0] > s:
        s = i[0]
    if i[1] < m:
        m = i[1]
if m>=s:
    print(s, m)
else:
    print(0)
N,M = map(int,input().split())

left=1
right=pow(10,5)

for i in range(M):
    L,R=map(int,input().split())
    left=max(left, L)
    right=min(right,R)

print(right-left+1)
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n, m = map(int, input().split())
    L, R = 1, n
    for _ in range(m):
        l, m = map(int, input().split())
        if l > L: L = l
        if m < R: R = m
    print(R-L+1)
    
if __name__ == '__main__':
    main()
n,m=map(int,input().split())
lr=[list(map(int,input().split())) for i in range(m)]

min_num=0
max_num=10**5
for l,r in lr:
    if l>min_num:min_num=l
    if r<max_num:max_num=r

print(max_num-min_num+1)
import sys
input = sys.stdin.readline

n, m = [int(w) for w in input().split()]

m_l = 0
m_r = n
for i in range(m):
    l, r = [int(w) for w in input().split()]
    m_l = max(m_l, l)
    m_r = min(m_r, r)
print(m_r-m_l+1)
N,M = map(int,input().split())
x,y = [],[]
C = []
for i in range(M):
    L,R = map(int,input().split())
    x.append(L)
    y.append(R)
    C.append(list(range(L,R+1)))

D = 0
for k in range(N):
    K = 0
    for l in range(M):
        if k in C[l]:
            K += 1
    if K == M:
        D += 1
        
print(D)
L,M=map(int,input().split())
a=[0]*L
x=[]
y=[]
for i in range(M):
    b,c=map(int,input().split())
    x.append(b)
    y.append(c)
if max(x)<min(y):
    print(-max(x)+min(y)+1)
else:
    print(0)
_, M = map(int, input().split())
left_rights = [list(map(int, input().split())) for _ in range(M)]

l_max = 0
r_min = 10 ** 6
for l, r in left_rights:
    l_max = max(l_max, l)
    r_min = min(r_min, r)

print(r_min - l_max + 1)
import sys

n, m, *lr = map(int, sys.stdin.read().split())
lr = zip(*[iter(lr)] * 2)

def main():
    le, ra = 1, n
    for l, r in lr:
        le = max(l, le)
        ra = min(r, ra)
    return ra - le + 1

if __name__ == '__main__':
    ans = main()
    print(ans)
N,M = map(int,input().split())
L = []
R = []
for _ in range(M):
    a,b = map(int,input().split())
    L.append(a)
    R.append(b)

if R[0] - L[-1] >= 0:
    print(R[0] - L[-1] + 1)
else:
    print(0)
s = input()
l = ["A", "C", "G", "T"]
ans = 0
c = 0

for i in range(len(s)):
    if s[i] in l:
        c += 1
        if c > ans:
            ans = c
    else:
        c = 0

print(ans)
s=input()
ans=0
c=0
for i in range(len(s)):
    if s[i] in ["A","C","G","T"]:
        c+=1
    else:
        c=0
    ans=max(ans,c)
print(ans)
import re
print(max(map(len,re.findall("[ACGT]*",input()))))
words = input()

lst = ['A', 'C', 'G', 'T']
max_count = 0
count = 0
for i in range(len(words)):
  if words[i] in lst:
    count += 1
    if i == len(words) - 1:
      if count > max_count:
        max_count = count
  else:
    if count > max_count:
      max_count = count
    count = 0
print(max_count)
# coding: utf-8

S = input()

res = 0
now_mojisu = 0
agct = ["A", "C", "G", "T"]

for i in S:
    if i in agct:
        now_mojisu += 1
    else:
        now_mojisu = 0
    res = max(res, now_mojisu)

print(res)
import sys
import re
S = input()
if not ( 1 <= len(S) <= 10 ): sys.exit()
if not S.isupper: sys.exit()

result = 0
max = max(re.findall('[A|T|G|C]+', S),key=len) if re.findall('[A|T|G|C]+', S) else ''
if len(max) > 0:result = len(max)
print(result)