#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct segtree {
  int n;
  vector<T> dat;
  T ZERO;
  function<T(T, T)> func;
  segtree(int n_, function<T(T, T)> func_, T zero) : func(func_), ZERO(zero) {
    n = 1;
    while (n < n_) n *= 2;
    dat.resize(2 * n);
    for (int i = 0; i < 2 * n; i++) dat[i] = ZERO;
  }
  void update(int k, T a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  void set(int k, T a) {
    k += n - 1;
    dat[k] = a;
  }
  void build() {
    for (int i = n - 2; i >= 0; i--) {
      dat[i] = func(dat[2 * i + 1], dat[2 * i + 2]);
    }
  }
  T get(int k) { return dat[k + n - 1]; }
  T que(int a, int b) {
    T L = ZERO, R = ZERO;
    int A = a + n - 1;
    int B = b + n - 1;
    while (A < B) {
      if ((A & 1) == 0) L = func(L, dat[A++]);
      if ((B & 1) == 0) R = func(dat[--B], R);
      A = A >> 1;
      B = B >> 1;
    }
    return func(L, R);
  }
};
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  segtree<long long> seg(
      111111, [](long long a, long long b) { return min(a, b); }, 1000000000);
  seg.update(1, 0);
  vector<pair<pair<long long, long long>, long long>> v;
  for (long long(i) = (0); (i) < (m); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    v.emplace_back(make_pair(r, l), c);
  }
  sort(v.begin(), v.end());
  for (long long(i) = (0); (i) < (m); ++i) {
    long long l = v[i].first.second;
    long long r = v[i].first.first;
    long long c = v[i].second;
    long long tmp = min(seg.que(l, r + 1) + c, seg.get(r));
    seg.update(r, tmp);
  }
  if (seg.get(n) != 1000000000)
    cout << seg.get(n) << endl;
  else
    cout << -1 << endl;
  return 0;
}
