#include <bits/stdc++.h>
using namespace std;
using ll = __int64_t;
using lll = __int128_t;
const ll longinf = 1ll << 60;
const int inf = (1 << 30) - 1;
const ll mod = 1e9 + 7;
vector<pair<ll, ll>> nn[100001];
int main(void) {
  int n, m;
  cin >> n >> m;
  tuple<ll, ll, ll> queri[m];
  for (ll i = (ll)0ll; i < (ll)m; i++) {
    ll l, r, c;
    cin >> l >> r >> c;
    --l, --r;
    tuple<ll, ll, ll> tup;
    tup = make_tuple(c, l, r);
    queri[i] = tup;
  }
  sort(queri, queri + m);
  for (ll i = (ll)0ll; i < (ll)m; i++) {
    ll c = get<0>(queri[i]);
    ll l = get<1>(queri[i]);
    ll r = get<2>(queri[i]);
    for (int pos = l; pos < r; pos++) {
      if (!nn[pos].size()) {
        nn[pos].push_back({c, r});
      } else {
        pos = nn[pos][0].second - 1;
      }
    }
  }
  ll dist[n];
  fill(dist, dist + n, longinf);
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
  pq.push({0, 0});
  while (pq.size()) {
    pair<ll, ll> pos = pq.top();
    pq.pop();
    ll posd = pos.first;
    ll posn = pos.second;
    if (dist[posn] < longinf) {
      continue;
    } else {
      dist[posn] = posd;
    }
    ll nsize = nn[posn].size();
    for (int k = 0; k < nsize; k++) {
      pair<ll, ll> nnode = nn[posn][k];
      ll nposc = nnode.first;
      ll nposn = nnode.second;
      ll nposd = posd + nposc;
      pq.push({nposd, nposn});
    }
  }
  if (dist[n - 1] < longinf) {
    cout << dist[n - 1] << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
