#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int inf = 1e8;
const long long INF = 1e16;
const string alpha = "abcdefghijklmnopqrstuvwxyz";
struct edge {
  int from;
  long long cost;
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<edge> es[N];
  for (int i = 0; i < M; i++) {
    int L, R;
    long long C;
    cin >> L >> R >> C;
    L--, R--;
    edge e = {L, C};
    es[R].push_back(e);
  }
  long long dp[N];
  fill(dp, dp + N, INF);
  dp[0] = 0;
  int pre = 0;
  for (int i = 0; i < N; i++) {
    if (es[i].empty()) continue;
    for (auto &e : es[i]) {
      dp[i] = min(dp[i], dp[e.from] + e.cost);
    }
    for (int j = pre + 1; j <= i - 1; j++) dp[j] = dp[i];
    pre = i;
  }
  if (dp[N - 1] == INF)
    cout << -1 << endl;
  else
    cout << dp[N - 1] << endl;
}
