#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T> >;
template <typename T>
using WeightedGraph = vector<Edges<T> >;
using UnWeightedGraph = vector<vector<int> >;
template <typename T>
using Matrix = vector<vector<T> >;
template <typename T>
vector<T> dijkstra(WeightedGraph<T> &g, int s) {
  const auto INF = numeric_limits<T>::max();
  vector<T> dist(g.size(), INF);
  using Pi = pair<T, int>;
  priority_queue<Pi, vector<Pi>, greater<Pi> > que;
  dist[s] = 0;
  que.emplace(dist[s], s);
  while (!que.empty()) {
    T cost;
    int idx;
    tie(cost, idx) = que.top();
    que.pop();
    if (dist[idx] < cost) continue;
    for (auto &e : g[idx]) {
      auto next_cost = cost + e.cost;
      if (dist[e.to] <= next_cost) continue;
      dist[e.to] = next_cost;
      que.emplace(dist[e.to], e.to);
    }
  }
  return dist;
}
void solve() {
  int N, L;
  cin >> N >> L;
  WeightedGraph<int> es(N);
  for (int i = 0; i < L; i++) {
    int A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    es[A].emplace_back(A, B, C);
  }
  for (int i = 1; i < N - 1; i++) {
    es[i + 1].emplace_back(i + 1, i, 0);
  }
  vector<int> path = dijkstra(es, 0);
  if (path[N - 1] == 2147483647)
    cout << -1 << endl;
  else {
    cout << path[N - 1] << endl;
  }
  return;
}
int main(int argc, const char *argv[]) {
  solve();
  return 0;
}
