#include <bits/stdc++.h>
using namespace std;
struct lst_node {
  long long val;
  long long offset;
};
class lazy_segment_tree {
 public:
  long long size;
  vector<lst_node> node_list;
  lazy_segment_tree() {}
  void initialize_empty(long long n) {
    size = n;
    lst_node lsd;
    lsd.val = 0;
    lsd.offset = 0;
    node_list.assign(size * 4 + 100, lsd);
  }
  long long build_tree(long long l, long long r, long long p,
                       vector<long long>& a) {
    if (l == r) {
      node_list[p].val = a[l];
      node_list[p].offset = 2000000000;
      return p;
    } else {
      long long m = (l + r) >> 1;
      build_tree(l, m, p * 2, a);
      build_tree(m + 1, r, p * 2 + 1, a);
      node_list[p].val = min(node_list[p * 2].val, node_list[p * 2 + 1].val);
      node_list[p].offset = 2000000000;
      return p;
    }
  }
  void initialize(vector<long long>& a) {
    size = a.size();
    node_list.assign(size * 4 + 100, {2000000000, 0});
    build_tree(0, size - 1, 1, a);
  }
  long long update(long long pos, long long key) {
    long long ans = update_range_full(1, 0, size - 1, pos, pos, key);
    return ans;
  }
  long long update_range(long long l, long long r, long long off) {
    long long ans = update_range_full(1, 0, size - 1, l, r, off);
    return ans;
  }
  long long update_range_full(long long p, long long left, long long right,
                              long long l, long long r, long long off) {
    if ((left >= l) && (right <= r)) {
      if (left == right) {
        node_list[p].val = min(node_list[p].val, off);
        node_list[p].offset = 0;
      } else {
        node_list[p].offset = min(node_list[p].offset, off);
        node_list[p].val =
            min(min(node_list[p * 2].val, node_list[p * 2 + 1].val),
                node_list[p].offset);
      }
      return p;
    } else {
      long long m = (left + right) >> 1;
      long long a, b;
      if (m >= l)
        a = update_range_full(p * 2, left, m, l, r, off);
      else
        a = p * 2;
      if (m + 1 <= r)
        b = update_range_full(p * 2 + 1, m + 1, right, l, r, off);
      else
        b = p * 2 + 1;
      node_list[p].val =
          min(min(node_list[a].val, node_list[b].val), node_list[p].offset);
      return p;
    }
  }
  long long query(long long l, long long r) {
    if (l > r) {
      return 2000000000;
    }
    return query_full(1, 0, size - 1, l, r, 2000000000);
  }
  inline long long query_full(long long p, long long left, long long right,
                              long long l, long long r, long long off) {
    if ((left >= l) && (right <= r)) return min(node_list[p].val, off);
    long long m = (left + right) >> 1, ans = 2000000000;
    if (m >= l) {
      ans = min(
          ans, query_full(p * 2, left, m, l, r, min(off, node_list[p].offset)));
    }
    if (m + 1 <= r) {
      ans = min(ans, query_full(p * 2 + 1, m + 1, right, l, r,
                                min(off, node_list[p].offset)));
    }
    return ans;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, i, j, k, l, r, c, x;
  cin >> n >> m;
  vector<tuple<long long, long long, long long>> plist(m);
  for (i = 0; i < m; i++) {
    cin >> l >> r >> c;
    plist[i] = make_tuple(l, r, c);
  }
  sort(plist.begin(), plist.end());
  vector<long long> a(n + 2, 2000000000);
  a[1] = 0;
  lazy_segment_tree lst;
  lst.initialize(a);
  for (auto p : plist) {
    tie(l, r, c) = p;
    x = lst.query(l, r);
    lst.update_range(l, r, x + c);
  }
  x = lst.query(n, n);
  if (x == 2000000000) x = -1;
  cout << x << "\n";
  return 0;
}
