#include <bits/stdc++.h>
using namespace std;
class Edge {
  unsigned long long to;
  unsigned long long cost;

 public:
  Edge() : to(ULLONG_MAX), cost(ULLONG_MAX) {}
  Edge(unsigned long long t, unsigned long long c) {
    to = t;
    cost = c;
  }
  unsigned long long To() { return to; }
  unsigned long long Cost() { return cost; }
};
class Node {
  unsigned long long node_no;
  unsigned long long min_cost;
  bool done;
  vector<Edge> to_cost;

 public:
  Node() : node_no(ULLONG_MAX), min_cost(ULLONG_MAX), done(false) {}
  void push_edge(unsigned long long t, unsigned long long c) {
    to_cost.push_back(Edge(t, c));
  }
  void set_no(unsigned long long n) { node_no = n; }
  unsigned long long get_no() { return node_no; }
  void set_cost(unsigned long long c) { min_cost = c; }
  unsigned long long Min_cost() { return min_cost; }
  void set_done() { done = true; }
  bool is_Done() { return done; }
  vector<Edge>::size_type count_Edge() { return to_cost.size(); }
  Edge Return_Edge(vector<Edge>::size_type i) { return to_cost[i]; }
  friend bool operator<(const Node& a, const Node& b) {
    return a.min_cost > b.min_cost;
  }
};
int main() {
  unsigned long long N, M;
  cin >> N >> M;
  vector<Node> NodeA;
  for (unsigned long long i = 0; i < N; i++) {
    NodeA.push_back(Node());
    NodeA[i].set_no(i);
    if (i != 0) {
      NodeA[i].push_edge(i - 1ULL, 0ULL);
    }
  }
  for (unsigned long long i = 0; i < M; i++) {
    unsigned long long L, R, C;
    cin >> L >> R >> C;
    NodeA[L - 1].push_edge(R - 1, C);
  }
  NodeA[0].set_cost(0ULL);
  priority_queue<pair<unsigned long long, unsigned long long>,
                 vector<pair<unsigned long long, unsigned long long>>,
                 greater<pair<unsigned long long, unsigned long long>>>
      search_list;
  search_list.push(make_pair(0ULL, 0ULL));
  while (search_list.empty() == false) {
    Node tmp = NodeA[search_list.top().second];
    NodeA[search_list.top().second].set_done();
    search_list.pop();
    vector<Edge>::size_type max_count = tmp.count_Edge();
    for (vector<Edge>::size_type i = 0; i < max_count; i++) {
      unsigned long long to = tmp.Return_Edge(i).To();
      unsigned long long cost = tmp.Return_Edge(i).Cost();
      if (NodeA[to].is_Done() == false) {
        if (NodeA[to].Min_cost() > tmp.Min_cost() + cost) {
          NodeA[to].set_cost(tmp.Min_cost() + cost);
        }
        search_list.push(make_pair(NodeA[to].Min_cost(), to));
      }
    }
  }
  if (NodeA[N - 1].Min_cost() == ULLONG_MAX) {
    cout << -1;
    return 0;
  }
  cout << NodeA[N - 1].Min_cost();
  return 0;
}
