#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T> >;
template <typename T>
using WeightedGraph = vector<Edges<T> >;
using UnWeightedGraph = vector<vector<int> >;
template <typename T>
using Matrix = vector<vector<T> >;
vector<long long> bellman_ford(Edges<int> &edges, int V, int S) {
  vector<long long> path(V, 9223372036854775807);
  path[S] = 0;
  for (int i = 0; i < V; i++) {
    for (auto &e : edges) {
      if (path[e.src] == 9223372036854775807) continue;
      path[e.to] = min(path[e.to], path[e.src] + e.cost);
    }
  }
  for (auto &e : edges) {
    if (path[e.src] == 9223372036854775807) continue;
    if (path[e.src] + e.cost < path[e.to]) return vector<long long>();
  }
  return path;
}
void solve() {
  int N, M;
  cin >> N >> M;
  vector<tuple<int, int, int> > mat;
  vector<int> reach(N, 0);
  for (int i = 0; i < M; i++) {
    int A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    mat.emplace_back(C, A, B);
  }
  sort(mat.begin(), mat.end());
  int last_A = 0, last_B = 0;
  Edges<int> es;
  for (int i = 0; i < M; i++) {
    int A, B, C;
    A = get<1>(mat[i]);
    B = get<2>(mat[i]);
    C = get<0>(mat[i]);
    for (int i = A; i <= B; i++) {
      for (int j = i + 1; j <= B; j++) {
        if (reach[j] == 0) continue;
        es.emplace_back(i, j, C);
        cout << i << " " << j << " " << C << endl;
      }
    }
  }
  vector<long long> path = bellman_ford(es, N, 0);
  if (path.empty()) cout << -1 << endl;
  if (path[N - 1] == 9223372036854775807)
    cout << -1 << endl;
  else
    cout << path[N - 1] << endl;
  return;
}
int main(int argc, const char *argv[]) {
  solve();
  return 0;
}
