func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXM = 101000;   pair<int, int> a[MAXM];   vector<pair<int, int> > b[MAXM << 1];   vector<int> c[MAXM << 1], d[MAXM << 1];   int main() {     int n, m, i, j, k, x, y;     int f, l, r, top;     while (scanf( %d%d , &n, &m) != EOF) {       for (i = 0; i < m; i++) {         scanf( %d%d , &x, &y);         a[i] = make_pair(x, y);       }       sort(a, a + m);       for (i = 0; i + n - 1 < m; i++)         if (a[i].first == a[i + n - 1].first) break;       if (i + n - 1 < m && a[i].first == a[i + n - 1].first) {         puts( -1 );         continue;       }       if (a[0].first == 1) {         b[0].push_back(make_pair(1, a[0].second - 1));         f = 1;         top = 0;       } else {         b[0].push_back(make_pair(1, n));         b[1].push_back(make_pair(1, a[0].second - 1));         f = a[0].first;         top = 1;       }       for (i = 1; i < m; i++) {         if (a[i].first > f) {           if (a[i - 1].second != n)             b[top].push_back(make_pair(a[i - 1].second + 1, n));           top++;           if (a[i].first - f > 1) b[top++].push_back(make_pair(1, n));           if (a[i].second > 1) b[top].push_back(make_pair(1, a[i].second - 1));           f = a[i].first;         } else {           if (a[i].second - a[i - 1].second > 1)             b[top].push_back(make_pair(a[i - 1].second + 1, a[i].second - 1));         }       }       if (a[m - 1].second != n)         b[top].push_back(make_pair(a[m - 1].second + 1, n));       top++;       if (a[m - 1].first != n) b[top++].push_back(make_pair(1, n));       c[0].push_back(b[0][0].first);       d[0].push_back(b[0][0].second);       for (i = 1; i < top; i++) {         f = b[i].size();         for (j = 0; j < f; j++) {           k = lower_bound(d[i - 1].begin(), d[i - 1].end(), b[i][j].first) -               d[i - 1].begin();           if (k >= 0 && k < d[i - 1].size() && d[i - 1][k] >= b[i][j].first &&               c[i - 1][k] <= b[i][j].second) {             c[i].push_back(max(b[i][j].first, c[i - 1][k]));             d[i].push_back(b[i][j].second);           }         }         if (c[i].size() == 0) break;       }       if (d[top - 1].size() != 0 && d[top - 1][d[top - 1].size() - 1] == n)         printf( %d n , 2 * n - 2);       else         puts( -1 );     }     return 0;   }   