#include #include using namespace std; int main() { int n, m; cin >> n >> m; map mp; int a[100005], b[100005]; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; if (!mp.count(a[i])) { mp[a[i]] = 0; } mp[b[i]] = max(mp[b[i]], a[i]); } map dp; dp[0] = -1; int last = 0; for (auto itr = mp.begin(); itr != mp.end(); itr++) { last = max(last, dp[itr->second] + 1); dp[itr->first] = last; } cout << n * 2 - 2 - last << endl; }