#include <bits/stdc++.h>
using namespace std;
long long int mod = 998244353;
long long int power(long long int a, long long int b) {
  long long int res = 1;
  a %= mod;
  while (b > 0) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
long long int dp[100100];
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int m;
  cin >> m;
  vector<pair<int, pair<int, long long int> > > v;
  for (int i = 0; i < m; i++) {
    long long int l, r, c;
    cin >> l >> r >> c;
    v.push_back(make_pair(l, make_pair(r, c)));
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    dp[i] = 1e15;
  }
  map<int, long long int> mp;
  set<int> s;
  s.insert(1);
  s.insert(n);
  mp[1] = 0;
  mp[n] = 1e15;
  for (int i = 0; i < m; i++) {
    int k = *(s.upper_bound(v[i].first - 1));
    if (k == 1 || mp[k] < 1e15) {
      s.insert(v[i].second.first);
      if (mp[v[i].second.first] == 0) {
        mp[v[i].second.first] = mp[k] + v[i].second.second;
      } else {
        mp[v[i].second.first] =
            min(mp[v[i].second.first], mp[k] + v[i].second.second);
      }
    }
  }
  if (mp[n] == 1e15) {
    cout << -1 << endl;
  } else {
    cout << mp[n] << endl;
  }
}
