#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
class SegmentTree {
 public:
  long long n;
  vector<long long> node;
  SegmentTree(vector<long long> v) {
    long long size = v.size();
    n = 1;
    while (n < size) n *= 2;
    node.resize(2 * n - 1, INF);
    for (long long i = 0; i < size; i++) {
      node[n - 1 + i] = v[i];
    }
    for (long long i = n - 2; i >= 0; i--) {
      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  long long update(long long idx, long long val) {
    idx += n - 1;
    chmin(node[idx], val);
    while (idx > 0) {
      idx = (idx - 1) / 2;
      long long tmp = min(node[2 * idx + 1], node[2 * idx + 2]);
      chmin(node[idx], tmp);
    }
  }
  long long get_min(long long a, long long b, long long k = 0, long long l = 0,
                    long long r = -1) {
    if (r < 0) r = n;
    if (b <= l || r <= a) return INF;
    if (a <= l && r <= b) return node[k];
    long long vl = get_min(a, b, 2 * k + 1, l, (l + r) / 2);
    long long vr = get_min(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<long long> X(N, INF);
  X[0] = 0;
  SegmentTree tree(X);
  vector<tuple<int, int, long long>> BAC;
  for (int i = 0; i < M; i++) {
    int a, b;
    long long c;
    cin >> a >> b >> c;
    a--, b--;
    BAC.emplace_back(b, a, c);
  }
  sort(BAC.begin(), BAC.end());
  for (auto &&t : BAC) {
    int a, b;
    long long c;
    a = get<1>(t);
    b = get<0>(t);
    c = get<2>(t);
    long long m = tree.get_min(a, b + 1);
    tree.update(b, m + c);
  }
  long long ans;
  ans = tree.get_min(N - 1, N);
  if (ans == INF) ans = -1;
  cout << ans << endl;
  return 0;
}
