#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } int main() { fast_io(); int n, m; cin >> n >> m; vector a(m), b(m), val; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; val.push_back(a[i]); val.push_back(b[i]); } val.push_back(1); val.push_back(n); sort(val.begin(), val.end()); val.erase(unique(val.begin(), val.end()), val.end()); int K = val.size(); vector>> g(K); for (int i = 0; i < K - 1; i++) { g[i].push_back({i + 1, 2 * (val[i + 1] - val[i])}); } for (int i = 0; i < m; i++) { int u = lower_bound(val.begin(), val.end(), a[i]) - val.begin(); int v = lower_bound(val.begin(), val.end(), b[i]) - val.begin(); g[u].push_back({v, 2 * b[i] - 2 * a[i] - 1}); } vector dp(K, 1e18); dp[0] = 0; for (int i = 0; i < K; i++) { for (auto [j, w] : g[i]) { dp[j] = min(dp[j], dp[i] + w); } } cout << dp[K - 1] << "\n"; }