#include <bits/stdc++.h>
using namespace std;
void print(const map<int, long int>& mp) {
  for (const auto& x : mp) {
    printf("(%d %ld)", x.first, x.second);
  }
  printf("\n");
}
int main() {
  if (0) printf("*debug mode*\n");
  int n, m;
  cin >> n >> m;
  set<tuple<int, int, long int>> road;
  for (int i = 0; i < m; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    road.insert(make_tuple(s, t, c));
  }
  map<int, long int> mp;
  mp[1] = 0;
  for (const auto& x : road) {
    if (0) {
      printf("(%d,%d,%d):road\n", get<0>(x), get<1>(x), get<2>(x));
      print(mp);
    }
    auto its = mp.lower_bound(get<0>(x));
    if (its == mp.end()) {
      printf("-1");
      return 0;
    }
    auto itt = mp.lower_bound(get<1>(x));
    long int temp = (its->second) + get<2>(x);
    if (itt != mp.end() && (itt->second) < temp) continue;
    mp[get<1>(x)] = temp;
    if (0) {
      printf("add\n");
      print(mp);
    }
    itt = mp.lower_bound(get<1>(x));
    itt--;
    while ((itt->second) > temp) {
      itt = mp.erase(itt);
      itt--;
    }
    if (0) {
      printf("delete\n");
      print(mp);
    }
  }
  cout << mp[n];
}
