#include <bits/stdc++.h>
using i64 = std::int64_t;
struct Edge {
  int l, r;
  i64 c;
  bool operator<(const Edge& e) const { return l < e.l; }
};
struct State {
  int pos;
  i64 costs;
  int hasUsed;
  bool operator<(const State& s) const { return costs > s.costs; }
};
int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<Edge> edges(m);
  for (auto&& e : edges) std::cin >> e.l >> e.r >> e.c;
  std::sort(edges.begin(), edges.end());
  std::vector<int> uppers(n + 1);
  for (int i = 0; i < n + 1; ++i)
    uppers[i] = static_cast<int>(
        std::upper_bound(edges.begin(), edges.end(), Edge{i, 0, 0ll}) -
        edges.begin());
  std::vector<bool> hasVisited(n + 1);
  std::vector<i64> edgeMin(m, std::numeric_limits<i64>::max());
  std::priority_queue<State> pq;
  pq.push({1, 0ll, 0});
  while (!pq.empty()) {
    const auto cur = pq.top();
    pq.pop();
    if (cur.pos == n) {
      std::cout << cur.costs << std::endl;
      return 0;
    }
    if (hasVisited[cur.pos]) continue;
    hasVisited[cur.pos] = true;
    for (int i = cur.hasUsed; i < uppers[cur.pos]; ++i) {
      auto& e = edges[i];
      auto newCost = cur.costs + e.c;
      if (e.r > cur.pos && !hasVisited[e.r] && newCost < edgeMin[i]) {
        edgeMin[i] = newCost;
        pq.push({e.r, newCost, uppers[cur.pos]});
      }
    }
  }
  std::cout << -1 << std::endl;
}
