#include <bits/stdc++.h>
using namespace std;
const long long LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int mod = 1000000007;
struct Edge {
  int to;
  long long weight;
  Edge(int t, long long w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
int main() {
  int N, M;
  cin >> N >> M;
  Graph G(N);
  vector<pair<pair<int, int>, long long>> p(M);
  for (int i = 0; i < (M); ++i) {
    int L, R;
    long long C;
    cin >> L >> R >> C;
    --L;
    --R;
    p[i] = make_pair(make_pair(L, R), C);
  }
  sort((p).begin(), (p).end());
  vector<long long> dp(N, LINF);
  dp[0] = 0;
  int preL = 0;
  int preR = 0;
  for (int i = 0; i < (M); ++i) {
    int nowL = p[i].first.first;
    int nowR = p[i].first.second;
    long long nowC = p[i].second;
    if (preR < nowR) {
      for (int i = preR + 1; i <= nowR; ++i) {
        dp[i] = min(dp[i], dp[nowL] + nowC);
      }
    }
  }
  if (dp[N - 1] == LINF)
    cout << -1 << endl;
  else
    cout << dp[N - 1] << endl;
  return 0;
}
