#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
struct Graph {
  Graph(int size) : adj(size, vector<pair<int, double>>()) { this->n = size; }
  void addEdge(int a, int b, double w) { adj[a].push_back(make_pair(b, w)); }
  void dijkstra(int a) {
    d = vector<double>(n, INF);
    d[a] = 0;
    priority_queue<pair<double, int>, vector<pair<double, int>>,
                   greater<pair<double, int>>>
        que;
    que.push(make_pair(0, a));
    while (!que.empty()) {
      int u = que.top().second;
      que.pop();
      for (auto i : adj[u]) {
        if (d[i.first] > d[u] + i.second) {
          d[i.first] = d[u] + i.second;
          que.push(make_pair(d[i.first], i.first));
        }
      }
    }
  }
  double dist(int a) { return d[a]; }

 private:
  int n;
  vector<vector<pair<int, double>>> adj;
  vector<double> d;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, l, r, c;
  cin >> n >> m;
  Graph G(n);
  for (int i = 0; i < (m); ++i) {
    cin >> l >> r >> c;
    --l;
    --r;
    for (int j = l; j < (r + 1); ++j) {
      G.addEdge(l, j, c);
    }
  }
  G.dijkstra(0);
  if (G.dist(n - 1) == INF)
    cout << -1 << endl;
  else
    cout << G.dist(n - 1) << endl;
}
