#include <bits/stdc++.h>
using namespace std;
const long long oo = 1LL << 60;
int n;
vector<pair<int, int>> a[100100];
vector<pair<long long, int>> out[100100];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int m;
  cin >> n >> m;
  while (m--) {
    int l, r, c;
    cin >> l >> r >> c;
    a[l].push_back({c, r});
  }
  set<pair<long long, int>> s;
  for (int i = 1; i <= n; i++) {
    long long dist = oo;
    if (i == 1)
      dist = 0;
    else if (s.empty())
      dist = oo;
    else
      dist = s.begin()->first;
    if (i == n) {
      cout << (dist == oo ? -1 : dist) << endl;
      return 0;
    }
    if (dist != oo) {
      sort(a[i].begin(), a[i].end());
      int r = i;
      for (auto u : a[i])
        if (u.second > r) {
          s.insert({dist + u.first, i});
          out[u.second].push_back({dist + u.first, i});
          r = u.second;
        }
    }
    for (auto u : out[i]) s.erase(u);
  }
}
