#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<pair<int, int>, long long>> LRC(M);
  for (int i = 0; i < (M); i++)
    cin >> LRC[i].first.first >> LRC[i].first.second >> LRC[i].second;
  sort(LRC.begin(), LRC.end());
  vector<long long> ans(N, -1);
  if (LRC[0].first.first == 1)
    ans[0] = 0;
  else {
    cout << -1 << endl;
    return 0;
  }
  int u = -1;
  int v = -1;
  for (int i = 0; i < M; i++) {
    long long dd = 0;
    if (u != LRC[i].first.first) {
      u = LRC[i].first.first;
      v = LRC[i].first.second;
      if (ans[u - 1] != -1)
        dd = ans[u - 1] + LRC[i].second;
      else {
        cout << -1 << endl;
        return 0;
      }
      for (int j = u; j < v; j++) {
        if (ans[j] == -1) {
          ans[j] = dd;
        } else {
          ans[j] = min(dd, ans[j]);
        }
      }
    } else {
      v = LRC[i].first.second;
      if (ans[u - 1] != -1)
        dd = ans[u - 1] + LRC[i].second;
      else {
        cout << -1 << endl;
        return 0;
      }
      for (int j = v; j < LRC[i].first.second; j++) {
        if (ans[j] == -1) {
          ans[j] = dd;
        } else {
          ans[j] = min(dd, ans[j]);
        }
      }
    }
  }
  cout << ans[N - 1] << endl;
  return 0;
}
