#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  set<pair<long long, int>> paths;
  set<pair<int, pair<long long, int>>> eds;
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> res(n);
  for (int i = 0; i < (m); ++i) {
    int l, r, c;
    scanf("%d%d%d", &l, &r, &c);
    l--;
    r--;
    eds.insert({l, {c, r}});
  }
  paths.insert({0, 0});
  int curr = 0;
  auto it = eds.begin();
  long long cc = 0;
  while (curr < n - 1 and !paths.empty()) {
    res[curr] = cc;
    while (it != eds.end() and it->first <= curr) {
      paths.insert({cc + (it->second).first, (it->second).second});
      it++;
    }
    auto it2 = paths.begin();
    while (it2 != paths.end() and it2->second <= curr) paths.erase(it2++);
    int next = it2->second;
    cc = it2->first;
    for (int i = (curr + 1); i < (next + 1); ++i) {
      res[i] = cc;
    }
    curr = next;
  }
  if (curr == n - 1) {
    printf("%lld\n", cc);
  } else {
    printf("-1\n");
  }
}
