#include <bits/stdc++.h>
using namespace std;
const int maxn = 100020;
const int maxk = 350;
const int MOd = 1e9 + 7;
const int K = 320;
int a, b, e[maxn], segment[1 << 20], lazy[1 << 20], n;
pair<int, int> ar[maxn];
map<int, int> mp;
vector<int> w[maxn * 4];
void ind(int k, int b) {
  if (lazy[k]) {
    if (k < n) {
      lazy[k + k] = lazy[k];
      lazy[k + k + 1] = lazy[k];
    }
    if (lazy[k] == 1)
      segment[k] = b;
    else
      segment[k] = 1e9;
    lazy[k] = 0;
  }
}
int update(int k, int b, int s, int x, int y, int m) {
  ind(k, b);
  if (x > s || b > y) return segment[k];
  if (x <= b && s <= y) {
    lazy[k] = m;
    ind(k, b);
    return segment[k];
  }
  return segment[k] = min(update(k + k, b, (b + s) / 2, x, y, m),
                          update(k + k + 1, (b + s) / 2 + 1, s, x, y, m));
}
int find(int k, int b, int s, int x, int y) {
  ind(k, b);
  if (x > s || b > y) return 1e9;
  if (x <= b && s <= y) return segment[k];
  return min(find(k + k, b, (b + s) / 2, x, y),
             find(k + k + 1, (b + s) / 2 + 1, s, x, y));
}
int main() {
  scanf("%d %d", &a, &b);
  int ans = a + a - 2;
  for (int i = 1, j, k; i <= b; i++) {
    scanf("%d %d", &j, &k);
    ar[i].first = j;
    ar[i].second = k;
    mp[ar[i].first] = 1;
    if (ar[i].first > 1) mp[ar[i].first - 1] = 1;
    mp[ar[i].second] = 1;
    if (ar[i].second > 1) mp[ar[i].second - 1] = 1;
  }
  mp[1] = mp[a] = 1;
  a = 0;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
    it->second = ++a;
  n = 1;
  while (n < a) n <<= 1;
  for (int i = 1; i <= b; i++) {
    w[mp[ar[i].first]].push_back(mp[ar[i].second]);
  }
  for (int i = n; i < n + n; i++) segment[i] = 1e9;
  segment[mp[1] + n - 1] = mp[1];
  for (int i = n - 1; i >= 1; i--)
    segment[i] = min(segment[i + i], segment[i + i + 1]);
  for (int i = 1, k = 0; i <= a; i++) {
    int back = 1;
    w[i].push_back(a + 1);
    sort(w[i].begin(), w[i].end());
    for (int j = 0; j < w[i].size(); j++) {
      int t = find(1, 1, n, back, w[i][j] - 1);
      if (t > w[i][j]) t = w[i][j];
      if (back <= 1) back = 2;
      update(1, 1, n, back - 1, t - 1, 2);
      if (t < w[i][j]) update(1, 1, n, t, w[i][j] - 1, 1);
      back = w[i][j] + 1;
    }
  }
  int t = find(1, 1, n, a, a);
  if (t <= a)
    printf("%d\n", ans);
  else
    printf("-1\n");
  return 0;
}
