#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<tuple<int, int, int>> lcr(M);
  for (int i = (0); i < (M); ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    lcr[i] = make_tuple(l, c, r);
  }
  sort(lcr.begin(), lcr.end());
  if (get<0>(lcr[0]) != 1) {
    cout << -1 << endl;
    exit(0);
  }
  vector<ll> dp(N + 1, LLONG_MAX);
  dp[1] = 0;
  int l = get<0>(lcr[0]);
  int r = get<2>(lcr[0]);
  int c = get<1>(lcr[0]);
  for (int i = (l + 1); i < (r + 1); ++i) dp[i] = c;
  int i = 1;
  while (i < M) {
    int il = get<0>(lcr[i]);
    int ic = get<1>(lcr[i]);
    int ir = get<2>(lcr[i]);
    if (il == l) {
      if (ir > r && dp[ir] > dp[l] + ic) {
        for (int j = (r + 1); j < (ir + 1); ++j) dp[j] = min(dp[j], dp[l] + ic);
        r = ir;
      }
      i++;
    } else {
      l = il;
      r = il;
      if (dp[l] == LLONG_MAX) {
        cout << -1 << "\n";
        exit(0);
      }
    }
  }
  cout << dp[N] << endl;
}
