#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long to;
  long long cost;
};
using P = pair<long long, long long>;
long long V;
vector<edge> G[100001];
long long d[100001];
void dijkstra(long long s) {
  priority_queue<P, vector<P>, greater<P> > que;
  fill(d, d + V, 1e+9);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P 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(P(d[e.to], e.to));
      }
    }
  }
}
int main() {
  cin >> V;
  long long E;
  cin >> E;
  for (long long i = 0; i < E; ++i) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    for (long long i = a; i <= b; i++) {
      edge e = {i, c};
      G[a].push_back(e);
    }
  }
  dijkstra(0);
  if (d[V - 1] != 1e+9)
    cout << d[V - 1] << endl;
  else
    cout << -1 << endl;
}
