#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N, M;
  cin >> N >> M;
  vector<map<int, int>> edge(N);
  for (int i = 0; i < (N - 1); ++i) {
    edge[i + 1][i] = 0;
  }
  for (int i = 0; i < (M); ++i) {
    int L, R, C;
    cin >> L >> R >> C;
    --L;
    --R;
    if (edge[L].find(R) == edge[L].end()) {
      edge[L][R] = C;
    } else {
      edge[L][R] = min(C, edge[L][R]);
    }
  }
  vector<int> costTo(N, INT_MAX);
  costTo[0] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push({0, 0});
  while (!q.empty()) {
    auto c = q.top();
    q.pop();
    int cost = c.first;
    int curr = c.second;
    if (costTo[curr] < cost) continue;
    for (auto next : edge[curr]) {
      if (costTo[curr] + next.second < costTo[next.first]) {
        costTo[next.first] = costTo[curr] + next.second;
        q.push({costTo[next.first], next.first});
      }
    }
  }
  long long ans = ((costTo[N - 1] != INT_MAX) ? costTo[N - 1] : -1);
  cout << ans << endl;
  return 0;
}
