#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, q, L[N], R[N], C[N];
long long dp[N];
vector<int> V[N], U[N];
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d%d", &L[i], &R[i], &C[i]);
    V[L[i]].push_back(i);
    U[R[i]].push_back(i);
  }
  dp[1] = 0;
  set<pair<long long, int> > ST;
  for (int i = 1; i <= n; i++) {
    if (i > 1 && !ST.size()) return !printf("-1\n");
    if (i > 1) dp[i] = ST.begin()->first;
    for (int id : V[i]) ST.insert({C[id] + dp[i], id});
    for (int id : U[i]) ST.erase({C[id] + dp[L[id]], id});
  }
  printf("%d\n", dp[n]);
  return 0;
}
