#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<tuple<int, int, long long>> v;
  for (int i = 0; i < m; i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    v.emplace_back(l - 1, r - 1, c);
  }
  sort(v.begin(), v.end());
  long long d[100010], INF = 1e18;
  set<long long> s;
  s.insert(0);
  s.insert(INF);
  for (int i = 1; i < n; i++) d[i] = INF;
  d[0] = 0;
  for (int i = 0; i < m; i++) {
    int k = get<0>(v[i]);
    decltype(s)::iterator l = s.lower_bound(k);
    if (i != m - 1 && get<0>(v[i]) == get<0>(v[i + 1]) &&
        get<2>(v[i]) >= get<2>(v[i + 1]))
      continue;
    if (*l == INF) continue;
    d[get<1>(v[i])] = min(d[get<1>(v[i])], d[*l] + get<2>(v[i]));
    s.insert(get<1>(v[i]));
  }
  if (d[n - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
}
