/** * author: shu8Cream * created: 01.07.2022 21:48:29 **/ #include using namespace std; #define overload3(a,b,c,d,...) d #define rep1(i,n) for (int i=0; i<(n); i++) #define rep2(i,a,n) for (int i=(a); i<(n); i++) #define rep(...) overload3(__VA_ARGS__, rep2, rep1)(__VA_ARGS__) #define rrep1(i,n) for (int i=(n-1); i>=0; i--) #define rrep2(i,a,n) for (int i=(n-1); i>=(a); i--) #define rrep(...) overload3(__VA_ARGS__, rrep2, rrep1)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) int((x).size()) #define pcnt __builtin_popcountll using ll = long long; using P = pair; template using vc = vector; template using vv = vc>; using vi = vc; using vvi = vv; const ll INF = 8e18; templateistream& operator>>(istream&i,vc&v){rep(j,sz(v))i>>v[j];return i;} templatestring join(const T&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<ostream& operator<<(ostream&o,const vc&v){if(sz(v))o<istream& operator>>(istream&i,pair&v){return i>>v.first>>v.second;} templateostream& operator<<(ostream&o,const pair&v){return o< inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template string to_string(T s); template string to_string(pair p); string to_string(char c) { return string(1, c); } string to_string(string s) { return s; } string to_string(const char s[]) { return string(s); } template string to_string(T v) { if (v.empty()) return "{}"; string ret = "{"; for (auto x : v) ret += to_string(x) + ","; ret.back() = '}'; return ret; } template string to_string(pair p) { return "{" + to_string(p.first) + ":" + to_string(p.second) + "}"; } void debug_out() { cout << endl; } template void debug_out(Head H, Tail... T) { cout << to_string(H) << " "; debug_out(T...); } #ifdef _DEBUG #define debug(...) debug_out(__VA_ARGS__) #else #define debug(...) #endif template vector compress(vector a){ auto b = a; sort(all(b)); b.erase(unique(all(b)), b.end()); for(auto &p:a) p = lower_bound(all(b),p) - b.begin(); return a; } struct edge {int to; ll cost; int id=0;}; vi dist; // 最短経路木 vc tree; // edges:グラフ, V:頂点数, s:始点 を与える void dijkstra(vv &edges, int V, int s){ dist.resize(V,INF); dist[s] = 0; priority_queue, greater

> q; tree.resize(V-1); q.push(P(0, s)); while (!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if (dist[v] < p.first) continue; for (auto e : edges[v]) { if (dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; q.push(P(dist[e.to], e.to)); tree[e.to-1] = e.id+1; } } } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); ll n,m; cin >> n >> m; vi c; rep(i,m){ int a,b; cin >> a >> b; c.push_back(a); c.push_back(b); } c.push_back(1); c.push_back(n); auto cmp = compress(c); int len = *max_element(all(cmp))+1; vv to(len); rep(i,m){ ll cst = c[i*2+1]*2-c[i*2]*2-1; to[cmp[i*2]].push_back({cmp[i*2+1], cst}); to[cmp[i*2+1]].push_back({cmp[i*2], cst}); } sort(all(c)); c.erase(unique(all(c)),c.end()); rep(i,sz(c)-1){ ll cst = (c[i+1]-c[i])*2; to[i].push_back({i+1,cst}); to[i+1].push_back({i,cst}); } dijkstra(to,sz(c),0); cout << dist[sz(c)-1] << endl; }