func_code_string: #include <bits/stdc++.h>   using namespace std;   struct state {     int x, y;   } bl[100005], rec[100005], cur[100005];   long long n, m;   bool cmp(const state& a, const state& b) {     if (a.y == b.y) return a.x < b.x;     return a.y < b.y;   }   bool funct() {     int counter = 1;     rec[0].x = 1, rec[0].y = 1;     for (int i = 0; i < (int)m; i++) {       if (bl[i].y != bl[i - 1].y + 1) {         counter = 1;         rec[0].y = n;       }       int k = i + 1;       int tmp = 0;       int c = 0;       for (k = i + 1; bl[k].y == bl[i].y; k++)         ;       k = k - 1;       for (int j = i; j <= k; j++) {         if (bl[j].x > tmp + 1) {           cur[c].x = tmp + 1;           cur[c].y = bl[j].x - 1;           c++;         }         tmp = bl[j].x;       }       if (n > tmp) {         cur[c].x = tmp + 1;         cur[c].y = n;         c++;       }       int t = 0;       for (int i = 0; i < counter; i++) {         while (cur[t].x <= rec[i].y && t < c) {           if (cur[t].y >= rec[i].x) {             cur[t].x = max(cur[t].x, rec[i].x);           } else {             cur[t].x = cur[t].y = -1;           }           t++;         }         if (t >= c) break;       }       counter = 0;       for (int i = 0; i < t; i++)         if (cur[i].x != -1 && cur[i].y != -1) {           rec[counter].x = cur[i].x;           rec[counter].y = cur[i].y;           counter++;         }       if (counter == 0) return false;       i = k;     }     if (bl[m - 1].y != n) {       return true;     }     if (rec[counter - 1].y == n) {       return true;     } else {       return false;     }   }   int main() {     cin >> n >> m;     int xx;     int yy;     for (int i = 0; i < (int)m; i++) {       cin >> xx >> yy;       bl[i].x = xx;       bl[i].y = yy;     }     sort(bl, bl + m, cmp);     if (funct()) {       cout << 2 * n - 2 << endl;     } else {       printf( -1 n );     }     return 0;   }   