#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
long long int power(long long int a, long long int b, long long int m) {
  if (b == 0) return 1;
  if (b == 1) return a % m;
  long long int t = power(a, b / 2, m) % m;
  t = (t * t) % m;
  if (b & 1) t = ((t % m) * (a % m)) % m;
  return t;
}
long long int modInverse(long long int a, long long int m) {
  return power(a, m - 2, m);
}
long long int n, m;
vector<pair<long long int, long long int>> vc[3000005];
long long int nxt[3000005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k, l;
  cin >> n >> m;
  vector<pair<long long int, pair<long long int, long long int>>> tmp;
  long long int a, b, c;
  for (i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    tmp.push_back({c, {a, b}});
  }
  sort(tmp.begin(), tmp.end());
  for (i = 1; i <= n; i++) {
    nxt[i] = i + 1;
  }
  for (auto it : tmp) {
    for (i = it.second.first; i < it.second.second; i++) {
      for (j = nxt[i]; j <= it.second.second; j++) {
        vc[i].push_back({j, it.first});
        vc[j].push_back({i, it.first});
      }
      nxt[i] = it.second.second + 1;
    }
  }
  priority_queue<pair<long long int, long long int>,
                 vector<pair<long long int, long long int>>,
                 greater<pair<long long int, long long int>>>
      pq;
  vector<long long int> dt(n + 1, 1e10);
  pq.push({0, 1});
  dt[1] = 0;
  while (pq.size() > 0) {
    long long int u = pq.top().second;
    pq.pop();
    for (long long int i = 0; i < vc[u].size(); i++) {
      long long int v = vc[u][i].first;
      long long int w = vc[u][i].second;
      if (dt[v] > dt[u] + w) {
        dt[v] = dt[u] + w;
        pq.push({dt[v], v});
      }
    }
  }
  if (dt[n] >= 1e10) {
    cout << "-1"
         << "\n";
  } else
    cout << dt[n];
  return 0;
}
