#include <bits/stdc++.h>   using namespace std;   const int INF = 2000000000;   struct pkt {     int k, r;   };   struct prze {     int k, r1, r2;   };   bool porownanie(pkt a, pkt b) {     if (a.k < b.k) return true;     if (a.k > b.k) return false;     if (a.r < b.r) return true;     return false;   }   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     vector<pkt> pkty(m);     for (int i = 0; i < m; ++i) {       cin >> pkty[i].r >> pkty[i].k;       --pkty[i].r;       --pkty[i].k;     }     sort(pkty.begin(), pkty.end(), porownanie);     vector<vector<prze> > zab;     for (int L = 0; L < m;) {       int P = L;       while (P < m && pkty[L].k == pkty[P].k) ++P;       --P;       vector<prze> t;       for (int l = L; l <= P;) {         int p = l;         while (p <= P && pkty[p].r - pkty[l].r == p - l) ++p;         --p;         t.push_back({pkty[l].k, pkty[l].r, pkty[p].r});         l = p + 1;       }       zab.push_back(t);       L = P + 1;     }     for (int i = 0; i < zab.size(); ++i) {       if (zab[i].size() == 1 && zab[i][0].r1 == 0 && zab[i][0].r2 == n - 1) {         cout <<  -1 ;         return 0;       }     }     vector<vector<prze> > doz;     if (zab[0][0].k != 0) {       vector<prze> t;       t.push_back({0, 0, n - 1});       doz.push_back(t);     }     for (int i = 0; i < zab.size(); ++i) {       if (i != 0) {         if (zab[i - 1][0].k != zab[i][0].k - 1) {           vector<prze> t;           t.push_back({zab[i][0].k - 1, 0, n - 1});           doz.push_back(t);         }       }       int ile_prze = zab[i].size();       vector<prze> t;       if (zab[i][0].r1 - 1 >= 0) t.push_back({zab[i][0].k, 0, zab[i][0].r1 - 1});       for (int j = 0; j < ile_prze - 1; ++j)         t.push_back({zab[i][0].k, zab[i][j].r2 + 1, zab[i][j + 1].r1 - 1});       if (zab[i][ile_prze - 1].r2 < n - 1)         t.push_back({zab[i][0].k, zab[i][ile_prze - 1].r2 + 1, n - 1});       doz.push_back(t);     }     if (zab.back()[0].k != n - 1) {       vector<prze> t;       t.push_back({n - 1, 0, n - 1});       doz.push_back(t);     }     zab.clear();     vector<vector<int> > lo(doz.size());     for (int i = 0; i < doz.size(); ++i)       for (int j = 0; j < doz[i].size(); ++j) lo[i].push_back(INF);     for (int i = 0; i < lo[0].size(); ++i) {       if (i == 0)         lo[0][i] = 0;       else         lo[0][i] = INF;     }     for (int i = 1; i < doz.size(); ++i) {       vector<int> g(doz[i].size(), INF);       int k = 0;       for (int j = 0; j < doz[i].size(); ++j) {         while (k < doz[i - 1].size() && doz[i - 1][k].r2 < doz[i][j].r1) ++k;         if (k < doz[i - 1].size()) g[j] = k;       }       vector<int> d(doz[i].size(), INF);       k = doz[i - 1].size() - 1;       for (int j = doz[i].size() - 1; j >= 0; --j) {         while (k >= 0 && doz[i - 1][k].r1 > doz[i][j].r2) --k;         if (k >= 0) d[j] = k;       }       for (int j = 0; j < doz[i].size(); ++j) {         if (d[j] != INF && g[j] != INF && g[j] <= d[j]) {           for (int k = g[j]; k <= d[j]; ++k) {             if (lo[i - 1][k] != INF) {               if (lo[i - 1][k] < doz[i][j].r1)                 lo[i][j] = min(lo[i][j], doz[i][j].r1);               else {                 if (lo[i - 1][k] <= doz[i][j].r2)                   lo[i][j] = min(lo[i][j], lo[i - 1][k]);               }             }           }         }       }     }     int x = doz.size();     int y = doz[x - 1].size();     if (doz[x - 1][y - 1].r2 == n - 1 && lo[x - 1][y - 1] != INF)       cout << 2 * (n - 1);     else       cout <<  -1 ;     return 0;   }