#include <bits/stdc++.h>
using namespace std;
const long long MX = 1e5 + 5, INF = 5LL << 57, MOD = 1e9 + 7;
long long N, M;
string S;
vector<long long> va, vb;
long long dat[11 * MX + 5];
long long n;
void init(long long n_) {
  n = 1;
  while (n < n_) {
    n *= 2;
  }
  for (long long i = 0; i < 2 * n - 1; ++i) dat[i] = INF;
}
void update(long long k, long long a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
long long query(long long a, long long b, long long k, long long l,
                long long r) {
  if (r <= a || b <= l) return INF;
  if (a <= l && r <= b)
    return dat[k];
  else {
    long long vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    long long vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}
vector<tuple<long long, long long, long long>> lrc;
void input() {
  cin >> N >> M;
  init(N);
  for (long long i = (long long)(1); i <= (long long)(M); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    lrc.push_back(tie(l, r, c));
  };
}
void solve() {
  sort(lrc.begin(), lrc.end());
  long long ans = INF;
  update(1, 0);
  for (long long i = 0; i < N; ++i) {
    long long a, b, c;
    tie(a, b, c) = lrc[i];
    long long tmp = query(a, b + 1, 0, 0, n);
    if (tmp == INF) {
      cout << -1 << endl;
      return;
    }
    long long kk = query(b, b + 1, 0, 0, n);
    update(b, min(kk, tmp + c));
    if ((i < N - 1) && (get<0>(lrc[i + 1]) <= b)) {
      long long aa = get<0>(lrc[i + 1]);
      long long ll = query(aa, aa + 1, 0, 0, n);
      update(aa, min(ll, tmp + c));
    }
    if (b == N) {
      ans = min(ans, query(a, b + 1, 0, 0, n) + c);
    }
  };
  cout << ans << endl;
}
signed main() {
  input();
  solve();
  return 0;
}
