#include <bits/stdc++.h>
using namespace std;
using vl = vector<long long>;
using vvl = vector<vl>;
constexpr int MOD = 1000000007;
using long long = long long;
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<long long> L(M), R(M), C(M);
  for (long long i = 0; i < (long long)M; i++) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--;
    R[i]--;
  }
  vector<tuple<int, long long, int>> path(M);
  for (long long i = 0; i < (long long)M; i++) {
    path[i] = make_tuple(L[i], C[i], R[i]);
  }
  sort(path.begin(), path.end());
  vector<long long> dist(N, (1ll << 60));
  dist[0] = 0;
  for (long long i = 0; i < (long long)M; i++) {
    if (i > 0 and get<0>(path[i]) == get<0>(path[i - 1])) {
      for (int j = get<2>(path[i - 1]) + 1; j <= get<2>(path[i]); j++) {
        dist[j] = min(dist[j], dist[get<0>(path[i])] + get<1>(path[i]));
      }
    } else {
      for (int j = get<0>(path[i]) + 1; j <= get<2>(path[i]); j++) {
        dist[j] = min(dist[j], dist[get<0>(path[i])] + get<1>(path[i]));
      }
    }
  }
  if (dist[N - 1] == (1ll << 60)) {
    cout << -1 << endl;
  } else {
    cout << dist[N - 1] << endl;
  }
  return 0;
}
