#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chmin(T &a, T b) {
  if (a > b) a = b;
}
template <typename T>
void chmax(T &a, T b) {
  if (a < b) a = b;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
const long long INF = 1e18;
struct SqrtDecomposition {
  int n, sqrtn, k;
  vector<long long> dat, lazy;
  vector<bool> lazyflag;
  void init(int x) {
    n = x;
    sqrtn = sqrt(n);
    k = (n + sqrtn - 1) / sqrtn;
    dat.assign(sqrtn * k, INF);
    lazy.assign(k, INF);
    lazyflag.assign(k, false);
  }
  void eval(int b) {
    if (!lazyflag[b]) return;
    lazyflag[b] = false;
    for (int i = sqrtn * b; i <= sqrtn * (b + 1) - 1; ++i) {
      chmin(dat[i], lazy[b]);
    }
    lazy[b] = INF;
  }
  void update(int l, int r, long long x) {
    for (int b = 0; b < k; ++b) {
      int p = sqrtn * b;
      int q = sqrtn * (b + 1);
      if (q <= l || r <= p) continue;
      if (l <= p && q <= r) {
        lazyflag[b] = true;
        chmin(lazy[b], x);
      } else {
        eval(b);
        for (int i = max(l, p); i <= min(r, q) - 1; ++i) {
          chmin(dat[i], x);
        }
      }
    }
  }
  long long query(int l, int r) {
    long long ret = INF;
    for (int b = 0; b < k; ++b) {
      int p = sqrtn * b;
      int q = sqrtn * (b + 1);
      if (q <= l || r <= p) continue;
      if (l <= p && q <= r) {
      } else {
        eval(b);
        for (int i = max(l, p); i <= min(r, q) - 1; ++i) {
          chmin(ret, dat[i]);
        }
      }
    }
    return ret;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<long long> > l2r(n), l2c(n);
  for (int i = 0; i < m; ++i) {
    int l = in() - 1, r = in(), c = in();
    l2r[l].push_back(r);
    l2c[l].push_back(c);
  }
  SqrtDecomposition sd;
  sd.init(n);
  sd.update(0, 1, 0);
  for (int i = 0; i < n - 1; ++i) {
    long long x = sd.query(i, i + 1);
    if (x == INF) {
      cout << -1 << endl;
      return 0;
    }
    for (int j = 0; j < l2r[i].size(); ++j) {
      int r = l2r[i][j];
      long long c = l2c[i][j];
      sd.update(i + 1, r, x + c);
    }
  }
  long long ans = sd.query(n - 1, n);
  cout << ans << endl;
  return 0;
}
