#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long INFLL = 2e18;
const int MOD = 1000000007;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<long long>> elase_list(n);
  priority_queue<tuple<int, int, long long>, vector<tuple<int, int, long long>>,
                 greater<tuple<int, int, long long>>>
      que;
  for (int i = 0; i < m; i++) {
    int l, r;
    long long c;
    cin >> l >> r >> c;
    r--;
    que.push(make_tuple(l, r, c));
  }
  set<long long> costs;
  vector<long long> dp(n, INFLL);
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    while (que.size()) {
      int l, r;
      long long c;
      tie(l, r, c) = que.top();
      if (l != i) break;
      que.pop();
      if (dp[i - 1] != INFLL) {
        costs.insert(dp[i - 1] + c);
        elase_list[r].push_back(dp[i - 1] + c);
      }
    }
    if (costs.size()) dp[i] = *costs.begin();
    for (int j = 0; j < elase_list[i].size(); j++) {
      auto p = costs.find(elase_list[i][j]);
      costs.erase(p);
    }
  }
  if (dp[n - 1] != INFLL)
    cout << dp[n - 1] << endl;
  else
    cout << -1 << endl;
}
