#include <bits/stdc++.h>
using namespace std;
void dout() { cerr << endl; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    --l, --r;
    g[l].emplace_back(r, c);
    g[r].emplace_back(l, c);
  }
  for (int i = 1; i < n; ++i) {
    g[i].emplace_back(i - 1, 0);
  }
  vector<long long> dist(n, 1e18);
  dist[0] = 0;
  set<pair<int, int>> st;
  st.insert({dist[0], 0});
  while (!st.empty()) {
    auto [d, v] = *st.begin();
    st.erase(st.begin());
    if (d > dist[v]) {
      continue;
    }
    for (auto [to, w] : g[v]) {
      if (dist[to] > dist[v] + w) {
        dist[to] = dist[v] + w;
        st.insert({dist[to], to});
      }
    }
  }
  if (dist[n - 1] > (long long)1e18 - 7) {
    cout << -1 << "\n";
  } else {
    cout << dist[n - 1] << "\n";
  }
  return 0;
}
