#include <bits/stdc++.h>
static const long long MOD = 998244353;
static const long long INF = 1000000000000000000LL;
using namespace std;
struct Node {
  int l;
  int r;
  long long c;
};
bool cmp(const Node &a, const Node &b) {
  if (a.l == b.l) {
    return a.r < b.r;
  }
  return a.l < b.l;
}
Node make_Node(int l, int r, long long c) {
  Node mynode = {l, r, c};
  return mynode;
}
int main() {
  long long N, M;
  cin >> N >> M;
  long long dp[N + 1];
  for (long long i = 0; i < (N + 1); i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  vector<Node> v(M);
  for (long long i = 0; i < (M); i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    --l;
    --r;
    v[i] = make_Node(l, r, c);
  }
  sort(v.begin(), v.end(), cmp);
  long long peak = 0;
  for (long long i = 0; i < (M); i++) {
    long long l = v[i].l;
    long long r = v[i].r;
    if (dp[r] <= dp[l] + v[i].c) continue;
    for (long long j = peak; j <= r; ++j) {
      dp[j] = min(dp[j], dp[peak] + v[i].c);
    }
    peak = r;
  }
  long long res = dp[N - 1];
  if (res >= INF) res = -1;
  cout << res << endl;
  return 0;
}
