#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long to;
  long long cost;
};
vector<edge> G[200000];
long long d[200000];
signed main() {
  long long n, m, l, r, c;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      que;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].push_back({r, c});
    G[r].push_back({l, c});
  }
  for (int i = 1; i < n; i++) {
    G[i].push_back({i - 1, 0});
    d[i] = 1000000000;
  }
  d[0] = 1000000000;
  que.push(pair<long long, long long>(0, 0));
  while (!que.empty()) {
    pair<long long, long long> tmp = que.top();
    que.pop();
    long long v, dis;
    v = tmp.second;
    dis = tmp.first;
    if (d[v] <= dis) {
      continue;
    }
    d[v] = dis;
    for (int i = 0; i < G[v].size(); i++) {
      que.push({dis + G[v][i].cost, G[v][i].to});
    }
  }
  if (d[n - 1] == 1000000000) d[n - 1] = -1;
  cout << d[n - 1] << endl;
}
