#include <bits/stdc++.h>
using namespace std;
struct lrc {
  int l, r, c;
};
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int> > v(m);
  for (int i = 0; i < (int)(m); i++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    v[i] = make_tuple(l, r, c);
  }
  sort(v.begin(), v.end());
  vector<long long> cost(n + 1);
  fill((cost).begin(), (cost).end(), INT_MAX >> 1);
  cost[0] = 0;
  for (int i = 0; i < (int)(m); i++) {
    for (int j = (get<0>(v[i])); j < (int)(get<1>(v[i])); j++) {
      if (cost[get<0>(v[i])] + get<2>(v[i]) < cost[j]) {
        cost[j] = cost[get<0>(v[i])] + get<2>(v[i]);
      } else {
      }
    }
  }
  if (cost[n - 2] == INT_MAX >> 1)
    cout << "-1" << endl;
  else
    cout << cost[n - 2] << endl;
  return 0;
}
