#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using T = tuple<int, int, int>;
template <typename T>
struct Dijkstra {
  struct Edge {
    int to;
    T cost;
  };
  vector<int> prev;
  vector<vector<Edge>> g;
  Dijkstra(int n) : prev(n, -1), g(n) {}
  void addEdge(int u, int v, T w) {
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  vector<T> build(int s) {
    vector<T> dist(g.size(), -1);
    using Node = pair<T, int>;
    priority_queue<Node, vector<Node>, greater<Node>> pq;
    pq.push({dist[s] = 0, s});
    while (!pq.empty()) {
      auto d = pq.top().first;
      auto u = pq.top().second;
      pq.pop();
      if (dist[u] < d) continue;
      for (auto&& v : g[u]) {
        if (dist[v.to] < 0 || dist[v.to] > dist[u] + v.cost) {
          dist[v.to] = dist[u] + v.cost;
          prev[v.to] = u;
          pq.push({dist[v.to], v.to});
        }
      }
    }
    return dist;
  }
  vector<int> getPath(int t) {
    vector<int> path;
    for (; t != -1; t = prev[t]) path.push_back(t);
    reverse(begin(path), end(path));
    return path;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<T> ts(m);
  for (int i = 0; i < m; i++) {
    int l, r, c;
    cin >> l >> r >> c, l--, r--;
    ts[i] = make_tuple(l, r, c);
  }
  sort(begin(ts), end(ts));
  Dijkstra<ll> g(n);
  for (int i = 0; i < m; i++) {
    int li, ri, ci;
    tie(li, ri, ci) = ts[i];
    if (ri == n - 1) g.addEdge(i, n - 1, ci);
    for (int j = i + 1; j < m; j++) {
      int lj, rj, cj;
      tie(lj, rj, cj) = ts[j];
      if (ri < lj) break;
      g.addEdge(i, j, ci);
    }
  }
  auto d = g.build(0);
  cout << ((d[n - 1] >= 0) ? d[n - 1] : -1) << endl;
}
