#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5, mod = 1e17;
long long dist[N];
vector<pair<int, int> > adj[N];
long long n, m;
void dij() {
  for (int i = 0; i < n; i++) dist[i] = mod;
  set<pair<int, int> > s;
  dist[0] = 0;
  s.insert({0, 0});
  while (!s.empty()) {
    int v = (*s.begin()).second;
    s.erase(s.begin());
    for (auto e : adj[v]) {
      int w = e.second, u = e.first;
      if (dist[u] > dist[v] + w) {
        dist[u] = dist[v] + w;
        s.insert({dist[u], u});
      }
    }
  }
}
int main() {
  cin >> n >> m;
  vector<pair<pair<int, int>, int> > edge;
  for (int i = 0; i < n - 1; i++) {
    adj[i + 1].push_back({i, 0});
  }
  for (int i = 0; i < m; i++) {
    int v, u, w;
    cin >> v >> u >> w;
    v--, u--;
    adj[v].push_back({u, w});
    adj[u].push_back({v, w});
  }
  dij();
  if (dist[n - 1] == mod)
    cout << -1 << "\n";
  else
    cout << dist[n - 1] << "\n";
}
