#include <bits/stdc++.h>
using namespace std;
struct edge {
  int p;
  int w;
};
vector<vector<edge> > graph(100000);
vector<int> res(100000);
int v, e, r;
void dijkstra() {
  for (int i = 0; i < v; i++) res[i] = INT_MAX;
  res[r] = 0;
  priority_queue<pair<int, int> > que;
  que.push(make_pair(0, r));
  while (!que.empty()) {
    edge now;
    now.p = que.top().second;
    now.w = que.top().first * -1;
    que.pop();
    if (res[now.p] < now.w) continue;
    for (int i = 0; i < graph[now.p].size(); i++) {
      if (res[graph[now.p][i].p] > now.w + graph[now.p][i].w) {
        res[graph[now.p][i].p] = now.w + graph[now.p][i].w;
        que.push(make_pair(res[graph[now.p][i].p] * -1, graph[now.p][i].p));
      }
    }
  }
}
int main() {
  cin >> v >> e;
  for (int i = 0; i < e; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    s--, t--;
    edge memoa = {s, c};
    edge memob = {t, c};
    graph[s].push_back(memob);
  }
  e += v - 1;
  for (int i = 0; i < v - 1; i++) {
    edge memo = {i, 0};
    graph[i + 1].push_back(memo);
  }
  r = 0;
  dijkstra();
  if (res[v - 1] == INT_MAX)
    cout << -1 << endl;
  else
    cout << res[v - 1] << endl;
}
