#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long to, cost;
};
long long V;
vector<edge> G[101010];
long long d[101010];
void dikstra(long long s) {
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      que;
  for (long long i = 0; i < V; i++) d[i] = 1000000000;
  d[s] = 0;
  que.push(pair<long long, long long>(0, s));
  while (!que.empty()) {
    pair<long long, long long> 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, long long>(d[e.to], e.to));
      }
    }
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long M;
  cin >> V >> M;
  for (long long i = 0; i < M; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edge e = {b, c};
    G[a].push_back(e);
  }
  for (long long i = V - 1; i > 0; i--) {
    edge e = {i - 1, 0};
    G[i].push_back(e);
  }
  dikstra(0);
  if (d[V - 1] == 1000000000) d[V - 1] = -1;
  cout << d[V - 1] << endl;
  return 0;
}
