#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
signed main() {
  long long n, m;
  cin >> n >> m;
  pair<pair<long long, long long>, long long> p[m];
  long long c, l, r;
  for (long long i = 0; i < m; i++) {
    cin >> l >> r >> c;
    p[i] = make_pair(make_pair(l - 1, r - 1), c);
  }
  long long dp[n];
  sort(p, p + n);
  fill(dp, dp + n, MOD * MOD);
  dp[0] = 0;
  long long j = 0;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      que;
  while (j < n && p[j].first.first == 0) {
    que.push(make_pair(dp[0] + p[j].second, p[j].first.second));
    j++;
  }
  for (long long i = 1; i < n; i++) {
    while (!que.empty() && que.top().second < i) que.pop();
    if (!que.empty()) dp[i] = que.top().first;
    while (j < n && p[j].first.first < i) j++;
    while (j < n && p[j].first.first == i) {
      que.push(make_pair(dp[i] + p[j].second, p[j].first.second));
      j++;
    }
  }
  if (dp[n - 1] >= MOD * MOD)
    cout << -1;
  else
    cout << dp[n - 1];
  return 0;
}
