#include <bits/stdc++.h>
using namespace std;
int rmq[400000] = {};
int N, M;
tuple<int, int, int> LRC[100000];
void update(int index, int value) {
  for (int idx = (1 << 17) + index - 1; idx > 0; idx /= 2) {
    rmq[idx] = min(rmq[idx], value);
  }
}
int query(int begin, int end, int node_idx, int node_b, int node_e) {
  if (node_b >= end || node_e <= begin) {
    return INT_MAX;
  }
  if (begin <= node_b && node_e <= end) {
    return rmq[node_idx];
  }
  int vl = query(begin, end, node_idx * 2, node_b, (node_b + node_e) / 2);
  int vr = query(begin, end, node_idx * 2 + 1, (node_b + node_e) / 2, node_e);
  return min(vl, vr);
}
int main() {
  for (int i = 0; i < 400000; i++) {
    rmq[i] = INT_MAX;
  }
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    LRC[i] = make_tuple(l, r, c);
  }
  sort(LRC, LRC + M);
  update(1, 0);
  for (int i = 0; i < M; i++) {
    int l, r, c;
    tie(l, r, c) = LRC[i];
    int m = query(l, r + 1, 1, 1, (1 << 17) + 1);
    update(r, m + c);
  }
  int ans = query(N, N + 1, 1, 1, (1 << 17) + 1);
  if (ans > 2e9 || ans < -2e9) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
