#include <bits/stdc++.h>
using namespace std;
class Dijkstra {
 public:
  struct edge {
    long long to;
    long long cost;
  };
  long long vertex;
  vector<vector<edge>> graph;
  vector<long long> dist;
  vector<long long> hist;
  Dijkstra(long long n) {
    vertex = n;
    graph = vector<vector<edge>>(n);
    dist = vector<long long>(n);
    hist = vector<long long>(n, -1);
  };
  void dijkstra(long long s) {
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        que;
    fill(dist.begin(), dist.end(), 1LL << 60);
    dist[s] = 0;
    que.push(pair<int, int>(0, s));
    while (!que.empty()) {
      pair<int, int> p = que.top();
      que.pop();
      long long v = p.second;
      if (dist[v] < p.first) continue;
      for (long long i = 0; i < graph[v].size(); i++) {
        edge e = graph[v][i];
        if (dist[e.to] > dist[v] + e.cost &&
            !(dist[e.to] == 1LL << 60 && dist[v] == 1LL << 60)) {
          dist[e.to] = dist[v] + e.cost;
          hist[e.to] = v;
          que.push(pair<int, int>(dist[e.to], e.to));
        }
      }
    }
  }
  void addEdge(long long from, long long to, long long cost) {
    edge e;
    e.to = to;
    e.cost = cost;
    graph[from].push_back(e);
  }
};
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> l(m), r(m), c(m);
  for (long long i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
    r[i]--;
  }
  Dijkstra d(n);
  for (long long i = 0; i < m; i++) {
    d.addEdge(l[i], r[i], c[i]);
    d.addEdge(r[i], l[i], c[i]);
  }
  for (long long i = 0; i < n - 1; i++) {
    d.addEdge(i + 1, i, 0);
  }
  d.dijkstra(0);
  if (d.dist[n - 1] == 1LL << 60) {
    cout << -1 << endl;
  } else {
    cout << d.dist[n - 1] << endl;
  }
  return 0;
}
