#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  tuple<int, int, int> lrc[m];
  for (int i = 0; i < m; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    --l;
    --r;
    lrc[i] = make_tuple(l, r, c);
  }
  long long d[n];
  for (int i = 0; i < n; i++) d[i] = -1;
  d[0] = 0;
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      pq_cost;
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      pq_left;
  for (int i = 0; i < m; i++) {
    int l = get<0>(lrc[i]);
    int r = get<1>(lrc[i]);
    int c = get<2>(lrc[i]);
    if (l == 0) {
      pq_cost.push(make_tuple(c, l, r));
    } else {
      pq_left.push(make_tuple(l, r, c));
    }
  }
  int right = 0;
  while (right < n - 1 && !pq_cost.empty()) {
    auto cost_top = pq_cost.top();
    pq_cost.pop();
    int c = get<0>(cost_top);
    int l = get<1>(cost_top);
    int r = get<2>(cost_top);
    if (r <= right) {
      continue;
    }
    for (int i = right + 1; i <= r; i++) {
      d[i] = d[l] + c;
    }
    right = r;
    while (!pq_left.empty()) {
      auto left_top = pq_left.top();
      int l = get<0>(left_top);
      int r = get<1>(left_top);
      int c = get<2>(left_top);
      if (l <= right) {
        pq_cost.push(make_tuple(c, l, r));
        pq_left.pop();
      } else {
        break;
      }
    }
  }
  cout << d[n - 1] << endl;
}
