/* -*- coding: utf-8 -*- * * 1995.cc: No.1995 CHIKA Road - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_M = 100000; const int MAX_K = MAX_M * 2 + 2; /* typedef */ typedef vector vi; /* global variables */ int as[MAX_M], bs[MAX_M], xs[MAX_K], dp[MAX_K]; vi nbrs[MAX_K]; /* subroutines */ inline void setmax(int &a, int b) { if (a < b) a = b; } /* main */ int main() { int n, m; scanf("%d%d", &n, &m); int k = 0; xs[k++] = 1, xs[k++] = n; for (int i = 0; i < m; i++) { scanf("%d%d", as + i, bs + i); xs[k++] = as[i], xs[k++] = bs[i]; } sort(xs, xs + k); k = unique(xs, xs + k) - xs; for (int i = 0; i < m; i++) { int ai = lower_bound(xs, xs + k, as[i]) - xs; int bi = lower_bound(xs, xs + k, bs[i]) - xs; nbrs[ai].push_back(bi); } for (int i = 0; i < k; i++) { if (i + 1 < n) setmax(dp[i + 1], dp[i]); for (auto j: nbrs[i]) setmax(dp[j], dp[i] + 1); } printf("%d\n", (n - 1) * 2 - dp[k - 1]); return 0; }