#include <bits/stdc++.h>
using namespace std;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  pair<pair<long long, long long>, long long> hen[100000];
  for (long long i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    hen[i].first.first = a;
    hen[i].first.second = b;
    hen[i].second = c;
  }
  sort(hen, hen + m);
  map<long long, long long> ans;
  ans[1] = 0;
  for (long long i = 0; i < m; i++) {
    long long p = 0;
    auto itr = ans.begin();
    for (; itr != ans.end(); itr++) {
      if (itr->first >= hen[i].first.first) {
        p = itr->first;
        break;
      }
    }
    if (p == 0) {
      cout << -1 << "\n";
      return 0;
    }
    if (ans.find(hen[i].first.second) != ans.end()) {
      ans[hen[i].first.second] =
          min(ans[p] + hen[i].second, ans[hen[i].first.second]);
    } else
      ans[hen[i].first.second] = ans[p] + hen[i].second;
  }
  auto itr2 = ans.begin();
  for (; itr2 != ans.end(); itr2++) {
    if (itr2->first >= n) {
      cout << itr2->second << "\n";
      return 0;
    }
  }
  cout << -1 << "\n";
  return 0;
}
