#include <bits/stdc++.h>
using namespace std;
const long long INF = (1ll << 31) - 1;
struct Segment {
  int n;
  vector<long long> node;
  Segment(int N) {
    n = 1;
    while (n < N) n *= 2;
    node.assign(2 * n - 1, INF);
  }
  void update(int k, long long a) {
    k += n - 1;
    node[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  long long find_min(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return INF;
    if (a <= l && r <= b)
      return node[k];
    else {
      long long vl = find_min(a, b, 2 * k + 1, l, (l + r) / 2);
      long long vr = find_min(a, b, 2 * k + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};
struct Range {
  int l, r;
  long long c;
};
bool operator<(const Range& x0, const Range& x1) {
  return (x0.r == x1.r ? x0.l > x1.l : x0.r < x1.r);
}
int main() {
  int n, m;
  cin >> n >> m;
  Segment tree(n);
  vector<Range> v(m);
  for (int i = 0; i < m; i++) {
    cin >> v[i].l >> v[i].r >> v[i].c;
    v[i].l--, v[i].r--;
  }
  sort(v.begin(), v.end());
  tree.update(0, 0);
  for (int i = 0; i < m; i++) {
    tree.update(v[i].r, min(tree.find_min(v[i].r, v[i].r + 1),
                            tree.find_min(v[i].l, v[i].r) + v[i].c));
  }
  long long ans = tree.find_min(n - 1, n);
  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}
