#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > v[100100];
long long dp[100100];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < (int)(m); i++) {
    int l, r, c;
    cin >> l >> r >> c;
    v[l].push_back(pair<long long, long long>(c, r));
  }
  for (int i = 2; i <= n; i++) {
    dp[i] = 1e18;
  }
  priority_queue<pair<long long, long long> > que;
  que.push(pair<long long, long long>(0, 1));
  while (!que.empty()) {
    pair<long long, long long> t = que.top();
    int i = t.second;
    que.pop();
    int r = i + 1;
    sort(v[i].begin(), v[i].end());
    for (auto p : v[i]) {
      while (r <= p.second) {
        if (dp[r] > dp[i] + p.first) {
          dp[r] = dp[i] + p.first;
          que.push(pair<long long, long long>(dp[i] + p.first, r));
        }
        r++;
      }
    }
  }
  if (dp[n] == 1e18) {
    cout << -1 << endl;
  } else {
    cout << dp[n] << endl;
  }
  return 0;
}
