#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 10;
vector<vector<pair<long long, long long>>> gra(MAXN);
long long dist[MAXN];
const long long INF = 1e18;
int32_t main() {
  long long n, m;
  cin >> n >> m;
  priority_queue<pair<pair<long long, long long>, long long>,
                 vector<pair<pair<long long, long long>, long long>>,
                 greater<pair<pair<long long, long long>, long long>>>
      pq;
  for (long long i = 0; i < m; i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    pair<pair<long long, long long>, long long> tmp;
    tmp.first.first = l;
    tmp.first.second = r;
    tmp.second = c;
    pq.push(tmp);
  }
  vector<long long> mv;
  long long count = 0;
  while (!pq.empty()) {
    pair<pair<long long, long long>, long long> t = pq.top();
    pq.pop();
    long long l = t.first.first;
    long long r = t.first.second;
    long long w = t.second;
    l -= 1;
    r -= 1;
    if (l == 0) {
      if (!count) {
        mv.push_back(0);
        count++;
      }
      auto it = upper_bound(mv.begin(), mv.end(), r);
      mv.insert(it, r);
      gra[l].emplace_back(make_pair(r, w));
    } else {
      long long pos1 = lower_bound(mv.begin(), mv.end(), l) - mv.begin();
      if (pos1 == (long long)mv.size()) {
        cout << -1 << endl;
        return 0;
      }
      long long pos2 = upper_bound(mv.begin(), mv.end(), r) - mv.begin();
      for (long long poi = pos1; poi < pos2; poi++) {
        long long a = mv[poi];
        gra[a].emplace_back(make_pair(r, w));
      }
      auto it = upper_bound(mv.begin(), mv.end(), r);
      mv.insert(it, r);
    }
  }
  for (long long i = 0; i < MAXN; i++) dist[i] = INF;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      pq2;
  long long u = 0;
  dist[u] = 0;
  pq2.push(make_pair(0, u));
  while (!pq2.empty()) {
    pair<long long, long long> ii = pq2.top();
    pq2.pop();
    long long dis = ii.first;
    long long u = ii.second;
    if (dis > dist[u]) continue;
    for (long long i = 0; i < (long long)gra[u].size(); i++) {
      long long nx = gra[u][i].first;
      long long we = gra[u][i].second;
      if (dist[nx] > dist[u] + we) {
        dist[nx] = dist[u] + we;
        pq2.push(make_pair(dist[nx], nx));
      }
    }
  }
  if (dist[n - 1] >= INF)
    cout << -1 << endl;
  else
    cout << dist[n - 1] << endl;
  return 0;
}
