#include <bits/stdc++.h>
using namespace std;
const long long inf = 174417441744;
pair<pair<long long, long long>, long long> mk_P(long long a, long long b,
                                                 long long c) {
  return pair<pair<long long, long long>, long long>(
      pair<long long, long long>(a, b), c);
}
struct segtree {
 private:
  vector<long long> node;
  long long siz;

 public:
  segtree(int n) {
    siz = 1;
    while (siz <= n) siz *= 2;
    node.resize(siz * 2 + 1, inf);
  }
  void upd(long long pos, long long x) {
    pos += siz - 1;
    node[pos] = x;
    while (pos > 0) {
      pos = (pos - 1) / 2;
      node[pos] = min(node[pos * 2 + 1], node[pos * 2 + 2]);
    }
  }
  long long get_min(long long a, long long b, long long pos, long long l,
                    long long r) {
    if (a <= l && r <= b) return node[pos];
    if (r <= a || b <= l) return inf;
    long long mid = (l + r) / 2;
    long long lb = get_min(a, b, pos * 2 + 1, l, mid);
    long long rb = get_min(a, b, pos * 2 + 2, mid, r);
    return min(lb, rb);
  }
  long long min_q(long long a, long long b) { return get_min(a, b, 0, 0, siz); }
  void seg_debug() {
    cout << siz << "SIZ**" << endl;
    for (int i = 0; i < node.size(); i++)
      cout << (node[i] == inf ? -1 : node[i]) << " ";
    cout << endl;
  }
};
int main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<pair<long long, long long>, long long> > pv;
  for (int i = 0; i < m; i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    pv.push_back(mk_P(l, r, c));
  }
  sort(pv.begin(), pv.end());
  segtree seg = segtree(n + 5);
  for (int i = 2; i <= n; i++) seg.upd(i, inf);
  seg.upd(1, 0);
  for (int i = 0; i < m; i++) {
    pair<pair<long long, long long>, long long> p = pv[i];
    long long l = p.first.first, r = p.first.second, c = p.second;
    long long min_v = seg.min_q(l, r + 1);
    long long now_v = seg.min_q(r, r + 1);
    seg.upd(r, min(now_v, min_v + c));
  }
  cout << (seg.min_q(n, n + 1) == inf ? -1 : seg.min_q(n, n + 1)) << endl;
}
