#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long mod = 1e9 + 7;
const long long big = 1e18;
const double PI = 2 * asin(1);
int main() {
  int N, M;
  cin >> N >> M;
  vector<tuple<int, long long, int> > arr(M);
  int L, R;
  long long C;
  for (int i = 0; i < M; ++i) {
    cin >> L >> R >> C;
    L--;
    R--;
    arr[i] = make_tuple(L, C, R);
  }
  sort(arr.begin(), arr.end());
  bool can = true;
  vector<long long> ans(N);
  for (int i = 0; i < N; ++i) ans[i] = big;
  ans[0] = 0;
  int index;
  for (int i = 0; i < M; ++i) {
    L = get<0>(arr[i]);
    C = get<1>(arr[i]);
    R = get<2>(arr[i]);
    if (ans[L] == big) {
      can = false;
      break;
    }
    index =
        lower_bound(ans.begin() + L, ans.begin() + R, ans[L] + C) - ans.begin();
    if (index > R) continue;
    for (int j = index; j <= R; ++j) {
      ans[j] = min(ans[j], ans[L] + C);
    }
  }
  if (can)
    cout << ans[N - 1] << endl;
  else
    cout << -1 << endl;
}
