#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using Int = long long;
using uint = unsigned int;
using uInt = unsigned long long;
constexpr int mod = 1e9 + 7;
constexpr int INF32 = numeric_limits<int>::max();
constexpr Int INF64 = numeric_limits<Int>::max();
template <class T>
inline bool cmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool cmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int n, m, _, x, y, q;
string s, sb;
bool ok;
char ch;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(0);
  cin >> n >> m;
  Int dp[n];
  pair<pair<int, int>, int> ed[m];
  fill(dp, dp + n, INF64);
  dp[n - 1] = 0;
  for (int(i) = 0; i < m; ++i) {
    cin >> x >> y >> q;
    ed[i] = {pair<int, int>(--y, --x), q};
  }
  sort(ed, ed + m);
  for (int i = m - 1; i >= 0; --i) {
    if (i != m - 1 && ed[i + 1].first.first == ed[i].first.first &&
        ed[i].second < ed[i + 1].second)
      continue;
    x = ed[i].first.first, y = ed[i].first.second, q = ed[i].second;
    if (dp[x] == INF64) continue;
    for (int j = y; j < x; j++) {
      if (!cmin(dp[j], dp[x] + q)) break;
    }
  }
  cout << (dp[0] == INF64 ? -1 : dp[0]) << '\n';
}
