#include <bits/stdc++.h>
using namespace std;
const long long int oo = 1e18;
const int MOD = 998244353;
const int N = 1e5 + 5;
vector<pair<int, int>> in[N];
vector<long long int> out[N];
long long int ans[N];
void solve() {
  int n, m, i;
  cin >> n >> m;
  for (i = 0; i <= n; i++) {
    out[i].clear();
    in[i].clear();
  }
  for (i = 0; i < m; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    in[l].push_back({c, r});
  }
  multiset<long long int> st;
  st.insert(0);
  out[1].push_back(0);
  ans[1] = 0;
  for (i = 1; i <= n; i++) {
    if (st.empty()) break;
    ans[i] = *st.begin();
    for (auto it : in[i]) {
      st.insert(ans[i] + it.first);
      out[it.second].push_back(ans[i] + it.first);
    }
    for (auto it : out[i]) {
      auto jt = st.find(it);
      st.erase(jt);
    }
  }
  if (i < n)
    cout << "-1\n";
  else
    cout << ans[n] << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  for (int tc = 1; tc <= t; ++tc) {
    solve();
  }
  return 0;
}
