#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<pair<int, int>, int> p[100000];
long long d[100000];
int main() {
  cin >> n >> m;
  for (int i = 2; i <= n; i++) d[i] = -1;
  for (int i = 0; i < m; i++)
    cin >> p[i].first.first >> p[i].first.second >> p[i].second;
  sort(p, p + m);
  for (int i = 0; i < m; i++) {
    int x = p[i].first.first, y = p[i].first.second;
    while (d[x] == -1 && x < y) x++;
    if (x == y) {
      cout << -1;
      return 0;
    }
    if (d[y] == -1)
      d[y] = d[x] + p[i].second;
    else
      d[y] = min(d[x] + p[i].second, d[y]);
  }
  if (d[n] == -1)
    cout << -1;
  else
    cout << d[n];
}
