#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 4;
vector<pair<int, int>> add[N];
vector<long long> er[N];
long long dp[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    add[l].push_back({r, c});
  }
  multiset<long long> ms;
  for (int i = 1; i <= n; ++i) {
    for (int c : er[i]) ms.erase(ms.find(c));
    if (i > 1)
      dp[i] = (ms.empty() ? -LLONG_MAX / 2 : *ms.begin());
    else
      dp[i] = 0;
    for (auto p : add[i]) {
      ms.insert(dp[i] + p.second);
      er[p.first + 1].push_back(dp[i] + p.second);
    }
  }
  cout << (dp[n] < 0 ? -1 : dp[n]) << "\n";
}
