#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++) o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
const long long inf = 1001002003004005006ll;
int N;
vector<long long> segt;
void update(int i, int x) {
  i += N - 1;
  segt[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    segt[i] = min(segt[2 * i + 1], segt[2 * i + 2]);
  }
}
long long query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) return inf;
  if (a <= l && r <= b)
    return segt[k];
  else {
    long long c1 = query(a, b, 2 * k + 1, l, (l + r) / 2);
    long long c2 = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(c1, c2);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int> > v;
  for (int i = (int)(0); i < (int)(m); ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    auto tp = make_tuple(l, r, c);
    v.push_back(tp);
  }
  sort(v.begin(), v.end(), [](tuple<int, int, int> x, tuple<int, int, int> y) {
    return get<1>(x) < get<1>(y);
  });
  N = 1;
  while (N < n) N *= 2;
  segt.resize(2 * N - 1, inf);
  update(0, 0);
  for (auto u : v) {
    int r, l, c;
    l = get<0>(u);
    r = get<1>(u);
    c = get<2>(u);
    l--;
    r--;
    long long value = query(l, r + 1, 0, 0, N);
    if (value + c < segt[r + N - 1]) {
      update(r, value + c);
    }
  }
  long long ans = segt[n + N - 2];
  if (ans == inf) ans = -1;
  cout << ans;
  return 0;
  for (int i = (int)(0); i < (int)(2 * N - 1); ++i) {
    cout << segt[i] << endl;
  }
}
