#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 1e5;
int n, m;
vector<pair<int, int>> v[N_MAX];
priority_queue<pair<long, int>, vector<pair<long, int>>,
               greater<pair<long, int>>>
    q;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int l, r, c;
    scanf("%d %d %d", &l, &r, &c);
    v[l - 1].emplace_back(r - 1, c);
  }
  long ans = 1e18;
  q.emplace(0, 0);
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0, e = v[i].size(); j < e; j++) {
      while (!q.empty() && q.top().second < i) q.pop();
      if (q.empty()) {
        printf("-1\n");
        return 0;
      }
      if (v[i][j].first == n - 1) {
        ans = min(ans, q.top().first + v[i][j].second);
      } else {
        q.emplace(q.top().first + v[i][j].second, v[i][j].first);
      }
    }
  }
  printf("%ld\n", ans);
  return 0;
}
