#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e6 + 1;
struct line {
  int l, r, w;
  bool operator<(const line& o) { return r < o.r; }
} lie[N];
int n, m;
ll dp[N];
vector<int> cur[N];
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1, l, r, w; i <= m; i++) {
    cin >> l >> r >> w;
    lie[i] = {l, r, w};
    cur[l].push_back(i);
  }
  memset(dp, 0x3f3f3f3f, sizeof dp);
  set<pair<ll, int>> active;
  ll ans = LLONG_MAX;
  lie[0] = {1, 1, 0};
  active.insert({0, 0});
  for (int i = 1; i <= n; i++) {
    while (!active.empty() && lie[active.begin()->second].r < i) {
      active.erase(active.begin());
    }
    for (auto& id : cur[i]) {
      if (active.empty()) {
        cout << -1;
        return 0;
      }
      auto t = active.begin();
      dp[id] = min(dp[id], t->first + lie[id].w);
      if (lie[id].r == n) {
        ans = min(ans, dp[id]);
      }
    }
    for (auto& id : cur[i]) {
      active.insert({dp[id], id});
    }
  }
  cout << ans;
}
