#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll inf = static_cast<ll>(1e17);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e17;
ll mod = static_cast<ll>(1e9 + 7);
std::ostream& endn(std::ostream& os) { return os.put(os.widen('\n')); }
template <class T>
constexpr int sz(const T& a) {
  return (int)a.size();
}
template <class T, class... Args>
constexpr T mins(T& a, Args... args) {
  return a = min<T>({a, args...});
}
template <class T, class... Args>
constexpr T maxs(T& a, Args... args) {
  return a = max<T>({a, args...});
}
int n, m;
vector<pair<int, ll>> g[100005];
ll d[100005];
void solve() {
  cin >> n >> m;
  for (int i = 0; i < (int)(n - 1); ++i) g[i + 1].emplace_back(i, 0);
  for (int i = 0; i < (int)(m); ++i) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    --l;
    --r;
    g[l].emplace_back(r, c);
  }
  fill(d, d + n, inf);
  priority_queue<pair<ll, int>> que;
  que.emplace(0LL, 0);
  d[0] = 0;
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    auto v = p.second;
    if (d[v] < p.first) continue;
    for (int i = 0; i < (int)(g[v].size()); ++i) {
      auto e = g[v][i];
      if (d[v] + e.second < d[e.first]) {
        d[e.first] = d[v] + e.second;
        que.emplace(d[e.first], e.first);
      }
    }
  }
  if (d[n - 1] != inf)
    cout << d[n - 1] << endn;
  else
    cout << -1 << endn;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  cout << flush;
  return 0;
}
