from collections import defaultdict from heapq import heapify, heappop, heappush n, m = map(int, input().split()) Edge = defaultdict(list) A, B = set(), set() for _ in range(m): a, b = map(int, input().split()) Edge[a - 1].append((b - 1, 2 * b - 2 * a - 1)) A.add(a - 1) INF = 10**18 DP = defaultdict(lambda: INF) DP[0] = 0 H = [(0, 0)] visited = set() heapify(H) while H: ccost, cp = heappop(H) if ccost > DP[cp] or cp in visited: continue if cp == n - 1: print(ccost) break visited.add(cp) seen = set() for np, dcost in Edge[cp]: seen.add(np) if ccost + dcost >= DP[np]: continue DP[np] = ccost + dcost heappush(H, (ccost + dcost, np)) for np in A: if np in seen: continue if cp >= np: continue if ccost + 2 * (np - cp) >= DP[np]: continue DP[np] = ccost + 2 * (np - cp) heappush(H, (ccost + 2 * (np - cp), np)) if ccost + 2 * (n - 1 - cp) < DP[n - 1]: DP[n - 1] = ccost + 2 * (n - 1 - cp) heappush(H, (ccost + 2 * (n - 1 - cp), n - 1))