#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int> > adj[100005];
bool vis[100005];
long long int dis[100005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int edge, vertex;
  cin >> vertex >> edge;
  long long int i, c, u, v;
  for (i = 1; i <= edge; i++) {
    cin >> u >> v >> c;
    adj[u].push_back({v, c});
    adj[v].push_back({u, c});
  }
  for (i = 2; i <= vertex; i++) {
    adj[i].push_back({i - 1, 0});
  }
  priority_queue<pair<long long int, long long int>,
                 vector<pair<long long int, long long int> >,
                 greater<pair<long long int, long long int> > >
      q;
  for (i = 1; i <= vertex; i++) {
    dis[i] = INT_MAX;
  }
  dis[1] = 0;
  q.push({dis[1], 1});
  while (!q.empty()) {
    pair<long long int, long long int> temp;
    temp = q.top();
    q.pop();
    u = temp.second;
    long long int cost = temp.first;
    if (vis[u]) continue;
    vis[u] = 1;
    for (auto x : adj[u]) {
      v = x.first;
      c = x.second;
      if (dis[v] <= c + cost) continue;
      dis[v] = c + cost;
      q.push({dis[v], v});
    }
  }
  if (dis[vertex] >= INT_MAX) dis[vertex] = -1;
  cout << dis[vertex] << endl;
  return 0;
}
