#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int a, b;
  long long c;
  long long d;
  Edge() {}
  friend istream& operator>>(istream& in, Edge& e) {
    in >> e.a >> e.b >> e.c;
    --e.a;
    --e.b;
    e.d = numeric_limits<long long>::max();
    return in;
  }
  bool operator<(const Edge& e) const { return d > e.d; }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  int m;
  cin >> N >> m;
  vector<vector<Edge>> edges(N);
  for (int i = 0; i < m; ++i) {
    Edge e;
    cin >> e;
    edges[e.a].push_back(e);
  }
  vector<long long> dists(N, numeric_limits<long long>::max());
  priority_queue<Edge> pq;
  int until = 0;
  auto set_dist = [&](int d) {
    dists[until] = d;
    for (auto edge : edges[until]) {
      edge.d = d + edge.c;
      pq.push(edge);
    }
    ++until;
  };
  set_dist(0);
  while (!pq.empty()) {
    auto edge = pq.top();
    pq.pop();
    while (until <= edge.b) {
      set_dist(edge.d);
    }
  }
  if (until < N) {
    cout << -1 << endl;
  } else {
    cout << dists[N - 1] << endl;
  }
  return 0;
}
