#include <bits/stdc++.h>
const int MGN = 10;
const int ARY_SZ_MAX = (int)1e7;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psl = pair<string, ll>;
void dijkstra(vl& d, vvi& es, int s, map<pii, ll>& cost) {
  int N = es.size();
  d = vl(N, (LLONG_MAX / 2));
  d[s] = 0;
  priority_queue<pll, vector<pll>, greater<pll>> pq;
  pq.push(pll(d[s], s));
  vb vis(N, false);
  while (!pq.empty()) {
    pll p = pq.top();
    pq.pop();
    int i = p.second;
    vis[i] = true;
    for (int t : es[i]) {
      if (vis[t]) continue;
      if (p.first + cost[pii(i, t)] < d[t]) {
        d[t] = p.first + cost[pii(i, t)];
        pq.push(pll(d[t], t));
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vi L(M), R(M), C(M);
  for (int i = int(0); i < int(M); ++i) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--;
    R[i]--;
  }
  vvi es(N);
  map<pii, ll> cost;
  for (int i = int(0); i < int(M); ++i) {
    int l = L[i], r = R[i];
    if (cost.count(pii(l, r)) == 0) {
      es[l].push_back(r);
      cost[pii(l, r)] = (ll)C[i];
    } else {
      cost[pii(l, r)] = min(cost[pii(l, r)], (ll)C[i]);
    }
    for (int i = l; i < r; i++) {
      if (cost.count(pii(i + 1, i)) == 0) {
        es[i + 1].push_back(i);
        cost[pii(i + 1, i)] = 0LL;
      }
    }
  }
  vl dist;
  dijkstra(dist, es, 0, cost);
  ll ans = dist[N - 1] >= (LLONG_MAX / 2) ? -1 : dist[N - 1];
  cout << ans << endl;
  return 0;
}
