#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
const long long int INF = 1e15;
const long long int MAX_N = 200010;
vector<pair<long long int, long long int> > G[MAX_N];
vector<long long int> dijkstra(long long int start) {
  vector<long long int> dist(MAX_N, INF);
  dist[start] = 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> > >
      que;
  que.push(make_pair(0, start));
  while (!que.empty()) {
    long long int cost, w;
    cost = que.top().first, w = que.top().second;
    que.pop();
    if (dist[w] < cost) continue;
    for (auto tmp : G[w]) {
      long long int v = tmp.first, time = tmp.second;
      if (dist[v] > dist[w] + time) {
        dist[v] = dist[w] + time;
        que.push(make_pair(dist[v], v));
      }
    }
  }
  return dist;
}
long long int n, m;
long long int u[100010], v[100010], c[100010];
signed main(void) {
  cin >> n >> m;
  long long int data[100010];
  for (int i = 0; i < (100010); i++) data[i] = 0;
  for (int i = 0; i < (m); i++) {
    cin >> u[i] >> v[i] >> c[i];
    u[i]--;
    v[i]--;
    data[u[i]]++;
    data[v[i]]--;
    G[u[i]].push_back(make_pair(v[i], c[i]));
  }
  for (int i = 0; i < (100010); i++) {
    if (data[i] > 0) {
      G[i + 1].push_back(make_pair(i, 0));
    }
  }
  auto dist1 = dijkstra(0);
  long long int ans = dist1.at(n - 1);
  if (ans != INF)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
