#include <bits/stdc++.h>
using namespace std;
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
const int N = 1e5;
int n, m;
set<pair<pair<int, int>, int>> edges;
priority_queue<pair<long long, pair<int, int>>> q;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0, l, r, c; i < m; i++) {
    scanf("%d%d%d", &l, &r, &c);
    edges.insert({{r - 1, l - 1}, c});
  }
  q.push({0, {0, 0}});
  while (!q.empty()) {
    int l = q.top().second.second;
    int r = q.top().second.first;
    long long dis = -q.top().first;
    q.pop();
    if (r == n - 1) {
      printf("%lld\n", dis);
      return 0;
    }
    while (edges.size()) {
      auto it = edges.lower_bound({{l, -1}, -1});
      if (it == edges.end() && it->first.second > r) break;
      q.push({-(dis + (it->second)), it->first});
      edges.erase(it);
    }
  }
  puts("-1");
}
