#include #include using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i; using vvi = vector>; template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline void drop(T x) { cout << x << endl; exit(0); } template void vout(vector v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; template map coord_comp(set s) { int a = 0; map m; for (auto u : s) m[u] = ++a; return m; } int main() { lint N, M; cin >> N >> M; vi v(M), w(M); rep(i, M) cin >> v[i] >> w[i]; lint a=0, b=0, c=0, x, y, z; vvi G(2*M+1); set s; rep(i, M) { s.insert(v[i]); s.insert(w[i]); } map m = coord_comp(s); rep(i, M) { G[m[v[i]]].pb(m[w[i]]); } vi dp(2*M+1); rep(i, 2*M) { chmax(dp[i+1], dp[i]); rep(j, G[i].size()) { chmax(dp[G[i][j]], dp[i]+1); } } std::cout << 2*N-2-dp[2*M] << '\n'; }