#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]);
  }
  for (int i = 0; i < n; ++i) {
    sort(fin[i].begin(), fin[i].end(), [](edge e1, edge e2) {
      if (e1.l != e2.l) return e1.l < e2.l;
      return e1.c < e2.c;
    });
  }
  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;
    for (int j = 0; j < fin[i].size(); ++j) {
      int mx = i + 1;
      int nx = j + 1;
      while (nx < fin[i].size() &&
             dp[fin[i][nx].l] + fin[i][nx].c >= dp[fin[i][j].l] + fin[i][j].c)
        ++nx;
      if (nx != fin[i].size()) mx = fin[i][nx].l;
      while (ptr < mx) {
        dp[ptr] = dp[fin[i][j].l] + fin[i][j].c;
        ++ptr;
      }
      j = nx - 1;
    }
  }
  cout << (dp[n] >= 1000000000000000LL ? -1 : dp[n]) << endl;
  return 0;
}
