#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n, vector<int>(n, -1));
  int l, r, c;
  for (int i = (1); i < (n); i++) to[i][i - 1] = 0;
  for (int i = 0; i < (m); i++) {
    cin >> l >> r >> c;
    l--;
    r--;
    to[l][r] = c;
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.emplace(0, 0);
  vector<int> checked(n, 0);
  vector<long long> cost(n, LLONG_MAX);
  cost[0] = 0;
  while (!que.empty()) {
    auto v = que.top();
    que.pop();
    if (checked[v.second]) continue;
    checked[v.second] = 1;
    for (int i = 0; i < (n); i++) {
      if (to[v.second][i] == -1) continue;
      if (cost[i] > cost[v.second] + to[v.second][i]) {
        cost[i] = cost[v.second] + to[v.second][i];
        que.emplace(cost[i], i);
      }
    }
  }
  if (cost[n - 1] == LLONG_MAX) {
    cout << -1 << endl;
  } else {
    cout << cost[n - 1] << endl;
  }
  return 0;
}
