#include <bits/stdc++.h>
using namespace std;
template <typename T>
using V = vector<T>;
template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, M;
  cin >> N >> M;
  vector<pair<pair<long long, long long>, long long>> edges(M);
  for (long long i = 0; i < (M); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    edges[i] = {pair<long long, long long>(r, l), c};
  }
  sort(edges.begin(), edges.end());
  V<long long> dp(N, 1e18);
  dp[N - 1] = 0;
  V<bool> visited(M);
  for (long long i = N - 1; i >= 0; i--) {
    if (dp[i] == 1e18) continue;
    pair<pair<long long, long long>, long long> f = {
        pair<long long, long long>(i - 1, 1e18), 1e18};
    auto it = lower_bound(edges.begin(), edges.end(), f);
    it++;
    long long ii = it - edges.begin();
    auto it2 = it;
    long long ii2 = ii;
    while (true) {
      visited[ii] = true;
      auto edge = *it;
      if (edge.first.second > i) break;
      long long l = edge.first.second;
      chmin(dp[l], dp[i] + edge.second);
      if (it == edges.end()) break;
      it++;
      ii++;
      if (visited[ii]) break;
    }
    ii2--;
    it2--;
    while (ii2 >= 0) {
      visited[ii2] = true;
      auto edge = *it2;
      if (edge.first.first < i) break;
      long long l = edge.first.second;
      chmin(dp[l], dp[i] + edge.second);
      if (it2 == edges.begin()) break;
      it2--;
      ii2--;
      if (visited[ii2]) break;
    }
  }
  long long res = dp[0] == 1e18 ? -1 : dp[0];
  cout << res << endl;
  return 0;
}
