#include <bits/stdc++.h>
using namespace std;
const long long INF = 10000000000000;
template <typename T>
struct Edge {
  int to;
  T cost;
};
template <typename T>
struct WeightedGraph {
  int n;
  vector<vector<Edge<T>>> g;
  WeightedGraph(int n) : n(n) { g.resize(n); }
  void init(int n_) {
    n = n_;
    g.resize(n_);
  }
  void add_edge(int from, int to, T cost) {
    g[from].push_back((Edge<T>){to, cost});
  }
};
template <typename T>
vector<T> Dijkstra(WeightedGraph<T> &g, int s) {
  int n = g.n;
  vector<T> d(n);
  fill(d.begin(), d.end(), INF);
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;
  d[s] = 0;
  que.push(pair<T, int>(0, s));
  while (que.size()) {
    pair<T, int> nowp = que.top();
    que.pop();
    if (d[nowp.second] < nowp.first) continue;
    for (Edge<T> e : g.g[nowp.second]) {
      if (d[e.to] > d[nowp.second] + e.cost) {
        d[e.to] = d[nowp.second] + e.cost;
        que.push(pair<T, int>(d[e.to], e.to));
      }
    }
  }
  return d;
}
int main() {
  int n, m;
  cin >> n >> m;
  WeightedGraph<long long> g(n);
  int b[100005]{0};
  for (int i = 0; i < m; i++) {
    int l, r;
    long long c;
    cin >> l >> r >> c;
    l--;
    r--;
    g.add_edge(l, r, c);
    b[l]++;
    b[r]--;
  }
  int k = 0;
  for (int i = 0; i < n; i++) {
    k += b[i];
    if (k) g.add_edge(i + 1, i, 0);
  }
  vector<long long> d = Dijkstra(g, 0);
  if (d[n - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
}
