#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using pll = pair<long, long>;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pll>> Graph(n);
  for (int i = 0; i < (int)(n - 1); i++) {
    Graph.at(i + 1).push_back(pll(i, 0));
  }
  for (int i = 0; i < (int)(m); i++) {
    long l, r, c;
    cin >> l >> r >> c;
    Graph.at(l - 1).push_back(pll(r - 1, c));
  }
  priority_queue<pll> que;
  vector<long> dis(n, 1e18);
  dis.at(0) = 0;
  que.push(pll(0, 0));
  while (que.size()) {
    pll P = que.top();
    que.pop();
    long v = P.first;
    long d = P.second;
    if (d > dis.at(v)) continue;
    for (pll nP : Graph.at(v)) {
      long nv = nP.second;
      long cost = nP.first;
      if (d + cost < dis.at(nv)) {
        dis.at(nv) = d + cost;
        que.push(pll(dis.at(nv), nv));
      }
    }
  }
  if (dis.at(n - 1) == 1e18)
    cout << -1 << endl;
  else
    cout << dis.at(n - 1) << endl;
}
