#include <bits/stdc++.h>
using namespace std;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
long long nmax = 100001;
long long bk = 1000;
vll dp(nmax, 2000000000000000000LL);
vll mi(100, 2000000000000000000LL);
long long minim(long long s, long long t) {
  long long ret = 2000000000000000000LL;
  long long now = s;
  while (now <= t) {
    if (now % bk == 0) {
      if (now + bk <= t) {
        ret = min(ret, mi[now / bk]);
        now += bk;
      } else {
        ret = min(ret, dp[now]);
        now++;
      }
    } else {
      ret = min(ret, dp[now]);
      now++;
    }
  }
  return ret;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  vector<pair<pair<long long, long long>, long long> > rlc(m);
  for (int i = 0; i < m; ++i) {
    long long r, l, c;
    cin >> l >> r >> c;
    l--;
    r--;
    rlc[i] = make_pair(make_pair(r, l), c);
  }
  dp[0] = 0;
  sort((rlc).begin(), (rlc).end());
  for (int i = 0; i < m; ++i) {
    auto pp = rlc[i];
    long long pr = (pp.first).first;
    long long pl = (pp.first).second;
    long long pc = pp.second;
    long long tp = minim(pl, pr - 1);
    if (tp == 2000000000000000000LL) {
    } else {
      dp[pr] = min(dp[pr], tp + pc);
      mi[pr / bk] = min(mi[pr / bk], dp[pr]);
    }
  }
  if (dp[n - 1] == 2000000000000000000LL) {
    cout << -1 << endl;
  } else {
    cout << dp[n - 1] << endl;
  }
  return 0;
}
