#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 5e15;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<map<long long, long long>> E(n);
  for (long long i = 1; i < n; i++) E[i][i - 1] = 0;
  for (long long i = 0; i < m; i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    E[l][r] = c;
  }
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  vector<long long> d(n, inf);
  q.push(pair<long long, long long>(0, 0));
  while (q.size()) {
    long long dis = q.top().first;
    long long f = q.top().second;
    q.pop();
    if (d[f] < dis) continue;
    d[f] = dis;
    for (auto p : E[f]) {
      long long ndis = dis + p.second;
      long long t = p.first;
      if (d[t] > ndis) {
        q.push(pair<long long, long long>(ndis, t));
      }
    }
  }
  if (d[n - 1] == inf) d[n - 1] = -1;
  cout << d[n - 1] << endl;
}
