#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 7;
class Dijkstra {
 private:
  vector<vector<pair<long long, int>>> graph;
  vector<long long> total_cost;
  vector<vector<int>> shortest_path;

 public:
  Dijkstra(vector<vector<pair<long long, int>>> graph, int start, int size,
           bool path_check = false) {
    priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                   greater<pair<long long, int>>>
        q;
    vector<long long> cost(size, INF);
    vector<vector<int>> path(size);
    if (path_check)
      for (int i = 0; i < size; i++) path[i].push_back(i);
    cost[start] = 0;
    q.push(make_pair(0ll, 0));
    while (!q.empty()) {
      auto now = q.top();
      q.pop();
      int prev = now.second;
      for (auto next : graph[prev]) {
        if (cost[next.second] <= cost[now.second] + next.first) continue;
        cost[next.second] = cost[now.second] + next.first;
        if (path_check)
          path[next.second].insert(path[next.second].begin(),
                                   path[now.second].begin(),
                                   path[now.second].end());
        q.push(next);
      }
    }
    total_cost = cost;
    if (path_check) shortest_path = path;
  }
  vector<long long> get_cost() { return total_cost; }
  vector<vector<int>> get_path() { return shortest_path; }
};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<long long, int>>> graph(n);
  for (int i = 0; i < m; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    graph[l].push_back(make_pair(c, r));
  }
  for (int i = 1; i < n; i++) graph[i].push_back(make_pair(0, i - 1));
  Dijkstra g(graph, 0, n);
  if (g.get_cost()[n - 1] == INF)
    cout << -1 << endl;
  else
    cout << g.get_cost()[n - 1] << endl;
}
