#include <bits/stdc++.h>
using namespace std;
using pll = pair<long long, long long>;
using vl = vector<long long>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  long long to;
  long long cost;
};
using Graph = vector<vector<Edge>>;
const long long MOD = 1000000007;
const double PI = 2.0 * asin(1.0);
const long long INF = 1L << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const long long MAX = (long long)pow(10, 7);
long long n, m;
vl l, r, c;
Graph G;
std::vector<long long> d;
void dijkstra(long long s) {
  std::priority_queue<pair<long long, long long>,
                      std::vector<pair<long long, long long>>,
                      std::greater<pair<long long, long long>>>
      que;
  for (long long i = 0; i < n; i++) d[i] = INF;
  d[s] = 0;
  que.push(pair<long long, long long>(0, s));
  while (!que.empty()) {
    pair<long long, long long> p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first) continue;
    for (long long i = 0; i < G[v].size(); i++) {
      Edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pair<long long, long long>(d[e.to], e.to));
      }
    }
  }
}
int solve() {
  cin >> n >> m;
  (l).resize(m);
  (r).resize(m);
  (c).resize(m);
  (d).resize(n);
  G.resize(n);
  vl goals;
  for (long long i = (0); i < (m); i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
    r[i]--;
    goals.push_back(r[i]);
  }
  goals.push_back(0);
  sort((goals).begin(), (goals).end());
  for (long long i = (0); i < (m); i++) {
    for (auto v : goals) {
      if (v > r[i] - 1) {
        break;
      }
      if (v < l[i]) {
        continue;
      }
      Edge e = {r[i], c[i]};
      G[v].push_back(e);
    }
  }
  dijkstra(0);
  if (d[n - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
  return 0;
}
int main() {
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  return end_solve;
}
