#include <bits/stdc++.h>
#pragma warning(disable : 4996)
const long long MOD = 998244353;
using namespace std;
int main(int argc, char* argv[]) {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<vector<pair<int, int> > > z(n);
  int i;
  for (i = 0; i < m; i++) {
    int l, r, c;
    scanf("%d%d%d", &l, &r, &c);
    l--;
    r--;
    z[r].push_back(make_pair(l, c));
  }
  set<int> save;
  vector<long long> dp(n, 9223300000000000000);
  dp[0] = 0;
  save.insert(0);
  for (i = 1; i < n; i++) {
    int siz = (int)z[i].size();
    int k;
    for (k = 0; k < siz; k++) {
      int l = z[i][k].first;
      int c = z[i][k].second;
      auto it = save.lower_bound(l);
      if (it != save.end()) {
        dp[i] = ((dp[i]) > (dp[*it] + c) ? (dp[*it] + c) : (dp[i]));
        save.insert(i);
      }
    }
  }
  if (dp[n - 1] == 9223300000000000000) {
    printf("-1\n");
  } else {
    printf("%lld\n", dp[n - 1]);
  }
  return 0;
}
