#include <bits/stdc++.h>
using namespace std;
bool debug = false;
template <typename T>
struct pq_small {
  priority_queue<T, vector<T>, greater<T>> pq;
  void push(T x) { pq.push(x); }
  void pop() { pq.pop(); }
  int size() { return pq.size(); }
  bool empty() { return pq.empty(); }
  T top() { return pq.top(); }
};
int n, m;
vector<pair<int, long long>> ra[100005];
long long dis[100005];
pq_small<pair<int, long long>> kill;
set<long long> val;
const long long inf = 1e18;
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    ra[a].push_back(make_pair(b, c));
  }
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      dis[i] = 0;
    } else {
      if (val.empty()) {
        dis[i] = inf;
      } else {
        dis[i] = *val.begin();
      }
    }
    for (pair<int, int> x : ra[i]) {
      long long v = 0ll + x.second + dis[i];
      kill.push(make_pair(x.first, v));
      val.insert(v);
    }
    while (!kill.empty()) {
      pair<int, long long> last = kill.top();
      if (last.first == i) {
        val.erase(last.second);
        kill.pop();
      } else {
        break;
      }
    }
  }
  if (dis[n - 1] >= inf) {
    cout << "-1";
  } else {
    cout << dis[n - 1] << endl;
  }
  return 0;
}
