#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
int a[300010], f[300010];
struct lne {
  int l, r, c;
};
multiset<int> s;
vector<lne> in[300010], out[300010];
inline void prework() {
  scanf("%d%d", &n, &m);
  int l, r, c;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &l, &r, &c);
    in[l].push_back(lne{l, r, c});
    out[r].push_back(lne{l, r, c});
  }
}
inline void mainwork() {
  f[1] = 0;
  for (auto d : in[1]) s.insert(d.c);
  for (int i = 2; i <= n; i++) {
    if (s.begin() == s.end()) {
      ans = -1;
      return;
    }
    f[i] = (*s.begin());
    for (auto d : in[i]) s.insert(f[i] + d.c);
    for (auto d : out[i]) s.erase(s.lower_bound(f[d.l] + d.c));
  }
  ans = f[n];
}
inline void print() { printf("%d", ans); }
int main() {
  int t = 1;
  for (int i = 1; i <= t; i++) {
    prework();
    mainwork();
    print();
  }
  return 0;
}
