#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, l, r, mx = 1, ka = 1;
  long long c, dp[100005];
  vector<pair<pair<int, int>, long long> > g;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> l >> r >> c;
    g.push_back(make_pair(make_pair(r, l), c));
  }
  sort(g.begin(), g.end());
  for (i = 1; i <= n; i++) dp[i] = 1e16;
  dp[1] = 0;
  for (i = 0; i < m; i++) {
    l = g[i].first.second;
    r = g[i].first.first;
    c = g[i].second;
    if (dp[l] != LLONG_MAX) {
      if (ka != r)
        for (j = ka + 1; j <= r; j++) dp[j] = min(dp[j], dp[l] + c);
      else
        for (j = ka; j <= r; j++) dp[j] = min(dp[j], dp[l] + c);
      ka = r;
    }
  }
  if (dp[n] == LLONG_MAX)
    cout << "-1" << endl;
  else
    cout << dp[n] << endl;
  return 0;
}
