#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int l;
  int r;
  int c;
} path;
bool comp(const path &a, const path &b) { return (a.l) < (b.l); }
int main() {
  int N, M;
  cin >> N >> M;
  vector<path> P;
  for (int i = 0; i < M; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    P.push_back((path){l - 1, r - 1, c});
  }
  sort(P.begin(), P.end(), comp);
  for (int i = 0; i < M; i++) {
    cout << P[i].l << endl;
  }
  long dp[N];
  for (int i = 0; i < M; i++) {
    dp[i] = 1e15;
  }
  dp[0] = 0;
  int top = 0;
  for (int i = 0; i < M; i++) {
    if (top < P[i].l) {
      cout << -1 << endl;
      return 0;
    } else {
      for (int j = P[i].l; j <= P[i].r; j++) {
        dp[j] = min(dp[j], dp[P[i].l] + P[i].c);
      }
    }
  }
  if (dp[N - 1] == 1e15) cout << -1 << endl;
  cout << dp[N - 1] << endl;
  return 0;
}
