#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
struct edge {
  ll l, r, c;
};
bool comp(const edge& e1, const edge& e2) { return e1.l < e2.l; }
const ll inf = 1e18;
int B = 320;
vector<ll> lazy(B, -1), a(100010, inf);
void update(ll s, ll t, ll x) {
  ll st = s / B, gt = t / B;
  if (lazy[st] != -1) {
    for (ll i = st * B; i < (st + 1) * B; i++) a[i] = min(a[i], lazy[st]);
    lazy[st] = -1;
  }
  if (lazy[gt] != -1) {
    for (ll i = gt * B; i < (gt + 1) * B; i++) a[i] = min(a[i], lazy[gt]);
    lazy[gt] = -1;
  }
  if (st == gt) {
    for (ll i = s; i <= t; i++) a[i] = min(a[i], a[s] + x);
  } else {
    for (ll i = s; i < (st + 1) * B; i++) a[i] = min(a[i], a[s] + x);
    for (ll i = gt * B; i <= t; i++) a[i] = min(a[i], a[s] + x);
    for (ll i = st + 1; i < gt; i++) {
      lazy[i] = min(lazy[i], a[s] + x);
    }
  }
}
ll find(ll s) {
  ll st = s / B;
  if (lazy[st] != -1) {
    for (ll i = st * B; i < (st + 1) * B; i++) a[i] = min(a[i], lazy[st]);
    lazy[st] = -1;
  }
  return a[s];
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  ll l, r, c;
  vector<edge> ee;
  for (int i = 0; i < m; i++) {
    cin >> l >> r >> c;
    l--, r--;
    ee.push_back(edge{l, r, c});
  }
  sort(ee.begin(), ee.end(), comp);
  a[0] = 0;
  for (int i = 0; i < m; i++) {
    l = ee[i].l, r = ee[i].r, c = ee[i].c;
    update(l, r, c);
  }
  ll res = a[n - 1];
  if (res == inf) res = -1;
  cout << res << endl;
  return 0;
}
