#include <bits/stdc++.h>
using namespace std;
bool comp(pair<pair<long long int, long long int>, long long int> a,
          pair<pair<long long int, long long int>, long long int> b) {
  if (a.first == b.first)
    return a.second < b.second;
  else
    return a.first < b.first;
}
void solve() {
  long long int n, m;
  cin >> n >> m;
  pair<pair<long long int, long long int>, long long int> arr[m];
  for (long long int i = 0; i < m; i++) {
    cin >> arr[i].first.first >> arr[i].first.second >> arr[i].second;
  }
  sort(arr, arr + m, comp);
  vector<long long int> dp(n + 1, 1000000000000000000LL);
  dp[1] = 0;
  long long int i = 0, d = 1, base = 0, maxx = 1;
  while (i < m) {
    d = maxx;
    while (i < m && arr[i].first.first <= d) {
      dp[arr[i].first.second] =
          min(dp[arr[i].first.second], dp[arr[i].first.first] + arr[i].second);
      maxx = max(maxx, arr[i].first.second);
      i++;
    }
    long long int minn = maxx;
    for (long long int j = maxx - 1; j > d; j--) {
      dp[j] = min(dp[j], dp[j + 1]);
      if (dp[j] < dp[minn]) {
        minn = j;
      }
    }
    if (d != maxx) maxx = minn;
  }
  if (dp[n] == 1000000000000000000LL) {
    cout << "-1\n";
    return;
  }
  cout << dp[n] << "\n";
}
signed main() {
  long long int t = 1;
  while (t--) {
    solve();
  }
}
