#include <bits/stdc++.h>
using namespace std;
long long n, t[4 * 100001] = {(long long)1e18};
vector<bool> marked(4 * 100001, 0);
void push(long long v) {
  if (marked[v]) {
    t[v * 2] = min(t[v * 2], t[v]);
    t[v * 2 + 1] = min(t[v * 2 + 1], t[v]);
    marked[v * 2] = marked[v * 2 + 1] = true;
    marked[v] = false;
  }
}
void build(long long v, long long tl, long long tr) {
  if (tl == tr) {
    t[v] = (tl == 1 ? 0 : (long long)1e18);
  } else {
    long long tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm + 1, tr);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}
void update(long long v, long long tl, long long tr, long long l, long long r,
            long long new_val) {
  if (l > r) return;
  if (l == tl && tr == r) {
    t[v] = min(t[v], new_val);
    marked[v] = true;
  } else {
    push(v);
    long long tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(r, tm), new_val);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, new_val);
  }
}
long long find(long long v, long long tl, long long tr, long long pos) {
  if (tl == tr) {
    return t[v];
  }
  push(v);
  long long tm = (tl + tr) / 2;
  if (pos <= tm)
    return find(v * 2, tl, tm, pos);
  else
    return find(v * 2 + 1, tm + 1, tr, pos);
}
int main() {
  long long(n), (m);
  scanf("%lld%lld", &(n), &(m));
  ;
  vector<vector<long long>> vecs;
  for (long long i = 0; i < (m); i++) {
    long long(a), (b), (c);
    scanf("%lld%lld%lld", &(a), &(b), &(c));
    ;
    vecs.push_back(vector<long long>{a, b, c});
  }
  sort((vecs).begin(), (vecs).end());
  build(1, 1, n);
  long long a1;
  for (long long i = 0; i < (m); i++) {
    a1 = find(1, 1, n, vecs[i][0]);
    update(1, 1, n, vecs[i][0], vecs[i][1], a1 + vecs[i][2]);
  }
  a1 = find(1, 1, n, n);
  if (a1 == (long long)1e18)
    printf("-1\n");
  else
    printf("%lld\n", a1);
  return 0;
}
