#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a % b == 0) return b;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  long long c = gcd(a, b);
  return ((a / c) * (b / c) * c);
}
const long long INF = long long(1e18) + long long(7);
const long long MOD = 1000000007LL;
struct MQ {
  long long n, rn, bn, Q = 0;
  vector<long long> a, b, at, bt;
  MQ(long long n_) {
    n = n_;
    rn = sqrt(n);
    bn = ceil((double)n / rn);
    a.resize(n, INF);
    at.resize(n, 0);
    b.resize(bn, INF);
    bt.resize(n, 0);
  }
  int value(long long s) {
    if (a[s] > b[s / rn]) a[s] = b[s / rn];
    return a[s];
  }
  void updete(long long s, long long t, long long x) {
    Q++;
    for (long long i = long long(s); i < long long(t + 1); ++i) {
      if (i % rn == 0 && (i + rn - 1) <= t) {
        if (x < b[i / rn]) b[i / rn] = x;
        bt[i / rn] = Q;
        i += rn - 1;
      } else {
        if (a[i] > x) a[i] = x;
        at[i] = Q;
      }
    }
  }
};
int main() {
  long long N, M;
  cin >> N >> M;
  vector<tuple<long long, long long, long long> > LRC(M);
  for (long long i = long long(0); i < long long(M); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    LRC[i] = tie(l, r, c);
  }
  (sort(LRC.begin(), LRC.end()));
  MQ minq(N);
  minq.updete(0, 0, 0);
  for (long long i = long long(0); i < long long(M); ++i) {
    long long l, r, c;
    tie(l, r, c) = LRC[i];
    long long v = minq.value(l);
    if (v >= INF) continue;
    minq.updete(l, r, c + v);
  }
  long long ans = minq.value(N - 1);
  if (ans == INF || ans < 0) ans = -1;
  cout << ans << endl;
}
