#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  long long l[m], r[m], c[m];
  for (long long i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
    r[i]--;
  }
  vector<pair<long long, long long> > vec[n];
  for (long long i = 0; i < m; i++) {
    for (long long j = l[i] + 1; j < r[i] + 1; j++) {
      vec[j].push_back({l[i], c[i]});
    }
  }
  bool skip[n];
  fill(skip, skip + n, true);
  for (long long i = 0; i < m; i++) {
    skip[l[i]] = false;
    skip[r[i]] = false;
  }
  long long dp[n];
  fill(dp, dp + n, 1e18);
  dp[0] = 0;
  for (long long i = 1; i < n; i++) {
    if (vec[i].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (skip[i]) continue;
    long long tempmin = 1e18;
    for (long long j = 0; j < vec[i].size(); j++) {
      long long start = vec[i][j].first;
      long long cost = vec[i][j].second;
      dp[i] = min(dp[i], dp[start] + cost);
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}
