#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long from, to, cost;
  edge(long long _a, long long _b, long long _c) : from(_a), to(_b), cost(_c){};
};
using pll = pair<long long, long long>;
const long long INF = 1e17;
void dijkstra(int s, vector<long long> &d, vector<vector<pll>> &G) {
  priority_queue<pll, vector<pll>, greater<pll>> que;
  fill((d).begin(), (d).end(), INF);
  d[s] = 0;
  que.push(make_pair(0, s));
  while (!que.empty()) {
    pll p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) continue;
    for (int i = 0; i < G[v].size(); ++i) {
      pll e = G[v][i];
      if (d[e.first] > d[v] + e.second) {
        d[e.first] = d[v] + e.second;
        que.push(pll(d[e.first], e.first));
      }
    }
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<long long, pll>> kukan(M);
  vector<long long> s(M);
  vector<vector<pll>> G(M + 2, vector<pll>());
  for (int(i) = 0; (i) < (M); (i)++) {
    long long a, b, c;
    cin >> a >> b >> c;
    kukan[i] = make_pair(a, make_pair(b, c));
    s[i] = a;
  }
  sort((kukan).begin(), (kukan).end());
  vector<long long> dp(N + 1, INF);
  dp[1] = 0;
  for (int i = 0; i < M; i++) {
    for (int j = kukan[i].first + 1; j <= kukan[i].second.first; j++) {
      dp[j] = min(dp[j], dp[kukan[i].first] + kukan[i].second.second);
    }
  }
  if (dp[N] == INF)
    cout << -1 << endl;
  else
    cout << dp[N] << endl;
  return 0;
}
