#include <bits/stdc++.h>
using namespace std;
class Edge {
 public:
  int from;
  int to;
  int cost;
  Edge(int _from, int _to, int _cost) {
    from = _from;
    to = _to;
    cost = _cost;
  }
};
bool has_smaller_from(const Edge& a, const Edge& b) { return a.from < b.from; }
int main() {
  int nnode, nedge;
  cin >> nnode >> nedge;
  vector<Edge> edge;
  for (int i = 0; i < nedge; i++) {
    int from, to, cost;
    cin >> from >> to >> cost;
    edge.emplace_back(from, to, cost);
  }
  sort(edge.begin(), edge.end(), has_smaller_from);
  map<int, int> d;
  d[1] = 0;
  for (const auto& e : edge) {
    auto it = d.lower_bound(e.from);
    if (it == d.end()) {
      cout << "-1\n";
      return 0;
    }
    int cost = it->second + e.cost;
    vector<decltype(it)> to_be_erased;
    for (it = next(it, 1); it != d.end(); it++) {
      if (e.to < it->first) {
        break;
      }
      if (it->second < cost) {
        break;
      }
      to_be_erased.push_back(it);
    }
    for (auto i : to_be_erased) {
      d.erase(i);
    }
    if (it == d.end()) {
      d[e.to] = cost;
    } else {
      auto iter = d.find(e.to);
      if (iter == d.end()) {
        d[e.to] = cost;
      } else {
        d[e.to] = min(d[e.to], cost);
      }
    }
  }
  auto it = d.find(nnode);
  if (it == d.end()) {
    cout << "-1\n";
  } else {
    cout << it->second << '\n';
  }
  return 0;
}
