#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353, MAX = 1 << 17;
const long long INF = 1LL << 60;
int n;
long long dat[2 * MAX - 1], lazy[2 * MAX - 1];
bool done[2 * MAX - 1];
void init(int n_) {
  n = 1;
  while (n < n_) n *= 2;
  for (int i = 0; i < 2 * n - 1; i++) {
    dat[i] = INF;
    lazy[i] = INF;
  }
}
void eval(int k, int l, int r) {
  if (done[k] == 0) return;
  dat[k] = min(dat[k], lazy[k]);
  if (r - l > 1) {
    lazy[k * 2 + 1] = lazy[k];
    lazy[k * 2 + 2] = lazy[k];
    done[k * 2 + 1] = done[k * 2 + 2] = 1;
  }
  done[k] = 0;
}
void update(int a, int b, long long x, int k, int l, int r) {
  eval(k, l, r);
  if (r <= a || b <= l) return;
  if (a <= l && r <= b) {
    lazy[k] = min(lazy[k], x);
    done[k] = 1;
    eval(k, l, r);
  } else {
    update(a, b, x, 2 * k + 1, l, (l + r) / 2);
    update(a, b, x, 2 * k + 2, (l + r) / 2, r);
    dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]);
  }
}
long long query(int a, int b, int k, int l, int r) {
  eval(k, l, r);
  if (r <= a || b <= l) return INF;
  if (a <= l && r <= b) return dat[k];
  long long vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
  long long vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
  return min(vl, vr);
}
struct edge {
  int from;
  int to;
  long long dis;
};
bool compare(edge a, edge b) {
  if (a.from == b.from) return a.to < b.to;
  return a.from < b.from;
}
int main() {
  int N, M;
  cin >> N >> M;
  init(N);
  vector<edge> S(M);
  for (int i = 0; i < M; i++) {
    int a, b;
    long long c;
    cin >> a >> b >> c;
    a--;
    b--;
    S[i] = {a, b, c};
  }
  sort((S).begin(), (S).end(), compare);
  update(0, 1, 0, 0, 0, n);
  for (int i = 0; i < M; i++) {
    int a = S[i].from, b = S[i].to;
    long long c = S[i].dis;
    long long mini = query(a, b + 1, 0, 0, n);
    update(a, b + 1, mini + c, 0, 0, n);
  }
  long long ans = query(N - 1, N, 0, 0, n);
  if (ans > 1000000000000000) ans = -1;
  cout << ans << endl;
}
