#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct edge {
  int to, cost;
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<edge> G[N];
  for (ll i = (0); i < (M); i++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    edge e;
    e.to = r, e.cost = c;
    G[l].push_back(e);
  }
  for (ll i = (0); i < (N - 1); i++) {
    edge e;
    e.to = i, e.cost = 0;
    G[i + 1].push_back(e);
  }
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  vector<int> d(N, 1e9);
  d[0] = 0;
  q.push(pair<int, int>(0, 0));
  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first) continue;
    for (auto e : G[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        q.push(pair<int, int>(d[e.to], e.to));
      }
    }
  }
  if (d[N - 1] >= 1e9) d[N - 1] = -1;
  cout << d[N - 1] << "\n";
  return 0;
}
