#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int main() {
  int n, m;
  cin >> n >> m;
  set<tuple<int, int, int> > eds;
  for (int i = 0; i < m; i++) {
    int l, r, v;
    cin >> l >> r >> v;
    l--;
    r--;
    eds.insert(make_tuple(l, r, v));
  }
  const long long inf = 1ll << 60;
  vector<long long> dist(n, inf);
  dist[0] = 0;
  set<pair<long long, int> > djk;
  djk.insert(make_pair(0ll, 0));
  while (!djk.empty()) {
    long long cs;
    int u;
    tie(cs, u) = *djk.begin();
    djk.erase(djk.begin());
    if (cs != dist[u]) continue;
    auto contains = [&](tuple<int, int, int> e, int x) {
      int l, r, v;
      tie(l, r, v) = e;
      return l <= x && x <= r;
    };
    while (!eds.empty() && contains(*eds.begin(), u)) {
      int l, r, v;
      tie(l, r, v) = *eds.begin();
      eds.erase(eds.begin());
      if (dist[r] > dist[u] + v) {
        dist[r] = dist[u] + v;
        djk.insert(make_pair(dist[r], r));
      }
    }
  }
  if (dist[n - 1] >= inf) dist[n - 1] = -1;
  cout << dist[n - 1] << endl;
  return 0;
}
