#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
const long long INF = 1LL << 60;
struct point {
  long long x, y, val;
  point() {}
  point(long long x, long long y, long long val) : x(x), y(y), val(val){};
  point(long long x, long long y) : x(x), y(y), val(0){};
};
auto query = [](long long x, long long y) { return min(x, y); };
const long long unit = INF;
struct SegTree {
 private:
  long long n;
  vector<long long> node;

 public:
  SegTree(vector<long long> v) {
    node.resize((1LL << 64 - __builtin_clzll(v.size())) * 2 - 1, unit);
    n = 1LL << 64 - __builtin_clzll(v.size());
    for (long long i = 0; i < v.size(); i++) node[i + n - 1] = v[i];
    for (long long i = n - 2; i >= 0; i--)
      node[i] = query(node[2 * i + 1], node[2 * i + 2]);
  }
  void update(long long x, long long val) {
    x += (n - 1);
    node[x] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = query(node[2 * x + 1], node[2 * x + 2]);
    }
  }
  long long getmin(long long a, long long b, long long k = 0, long long l = 0,
                   long long r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return unit;
    if (a <= l && r <= b) return node[k];
    long long vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
    long long vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
    return query(vl, vr);
  }
};
bool cmp(const point &a, const point &b) {
  if (a.y == b.y)
    return a.x < b.x;
  else
    return a.y < b.y;
}
int main() {
  long long n, m;
  cin >> n >> m;
  vector<point> v;
  for (long long i = 0; i < (long long)(m); i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    v.emplace_back(l, r, c);
  }
  sort(v.begin(), v.end(), cmp);
  vector<long long> t(n, (1LL << 31) - 1);
  t[0] = 0;
  SegTree s(t);
  for (long long i = 0; i < (long long)(m); i++) {
    long long mi = s.getmin(v[i].x, n);
    long long aaa = min(mi + v[i].val, s.getmin(v[i].y, v[i].y + 1));
    s.update(v[i].y, aaa);
  }
  long long ans = s.getmin(n - 1, n);
  if (ans > (1LL << 30)) ans = -1;
  cout << s.getmin(n - 1, n) << endl;
  return 0;
}
