#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int> > t(m);
  long long ans = 0;
  int l, r, v;
  for (int i = 0; i < m; i++) {
    cin >> l >> r >> v;
    t[i] = make_tuple(l, r, v);
  }
  sort(t.begin(), t.end());
  int arrived = 1;
  vector<long long> cost(n + 1, (long long)998244353 * 100000);
  cost[1] = 0;
  for (int i = 0; i < m; i++) {
    arrived = max(get<1>(t[i]), arrived);
    for (int j = get<0>(t[i]); j <= get<1>(t[i]); j++) {
      cost[j] = min(cost[j], get<2>(t[i]) + cost[get<0>(t[i])]);
    }
  }
  if (cost[n] == (long long)998244353 * 100000) cost[n] = -1;
  cout << cost[n] << endl;
  return 0;
}
