#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int n_max = 1e5 + 10;
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(tail) != 0) cout << ' ';
  print(forward<Tail>(tail)...);
}
template <class T>
void print(vector<T>& vec) {
  for (auto& a : vec) {
    cout << a;
    if (&a != &vec.back()) cout << ' ';
  }
  cout << endl;
}
template <class T>
void print(vector<vector<T>>& df) {
  for (auto& vec : df) {
    print(vec);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<long long, long long>>> graph(n);
  long long INF = INT_MAX;
  vector<long long> dp(n, INF);
  for (int i = 0; i < m; i++) {
    long long l, r;
    cin >> l >> r;
    long long c;
    cin >> c;
    l--;
    r--;
    graph[l].emplace_back(pair<long long, long long>(r, c));
  }
  dp[0] = 0;
  bool ok = true;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      pq;
  for (int i = 0; i < n; i++) {
    pair<long long, long long> temp = pair<long long, long long>(INF, -1);
    if (pq.size() > 0) temp = pq.top();
    while (temp.second < i && pq.size() > 0) {
      pq.pop();
      temp = pq.top();
    }
    if (i == 0 || temp.second >= i)
      dp[i] = min(dp[i], temp.first);
    else {
      ok = false;
      break;
    }
    for (auto j : graph[i]) {
      pq.push(pair<long long, long long>(dp[i] + j.second, j.first));
    }
  }
  cout << (ok ? dp[n - 1] : -1) << endl;
}
