#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chmin(T &a, T b) {
  if (a > b) a = b;
}
template <typename T>
void chmax(T &a, T b) {
  if (a < b) a = b;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
const long long INF = 1e18;
template <typename T>
struct SegmentTree {
  int n, width;
  vector<T> dat;
  void init(int x) {
    n = x;
    width = 1;
    while (width < n) width *= 2;
    dat.assign(2 * width - 1, INF);
  }
  void update(int i, T x) {
    i += width - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return INF;
    if (a <= l && r <= b) return dat[k];
    T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  T query(int l, int r) { return query(l, r, 0, 0, width); }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<long long> > l2r(n), l2c(n);
  for (int i = 0; i < m; ++i) {
    int l = in() - 1, r = in(), c = in();
    l2r[l].push_back(r);
    l2c[l].push_back(c);
  }
  SegmentTree<long long> seg;
  seg.init(n);
  seg.update(0, 0);
  for (int i = 0; i < n - 1; ++i) {
    long long x = seg.query(i, n);
    if (x == INF) {
      cout << -1 << endl;
      return 0;
    }
    for (int j = 0; j < l2r[i].size(); ++j) {
      int r = l2r[i][j] - 1;
      long long c = l2c[i][j];
      if (seg.query(r, r + 1) > x + c) {
        seg.update(r, x + c);
      }
    }
  }
  long long ans = seg.query(n - 1, n);
  cout << ans << endl;
  return 0;
}
