#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include // #include // #include // #include // #include #define int long long #define all(x) begin(x), end(x) using namespace std; // using namespace atcoder; // using mint = modint998244353; // using mint = modint1000000007; using ld = long double; using pii = pair; using vi = vector; using vvi = vector; using vvvi = vector; using vp = vector; using vvp = vector; using vs = vector; using vvc = vector>; void debug(vector a) { for (auto x : a) cout << x << ' '; cout << endl; } void debug(vector> a) { for (auto y : a) debug(y); } template inline bool chmax(T1 &a, T2 b) {return a < b and (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b and (a = b, true);} const int supl = LONG_LONG_MAX - 100; template vector compress(const vector& a) { vector cp = a; sort(cp.begin(), cp.end()); cp.erase(unique(cp.begin(), cp.end()), cp.end()); vector res(a.size()); for (int i = 0 ; i < a.size() ; i++) res[i] = lower_bound(cp.begin(), cp.end(), a[i]) - cp.begin(); return res; } void main_() { int n, m; cin >> n >> m; vp es(m); for (auto& [a, b] : es) cin >> a >> b; sort(all(es)); map comp; { set st; for (auto [a, b] : es) { st.emplace(a); st.emplace(b); } st.emplace(n); int cnt = 0; for (auto x : st) { comp[x] = cnt++; } } // cout << endl; // for (auto [x, y] : comp) cout << x << ' ' << y << endl; // vvi G(comp.size()); for (auto [a, b] : es) G[comp[a]].emplace_back(comp[b]); // // cout << endl; // debug(G); vi dp(comp.size()); for (int i = 0 ; i < comp.size() ; i++) { for (auto x : G[i]) chmax(dp[x], dp[i] + 1); if (i != n - 1) chmax(dp[i + 1], dp[i]); } cout << 2*(n - 1) - dp.back() << endl; } signed main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); main_(); return 0; }