#include <bits/stdc++.h>

using namespace std;

template<typename C>
class seg_tree {
  vector<C> tree;
  function<C(C, C)> key;
  C def;
  int p = 1;
public:
  seg_tree(int n, function<C(C, C)> _key, C _def) : key(_key), def(_def) {
    while (p < n) {
      p <<= 1;
    }
    tree.resize(p << 1, def);
  }

  seg_tree(const vector<C>& a, function<C(C, C)> _key, C _def) : key(_key), def(_def) {
    while (p < a.size()) {
      p <<= 1;
    }
    tree.resize(p << 1, def);
    for (int i = 0; i < a.size(); ++i) {
      tree[i ^ p] = a[i];
    }
    for (int i = p - 1; i; --i) {
      tree[i] = key(tree[i << 1], tree[i << 1 | 1]);
    }
  }

  C get(int l, int r) {
    C res = def;
    for (l ^= p, r ^= p; l <= r; l >>= 1, r >>= 1) {
      if (l & 1) {
        res = key(res, tree[l++]);
      }
      if ((r & 1) == 0) {
        res = key(res, tree[r--]);
      }
    }
    return res;
  }

  void update(int k, C x) {
    k ^= p;
    tree[k] = x;
    for (k >>= 1; k; k >>= 1) {
      tree[k] = key(tree[k << 1], tree[k << 1 | 1]);
    }
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n, m;
  cin >> n >> m;
  seg_tree<long long> dp(n, [](auto a, auto b) { return min(a, b); }, 1e18);
  dp.update(0, 0);
  vector<vector<pair<int, int>>> kek(n);
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    --l, --r;
    kek[r].emplace_back(l, c);
  }
  for (int r = 1; r < n; ++r) {
    long long mn = 1e18;
    for (auto [l, c] : kek[r]) {
      mn = min(mn, dp.get(l, r) + c);
    }
    dp.update(r, mn);
  }
  long long ans = dp.get(n - 1, n - 1);
  cout << (ans == 1e18 ? -1 : ans);

  return 0;
}