#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e+18;
template <typename T>
struct SegmentTree {
  int n;
  vector<T> a;
  function<T(T, T)> append;
  T unit;
  SegmentTree() = default;
  SegmentTree(int a_n, T a_unit, function<T(T, T)> a_append) {
    n = pow(2, ceil(log2(a_n)));
    a.resize(2 * n - 1, a_unit);
    unit = a_unit;
    append = a_append;
  }
  T point_get(int i) {
    T ret = unit;
    for (i = i + n; i > 0; i /= 2) {
      ret = append(a[i - 1], ret);
    }
    return ret;
  }
  void range_update(int l, int r, T t) { range_update(0, 0, n, l, r, t); }
  void range_update(int i, int il, int ir, int l, int r, T t) {
    if (l <= il and ir <= r) {
      a[i] = append(a[i], t);
    } else if (ir <= l or r <= il) {
      return;
    } else {
      range_update(2 * i + 1, il, (il + ir) / 2, l, r, t);
      range_update(2 * i + 2, (il + ir) / 2, ir, l, r, t);
    }
  }
  void set() {}
  void write_tree(int i, int h, int il, int ir) {
    for (int j = 0; j < (int)(h); ++j) cout << " ";
    cout << a[i] << endl;
    if (ir - il == 1) {
      return;
    } else {
      int im = (il + ir) / 2;
      write_tree(i * 2 + 1, h + 1, il, im);
      write(i * 2 + 2, h + 1, im, ir);
    }
  }
  void write_tree() { write_tree(0, 0, 0, n); }
  void write() {
    for (int i = 0; i < (int)(n); ++i) cout << point_get(i) << " ";
    cout << endl;
  }
};
void solve(long long N, long long M, std::vector<long long> L,
           std::vector<long long> R, std::vector<long long> C) {
  function<long long(long long, long long)> append =
      [](long long a, long long b) { return min(a, b); };
  SegmentTree<long long> st(N + 1, INF, append);
  vector<pair<pair<long long, long long>, long long> > v;
  for (int i = 0; i < M; i++) {
    v.push_back({{L[i], R[i]}, C[i]});
  }
  int i = 0;
  sort(v.begin(), v.end());
  st.range_update(0, 1, 0);
  for (int u = 0; u < N; u++) {
    long long d = st.point_get(u);
    if (u == N - 1) {
      if (d == INF) {
        cout << -1 << endl;
      } else {
        cout << d << endl;
      }
      break;
    }
    while (i < N and v[i].first.first == u) {
      st.range_update(v[i].first.first + 1, v[i].first.second + 1,
                      d + v[i].second);
      i++;
    }
  }
}
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> L(M);
  std::vector<long long> R(M);
  std::vector<long long> C(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &L[i]);
    scanf("%lld", &R[i]);
    scanf("%lld", &C[i]);
    L[i]--;
    R[i]--;
  }
  solve(N, M, std::move(L), std::move(R), std::move(C));
  return 0;
}
