#include <bits/stdc++.h>
using namespace std;
std::ostream& operator<<(std::ostream& os, pair<int, int> p) {
  os << p.first << ", " << p.second;
  return os;
}
class SegmentTree {
 public:
  int n;
  bool isLazy;

 private:
  vector<long long int> segTree, lazy;

 public:
  SegmentTree() {}
  SegmentTree(int n) {
    segTree = vector<long long int>(4 * n);
    lazy = vector<long long int>(4 * n);
  }
  void buildTree(int node, int l, int r, vector<long long int>& anaDizi) {
    if (l > r) return;
    if (l == r)
      segTree[node] = anaDizi[l];
    else {
      buildTree(node * 2, l, (l + r) / 2, anaDizi);
      buildTree(node * 2 + 1, (l + r) / 2 + 1, r, anaDizi);
      segTree[node] = min(segTree[node * 2], segTree[node * 2 + 1]);
    }
  }
  void updateTree(int node, int a, int b, int ind, long long int x) {
    if (a > b) return;
    if (a > ind || b < ind) return;
    if (a == b && a == ind) {
      segTree[node] = x;
      return;
    }
    updateTree(node * 2, a, (a + b) / 2, ind, x);
    updateTree(node * 2 + 1, (a + b) / 2 + 1, b, ind, x);
    segTree[node] = min(segTree[node * 2], segTree[node * 2 + 1]);
  }
  long long int query(int node, int a, int b, int l, int r) {
    if (l > r) return 1e17;
    if (a > b || a > r || b < l) return 1e17;
    if (lazy[node] != 0) {
      segTree[node] += lazy[node] * (b - a + 1);
      lazy[node * 2] += lazy[node];
      lazy[node * 2 + 1] += lazy[node];
      lazy[node] = 0;
    }
    if (a >= l && b <= r) return segTree[node];
    return min(query(node * 2, a, (a + b) / 2, l, r),
               query(node * 2 + 1, (a + b) / 2 + 1, b, l, r));
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<pair<long long int, pair<long long int, long long int> > > aras;
  for (int i = 0; i < m; i++) {
    long long int a, b, cost;
    cin >> a >> b >> cost;
    aras.push_back(pair<long long int, pair<long long int, long long int> >(
        a, pair<long long int, long long int>(cost, b)));
  }
  sort(aras.begin(), aras.end());
  vector<long long int> dist(n + 1, 1e17);
  dist[1] = 0;
  SegmentTree st(n + 1);
  st.buildTree(1, 1, n, dist);
  long long int last = 1;
  for (int i = 0; i < m; i++) {
    long long int cost = aras[i].second.first;
    long long int a = aras[i].first, b = aras[i].second.second;
    for (long long int smd = max(last + 1, a); smd <= b; smd++) {
      dist[smd] = min(dist[smd], st.query(1, 1, n, a, smd - 1) + cost);
      if (dist[smd] != 1e17)
        last = smd;
      else
        break;
      st.updateTree(1, 1, n, smd, dist[smd]);
    }
  }
  cout << (dist[n] == 1e17 ? -1 : dist[n]) << "\n";
  return 0;
}
