#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int dst, weight;
};
const int MAX_V = 1.0e5 + 10;
vector<Edge> G[MAX_V];
int V, M;
int L[MAX_V], R[MAX_V], C[MAX_V];
int d[MAX_V];
const int INF = 1.0e9;
void dijkstra(int s) {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(pair<int, int>(0, s));
  while (!que.empty()) {
    pair<int, int> 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.dst] > d[v] + e.weight) {
        d[e.dst] = d[v] + e.weight;
        que.push(pair<int, int>(d[e.dst], e.dst));
      }
    }
  }
}
int main() {
  cin >> V >> M;
  for (int i = 0; i < V - 1; i++) {
    Edge e = {i, 0};
    G[i + 1].push_back(e);
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    Edge e_a = {b - 1, c};
    G[a - 1].push_back(e_a);
  }
  dijkstra(0);
  if (d[V - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[V - 1] << endl;
  return 0;
}
