#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
struct edge {
  int l, r;
  long long c;
};
signed main() {
  int n, m;
  cin >> n >> m;
  vector<edge> g(m);
  for (int i = 0; i < m; ++i) cin >> g[i].l >> g[i].r >> g[i].c;
  vector<vector<edge>> fin(n + 1);
  for (int i = 0; i < m; ++i) {
    fin[g[i].r].push_back(g[i]);
  }
  vector<long long> dp(n + 1, 1000000000000000LL);
  dp[1] = 0;
  int ptr = 2;
  for (int i = 2; i <= n; ++i) {
    if (fin[i].size() == 0) continue;
    long long res = 1000000000000000LL;
    for (auto e : fin[i]) {
      res = min(res, dp[e.l] + e.c);
    }
    while (ptr <= i) dp[ptr++] = res;
  }
  cout << (dp[n] == 1000000000000000LL ? -1 : dp[n]) << endl;
  return 0;
}
