#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
const long long int mod = 1000000007ll;
const long long int smod = 998244353ll;
const long long int inf = 10000000000000007ll;
const double eps = 1e-10;
const long long int MAXN = 100005;
long long int n, m;
long long int dp[MAXN];
tuple<long long int, long long int, long long int> e[MAXN];
vector<long long int> q[MAXN];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    long long int l, r, c;
    cin >> l >> r >> c;
    e[i] = make_tuple(l, r, c);
  }
  for (long long int i = 0; i < n; i++) {
    long long int l, r, c;
    tie(l, r, c) = e[i];
    q[l].push_back(i);
  }
  for (long long int i = 0; i < n; i++) {
    long long int l, r, c;
    tie(l, r, c) = e[i];
    q[r].push_back(i);
  }
  dp[1] = 0;
  for (long long int i = 2; i <= n; i++) {
    dp[i] = inf;
  }
  map<long long int, long long int> z;
  for (long long int i = 1; i <= n; i++) {
    if (!z.empty()) {
      dp[i] = min(dp[i], z.begin()->first);
    }
    for (long long int j = 0; j < q[i].size(); j++) {
      long long int l, r, c;
      tie(l, r, c) = e[q[i][j]];
      long long int t = dp[l] + c;
      if (i == l) {
        z[t] += 1;
      } else if (i == r) {
        z[t] -= 1;
        if (!z[t]) {
          z.erase(t);
        }
      } else {
        throw 1;
      }
    }
  }
  if (dp[n] >= inf) {
    cout << -1 << endl;
  } else {
    cout << dp[n] << endl;
  }
  return 0;
}
