#include <bits/stdc++.h>
using i64 = long long;
int main() {
  constexpr i64 inf = std::numeric_limits<i64>::max() / 2;
  int n, m;
  std::cin >> n >> m;
  std::vector<std::tuple<int, int, i64, int>> evs;
  for (int i = 0; i < m; i++) {
    int l, r;
    i64 c;
    std::cin >> l >> r >> c;
    evs.emplace_back(l - 1, 0, c, i);
    evs.emplace_back(r - 1, 1, c, i);
  }
  std::sort(evs.begin(), evs.end());
  std::vector<i64> dist(n, inf), rr(n);
  dist[0] = 0;
  auto it = evs.begin();
  std::set<std::pair<i64, int>> ss;
  for (int i = 0; i < n; i++) {
    if (i) {
      if (ss.empty()) {
        std::cout << -1 << std::endl;
        return 0;
      }
      dist[i] = ss.begin()->first;
    }
    while (it != evs.end() && std::get<0>(*it) == i) {
      int index = std::get<3>(*it);
      if (std::get<1>(*it) == 0) {
        i64 cc = dist[i] + std::get<2>(*it);
        ss.emplace(cc, index);
        rr[index] = cc;
      } else {
        ss.erase(ss.lower_bound(std::make_pair(rr[index], index)));
      }
      it++;
    }
  }
  std::cout << dist[n - 1] << std::endl;
  return 0;
}
