#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
constexpr i64 INF = 1e17;
struct Node {
  i64 l, r, c;
  bool operator<(const Node &obj) const {
    if (l == obj.l) return r < obj.r;
    return l < obj.l;
  }
};
struct State {
  i64 num, cost;
  bool operator<(const State &obj) const { return cost > obj.cost; }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<Node> v;
  v.push_back({0, 1, 0});
  for (int i = 0; i < m; ++i) {
    i64 l, r, c;
    cin >> l >> r >> c;
    v.push_back({l, r, c});
  }
  sort(v.begin(), v.end());
  vector<i64> mini(m + 1, INF);
  mini[0] = 0;
  priority_queue<State> que;
  que.push({0, 0});
  int rmax = 0;
  while (!que.empty()) {
    auto cur = que.top();
    que.pop();
    if (v[cur.num].r <= rmax) continue;
    rmax = cur.num;
    if (v[cur.num].r == n) {
      cout << cur.cost << endl;
      return 0;
    }
    for (int i = cur.num + 1; i <= m; ++i) {
      if (v[i].l > v[cur.num].r) break;
      if (v[i].l == v[cur.num].l) continue;
      i64 new_cost = cur.cost + v[i].c;
      if (new_cost >= mini[i]) continue;
      mini[i] = new_cost;
      que.push({i, new_cost});
    }
  }
  cout << -1 << endl;
  return 0;
}
