#include <bits/stdc++.h>
using namespace std;
const int mod1 = 998244353, mod2 = 1e9 + 7;
const int MAX_N = 100005, MAX_M = 200010;
vector<pair<long long, long long>> adj[MAX_N];
long long dist[MAX_N];
long long N, M;
void Solve() {
  cin >> N >> M;
  for (long long i = 2; i <= N; i++) {
    adj[i].emplace_back(i - 1, 0ll);
    dist[i] = LLONG_MAX;
  }
  for (long long i = 1, u, v, c; i <= M; i++) {
    cin >> u >> v >> c;
    adj[u].emplace_back(v, c);
    adj[v].emplace_back(u, c);
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      bfs;
  bfs.push(make_pair(0, 1));
  while (!bfs.empty()) {
    long long x = bfs.top().second, d = bfs.top().first;
    bfs.pop();
    if (d == dist[x]) {
      for (auto v : adj[x]) {
        if (dist[v.first] > dist[x] + v.second) {
          dist[v.first] = dist[x] + v.second;
          bfs.push(make_pair(dist[v.first], v.first));
        }
      }
    }
  }
  cout << ((dist[N] != LLONG_MAX) ? dist[N] : -1);
}
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int TC = 1;
  while (TC--) Solve();
  return 0;
}
