#include <bits/stdc++.h>   using namespace std;   int N, M;   vector<pair<int, int> > V;   int main(int argc, const char *argv[]) {     cin >> N >> M;     for (int i = 0, x, y; i < M; i++) {       cin >> x >> y;       V.push_back(pair<int, int>(x, y));     }     sort(V.begin(), V.end());     vector<pair<int, int> > currI;     currI.push_back(pair<int, int>(1, 1));     int currRow = 0;     for (int i = 0; i < V.size();) {       pair<int, int> p = V[i];       vector<pair<int, int> > newI;       if (p.first > currRow + 1 && currI.size()) {         int left = currI[0].first;         currI.clear();         currI.push_back(pair<int, int>(left, N));       }       currRow = p.first;       vector<pair<int, int> > temp;       int last = 0;       while (i < V.size() && V[i].first == p.first) {         temp.push_back(pair<int, int>(last + 1, V[i].second - 1));         last = V[i].second;         i++;       }       temp.push_back(pair<int, int>(last + 1, N));       int j = 0;       for (auto x : temp) {         while (j < currI.size() && currI[j].second < x.first) j++;         if (j < currI.size()) {           int x1 = max(x.first, currI[j].first), x2 = x.second;           if (x2 >= x1) {             newI.push_back(pair<int, int>(x1, x2));           }         }       }       currI = newI;     }     bool poss = false;     if (currI.size() > 0) {       pair<int, int> l = currI[currI.size() - 1];       poss = (currRow < N && l.second <= N);       poss = poss || (currRow == N && l.second == N);     }     if (poss) {       cout << 2 * (N - 1) << endl;     } else {       cout << -1 << endl;     }     return 0;   }   