n,k = map(int,input().split())
mod = 998244353
s = []
for i in range(k):
    l,r =map(int,input().split())
    if l == r:
        if l < n:
            s.append(l)
    else:
        for i in range(l,r+1):
            if i < n:
                s.append(i)
    s = list(set(s))
s = sorted(s)

min1 = s[0]
tansa = sorted(s[:])
road = n-1
count=0
while tansa != []:
    search = tansa.pop()
    road-=search
    save = road
    for i in range(n//min1+1):
        count+=len(s)
        for e in s:
            road-=e
            if road<0:
                count-=1
            road = save
    road = n-1
print(road)