#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to;
  int cost;
};
using P = pair<int, int>;
int V;
vector<edge> G[1000000];
int d[1000000];
void dijkstra(int 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();
    int v = p.second;
    if (d[v] < p.first) continue;
    for (int 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;
  int E;
  cin >> E;
  for (int i = 0; i < E; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    for (int a_ = a; a_ <= b; a_++) {
      for (int b_ = a_ + 1; b_ <= b; b_++) {
        edge e = {b_, c};
        G[a_].push_back(e);
      }
    }
  }
  dijkstra(0);
  if (d[V - 1] == 1e+9) {
    cout << -1 << endl;
  } else {
    cout << d[V - 1] << endl;
  }
  return 0;
}
