#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long to, cost;
};
long long V;
vector<edge> G[10 * 10 * 10 * 10 * 10 * 5];
long long d[10 * 10 * 10 * 10 * 10 * 5];
void dijkstra(long long s) {
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      que;
  fill(d, d + V, 1e+12);
  d[s] = 0;
  que.push(pair<long long, int>(0, s));
  while (!que.empty()) {
    pair<long long, int> p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first) {
      continue;
    }
    for (long long i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pair<long long, int>(d[e.to], e.to));
      }
    }
  }
}
int main() {
  long long M;
  cin >> V >> M;
  for (long long i = 0; i < M; i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    struct edge a;
    a.to = r;
    a.cost = c;
    G[l].push_back(a);
  }
  for (long long i = V - 1; i > 0; i--) {
    long long l, r, c;
    struct edge x;
    x.to = i - 1;
    x.cost = 0;
    G[i].push_back(x);
  }
  dijkstra(0);
  if (d[V - 1] == 1e+12) {
    d[V - 1] = -1;
  }
  cout << d[V - 1] << endl;
}
