#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
struct edge {
  int from, to;
  long long cost;
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N, M;
  cin >> N >> M;
  vector<long long> D(N, 1LL << 62);
  D[0] = 0;
  vector<edge> A(M);
  for (int i = (int)(0); i < (int)(M); ++i) {
    int from, to;
    long long cost;
    cin >> from >> to >> cost;
    from--;
    to--;
    A[i] = {from, to, cost};
  }
  sort((A).begin(), (A).end(), [](const edge& a, const edge& b) {
    if (a.to != b.to)
      return a.to < b.to;
    else {
      return a.cost < b.cost;
    }
  });
  int cur = 1;
  for (int i = (int)(0); i < (int)(M); ++i) {
    if (cur <= A[i].to) {
      for (int j = (int)(0); j < (int)(A[i].to + 1); ++j) {
        D[j] = min(D[j], D[A[i].from] + A[i].cost);
      }
    }
    cur = A[i].to;
  }
  if (D[N - 1] == 1LL << 62) {
    output(-1);
    return 0;
  }
  output(D[N - 1]);
  return 0;
}
