#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1e9;
const long long IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename Monoid, typename Operator>
struct SegmentTree {
 private:
  typedef function<Monoid(Monoid, Monoid)> F;
  typedef function<Monoid(Monoid, Operator)> G;
  int n;
  F f;
  G g;
  Monoid id;
  vector<Monoid> dat;

 public:
  SegmentTree(int n_, F f, G g, Monoid id) {
    this->f = f;
    this->g = g;
    this->id = id;
    init(n_);
  }
  void init(int n_) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    dat.resize(2 * n - 1, id);
  }
  void build(int n_, vector<Monoid> v) {
    for (long long i = 0; i < n_; ++i) {
      dat[i + n - 1] = v[i];
    }
    for (int i = n - 2; i >= 0; i--) {
      dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]);
    }
  }
  void update(int i, Operator a) {
    i += n - 1;
    dat[i] = g(dat[i], a);
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]);
    }
  }
  Monoid query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) {
      return id;
    }
    if (a <= l && r <= b) {
      return dat[k];
    }
    Monoid vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    Monoid vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
  Monoid query(int a, int b) { return query(a, b, 0, 0, n); }
};
int main() {
  long long N, M;
  cin >> N >> M;
  vector<vector<long long>> G(M, vector<long long>(3));
  for (long long i = 0; i < M; ++i) {
    cin >> G[i][0] >> G[i][1] >> G[i][2];
    G[i][0]--, G[i][1]--;
  }
  sort(G.begin(), G.end());
  vector<long long> ans(N, IINF);
  ans[0] = 0;
  SegmentTree<long long, long long> seg(
      N, [](long long a, long long b) { return min(a, b); },
      [](long long a, long long b) { return b; }, IINF);
  seg.build(N, ans);
  for (long long i = 0; i < M; ++i) {
    long long a = G[i][0], b = G[i][1];
    long long c = seg.query(a, N);
    if (c == IINF) {
      cout << -1 << endl;
      return 0;
    }
    if (c + G[i][2] < seg.query(b, b + 1)) seg.update(b, c + G[i][2]);
  }
  cout << seg.query(N - 1, N) << endl;
}
