#include <bits/stdc++.h>
using namespace std;
struct SegTree {
  int n, start;
  vector<vector<pair<int, int>>> graph;
  vector<int> stop;
  SegTree(int n, int m) : n(n), graph(8 * n + m), stop(n) { init(1, 0, n - 1); }
  void add(int a, int b, int c) { graph[a].emplace_back(b, c); }
  void init(int node, int b, int e) {
    if (b == e) {
      add(2 * node, 2 * node + 1, 0);
      add(2 * node + 1, 2 * node, 0);
      if (b == 0) start = node * 2;
      stop[b] = node * 2 + 1;
    } else {
      int m = (b + e) / 2;
      add(4 * node + 0, 2 * node + 0, 0);
      add(4 * node + 2, 2 * node + 0, 0);
      add(2 * node + 1, 4 * node + 1, 0);
      add(2 * node + 1, 4 * node + 3, 0);
      init(node * 2, b, m);
      init(node * 2 + 1, m + 1, e);
    }
  }
  void add(int node, int b, int e, int l, int r, int to, int c) {
    l = max(l, b);
    r = min(r, e);
    if (l > r) return;
    if (b == l && e == r) {
      add(2 * node, to, c);
      add(to, 2 * node + 1, 0);
      return;
    }
    int m = (b + e) / 2;
    add(node * 2, b, m, l, r, to, c);
    add(node * 2 + 1, m + 1, e, l, r, to, c);
  }
  void Add(int l, int r, int idx, int c) {
    add(1, 0, n - 1, l, r, idx + 4 * n, c);
  }
  long long Solve() {
    const long long kInf = 2e18;
    vector<long long> dist(graph.size(), kInf);
    priority_queue<pair<long long, int>> pq;
    auto push = [&](int node, long long d) {
      if (dist[node] <= d) return;
      dist[node] = d;
      pq.emplace(-d, node);
    };
    push(start, 0);
    while (!pq.empty()) {
      int node;
      long long d;
      tie(d, node) = pq.top();
      pq.pop();
      d = -d;
      if (dist[node] != d) continue;
      for (auto e : graph[node]) {
        int vec, cost;
        tie(vec, cost) = e;
        push(vec, cost + d);
      }
    }
    if (dist[stop[n - 1]] == kInf) return -1;
    return dist[stop[n - 1]];
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  SegTree st(n, m);
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    cerr << "ADDING " << i << endl;
    st.Add(l - 1, r - 1, i, c);
  }
  cerr << "SOLVING" << endl;
  cout << st.Solve() << endl;
  return 0;
}
