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