#include <bits/stdc++.h>
using namespace std;
const long long int mod = 998244353;
const long long int INF = 99999999999999999;
long long int n, m, sum = 0, res = 0;
long long int L[200000], R[200000], C[200000], value[2000000] = {};
pair<pair<long long int, long long int>, long long int> LRC[200005];
set<long long int> se;
bool cmp(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.first != B.first.first) {
    return A.first.first < B.first.first;
  } else {
    return A.second < B.second;
  }
}
int main() {
  cout << fixed << setprecision(10);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> LRC[i].first.first >> LRC[i].first.second >> LRC[i].second;
  }
  sort(LRC, LRC + m, cmp);
  se.insert(1000000);
  value[1000000] = INF;
  if (LRC[0].first.first != 1) {
    cout << -1 << endl;
  } else {
    se.insert(LRC[0].first.second);
    value[LRC[0].first.second] = LRC[0].second;
  }
  for (int i = 1; i < m; i++) {
    if (*se.lower_bound(LRC[i].first.first) == 1000000) {
      cout << -1 << endl;
      return 0;
    } else {
      if (value[*se.lower_bound(LRC[i].first.first)] + LRC[i].second <
          value[*se.lower_bound(LRC[i].first.second)]) {
        se.insert(LRC[i].first.second);
        value[LRC[i].first.second] =
            value[*se.lower_bound(LRC[i].first.first)] + LRC[i].second;
      }
    }
  }
  cout << value[n];
}
