N,M = map(int,input().split()) s = set() from collections import defaultdict G = defaultdict(list) for _ in range(M): a,b = map(int,input().split()) G[a].append((b,2 * b - 2 * a - 1)) s.add(a) s.add(b) s.add(1) s.add(N) l = sorted(s) D = {v:i for i,v in enumerate(l)} n = len(l) for i in range(n - 1): G[l[i]].append((l[i+1],2 * (l[i+1] - l[i]))) inf = 10 ** 10 dist = [inf] * n dist[0] = 0 import heapq q = [(0,1)] while q: d,now = heapq.heappop(q) if dist[D[now]] < d:continue for v,c in G[now]: if dist[D[v]] > d + c: dist[D[v]] = d + c heapq.heappush(q,(d + c,v)) print(dist[-1])