#include <bits/stdc++.h>
using namespace std;
const long long DEPTH = 17;
const long long INF = 1e+16;
struct RUQ {
  long long d[1 << (DEPTH + 1)];
  RUQ() {
    long long i;
    for (i = 0; i < (1 << (DEPTH + 1)); i++) d[i] = INF;
  }
  void update(long long l, long long r, long long x, long long a = 0,
              long long b = (1 << DEPTH), long long id = 0) {
    if (a >= r || b <= l) return;
    if (l <= a && b <= r) {
      d[id] = min(d[id], x);
      return;
    }
    update(l, r, x, a, (a + b) / 2, id * 2 + 1);
    update(l, r, x, (a + b) / 2, b, id * 2 + 2);
  }
  long long query(long long pos) {
    long long ret = INF;
    pos += (1 << DEPTH) - 1;
    ret = min(ret, d[pos]);
    while (pos > 0) {
      pos = (pos - 1) / 2;
      ret = min(ret, d[pos]);
    }
    return ret;
  }
};
long long n, m;
tuple<long long, long long, long long> tupl[100001];
long long l[100001], r[100001], c[100001];
RUQ ruq;
signed main() {
  long long i;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
    r[i]--;
    tupl[i] = tuple<long long, long long, long long>(l[i], r[i], c[i]);
  }
  sort(tupl, tupl + m);
  for (i = 0; i < m; i++) {
    l[i] = get<0>(tupl[i]);
    r[i] = get<1>(tupl[i]);
    c[i] = get<2>(tupl[i]);
  }
  l[m] = n - 1;
  r[m] = n - 1;
  c[m] = 0;
  m++;
  ruq.update(0, 1, 0);
  for (i = 0; i < m - 1; i++) {
    long long val = ruq.query(i);
    val += c[i];
    long long iter = upper_bound(l, l + m, r[i]) - l;
    ruq.update(i + 1, iter, val);
  }
  long long res = ruq.query(m - 1);
  if (res >= INF / 2)
    cout << -1 << endl;
  else
    cout << res << endl;
  return 0;
}
