#include #include #define int long long #define ll long long #define rep(i, n) for(ll i = 0; i < (n); i++) #define rep2(i, n) for(ll i = 0; n; i++) #define rep3(i,s,n) for(ll i = (s); i < (n); i++) #define rep4(i,s,n) for(ll i = (s); n; i++) #define repe(v, V) for(auto& v: V) #define all(x) (x).begin(),(x).end() #define v vector #define mod(a,b) (((a) % (b) + (b)) % (b)) /* 負の数に対応した剰余 */ #define pi 3.14159265358979 #define eps 1e-13 #define inf 9223372036854775807 #define MAX_LL 9223372036854775807 #define umap unordered_map using namespace std; const int dx[4] = { 1, 0, -1, 0 }; const int dy[4] = { 0, 1, 0, -1 }; template using min_priority_queue = priority_queue, greater>; int to_base10(string Nx, int base) /* N進数から10進数に変換 */ { int N10 = 0; rep(i, Nx.length()) N10 += stoll(Nx.substr(Nx.length() - 1 - i, 1)) * pow(base, i); return N10; } string to_baseN(int N10, int base, int min_digits = 1) /* 10進数からN進数に変換 */ { string r; while (N10 != 0) { r = to_string(N10 % base) + r; N10 /= base; } while (r.length() < min_digits) r = "0" + r; return r; } v> prime_factorize(int N) /* 素因数分解 .first: 素数, .second: 指数 */ { v> res; rep4(a, 2, a * a <= N) { if (N % a != 0) continue; int ex = 0; while (N % a == 0) { ++ex; N /= a; } res.push_back({a, ex}); } if (N != 1) res.push_back({N, 1}); return res; } v divisors(int N)/*約数列挙*/ {v r;rep4(i, 1, i * i <= N) {if (N % i == 0) {r.push_back(i);if(i != N / i) r.push_back(N / i);}}return r;} int modinv(int a, int m)/*逆元*/ { int b = m, u = 1, v = 0;while (b) { long long t = a / b;a -= t * b; swap(a, b); u -= t * v; swap(u, v); }u %= m;if (u < 0) u += m; return u;} int sigma(int a, int b)/*a,a+1,...,bの総和*/ {return b * (b + 1) / 2 - a * (a - 1) / 2;} v> Pascal(int N, int mod = MAX_LL)/*パスカルの三角形(nCk=v[n][k]) O(N^2)*/ { v> com(N + 1,v(N + 1)); com[0][0] = 1; for (int i = 1; i < N + 1; ++i) { com[i][0] = 1; for (int j = 1; j < N + 1; j++) { com[i][j] = (com[i - 1][j - 1] + com[i - 1][j]) % mod; } }return com; } int Pow(int n, int p, int mod = MAX_LL)/* 指数関数のLL版(繰り返し2乗法) modあり */ {int ans = 1;int now = n;for (; p > 0; p >>= 1){if ((p & 1) == 1){ans *= now;ans %= mod;}now *= now;now %= mod;}return ans;} void printVec(const vector& vec)/* vectorの中身をprint */ {cout << "";for (auto it = vec.begin(); it != vec.end(); ++it) {cout << *it << " "; } cout << endl;} void makeset(v& vec)/* vectorをソートし、重複要素を削除*/ { sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end());} int N, M; signed main() { cout << setprecision(15); cin >> N >> M; v A(M), B(M), NodeList(0); rep(i, M) { cin >> A[i] >> B[i]; NodeList.push_back(A[i]); NodeList.push_back(B[i]); } NodeList.push_back(1); NodeList.push_back(N); makeset(NodeList); int N2 = NodeList.size(); v> G(N2), W(N2); rep(i, N2 - 1) { G[i].push_back(i + 1); W[i].push_back((NodeList[i + 1] - NodeList[i]) * 2); } rep(i, M) { int a = lower_bound(all(NodeList), A[i]) - NodeList.begin(); int b = lower_bound(all(NodeList), B[i]) - NodeList.begin(); G[a].push_back(b); W[a].push_back(2 * (B[i] - A[i]) - 1); } queue que; //stackにすればDFS vector dist(G.size(), MAX_LL); que.push(0); dist[0] = 0; while (!que.empty()) { int i = que.front(); que.pop(); for (int j = 0; j < G[i].size(); j++) { if (dist[G[i][j]] > dist[i] + W[i][j]) { dist[G[i][j]] = dist[i] + W[i][j]; que.push(G[i][j]); } } } cout << dist[N2 - 1]; }