#include <bits/stdc++.h>
using namespace std;
long long dx[4] = {1, 0, -1, 0};
long long dy[4] = {0, -1, 0, 1};
template <class T>
void inputVector(vector<T> &v, long long n) {
  v.resize(n);
  for (long long i = 0; i < v.size(); i++) cin >> v[i];
}
struct Info {
  long long L, R, C;
  Info(long long L, long long R, long long C) : L(L), R(R), C(C) {}
  bool operator<(const Info &a) const {
    if (L == a.L && R == a.R) return C < a.C;
    if (L == a.L) return R < a.R;
    return L < a.L;
  }
};
long long dp[100010];
class RangeMinQuery {
 private:
  long long bottomSt, bottomEd;
  vector<long long> a;
  vector<long long> rs, re;
  vector<long long> cand;
  long long set(long long n) {
    long long left = 2 * n + 1;
    long long right = 2 * n + 2;
    if (left <= a.size() - 1) {
      a[n] = min(set(left), set(right));
      rs[n] = rs[left];
      re[n] = re[right];
    }
    return a[n];
  }
  void search(long long s, long long t, long long n) {
    if (s <= rs[n] && re[n] <= t) {
      cand.push_back(a[n]);
      return;
    }
    long long left = 2 * n + 1;
    long long right = 2 * n + 2;
    if (s <= re[left]) search(s, t, left);
    if (rs[right] <= t) search(s, t, right);
  }

 public:
  RangeMinQuery(vector<long long> &v) {
    bottomEd = 0;
    for (long long i = 1, plus = 1; plus < v.size(); i++) {
      plus *= 2;
      bottomEd += plus;
    }
    a.resize(bottomEd + 1);
    rs.resize(bottomEd + 1);
    re.resize(bottomEd + 1);
    bottomSt = bottomEd / 2;
    for (long long i = bottomSt; i <= bottomEd; i++) {
      long long j = i - bottomEd / 2;
      a[i] = j < v.size() ? v[j] : LLONG_MAX;
      rs[i] = j;
      re[i] = j;
    }
    set(0);
  }
  void update(long long index, long long x) {
    long long pt = bottomSt + index;
    a[pt] = x;
    while (pt > 0) {
      pt = (pt - 1) / 2;
      a[pt] = min(a[2 * pt + 1], a[2 * pt + 2]);
    }
  }
  long long find(long long s, long long t) {
    cand.clear();
    search(s, t, 0);
    long long ret = cand[0];
    for (long long i = 1; i < cand.size(); i++) {
      ret = min(ret, cand[i]);
    }
    return ret;
  }
};
signed main() {
  long long N, M;
  cin >> N >> M;
  vector<Info> infos;
  for (long long i = 0; i < (M); i++) {
    long long L, R, C;
    cin >> L >> R >> C;
    L--;
    R--;
    infos.push_back(Info(L, R, C));
  }
  sort((infos).begin(), (infos).end());
  const long long INF = LLONG_MAX / 10;
  for (long long i = 0; i < (N); i++) dp[i] = INF;
  dp[0] = 0;
  vector<long long> v(N, INF);
  RangeMinQuery rmq(v);
  for (auto &info : infos) {
    if (dp[info.L] == INF) {
      long long minval = rmq.find(info.L, N - 1);
      if (minval == INF) {
        cout << -1 << endl;
        return 0;
      }
      dp[info.L] = minval;
    }
    if (dp[info.R] > dp[info.L] + info.C) {
      dp[info.R] = dp[info.L] + info.C;
      rmq.update(info.R, dp[info.R]);
    }
  }
  if (dp[N - 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[N - 1] << endl;
  }
  return 0;
}
