#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool chmax(T &a, T b) {     return a < b ? (a = b, 1) : 0;   }   template <class T>   inline bool chmin(T &a, T b) {     return a > b ? (a = b, 1) : 0;   }   int main() {     int n, m;     cin >> n >> m;     map<int, vector<int>> obs;     int left = n;     while (m--) {       int x, y;       cin >> x >> y;       if (x == 1)         chmin(left, y - 1);       else         obs[x].emplace_back(y);     }     vector<pair<int, int>> segs = {{1, left}};     int prv = 1;     for (auto &p : obs) {       auto &v = p.second;       sort(begin(v), end(v));       if (p.first != prv + 1) {         segs.assign(1, {segs.begin()->first, n});       }       vector<pair<int, int>> segs2;       auto it = segs.begin();       int p_v = 0, sz_v = v.size();       int lst = -1;       while (it != segs.end() && p_v < sz_v) {         chmax(it->first, lst);         if (it->first > it->second) {           it++;           continue;         }         if (v[p_v] <= it->first) {           it->first += v[p_v++] == it->first;           continue;         }         segs2.emplace_back(it->first, v[p_v] - 1);         lst = v[p_v++] + 1;       }       if (p_v == sz_v) {         while (it != segs.end()) {           chmax(it->first, lst);           if (it->first <= it->second) break;           it++;         }         if (it != segs.end()) {           segs2.emplace_back(it->first, n);         }       }       if ((segs = segs2).empty()) return !(cout << -1 << endl);       prv = p.first;     }     if (prv < n) {       segs.assign(1, {segs.begin()->first, n});     }     cout << (segs.rbegin()->second == n ? (n - 1) * 2 : -1) << endl;     return 0;   }   