#include <bits/stdc++.h>
using namespace std;
long long big = LLONG_MAX / 100;
long long seg[262144], lseg[262144];
void upq(int s, int t, long long v, int k, int l, int r) {
  if (r < s || t < l) {
    return;
  }
  if (s <= l && r <= t) {
    seg[k] = v;
    return;
  }
  if (seg[k] != big) {
    seg[k * 2] = seg[k];
    seg[k * 2 + 1] = seg[k];
    seg[k] = big;
  }
  upq(s, t, v, k * 2, l, (l + r) / 2);
  upq(s, t, v, k * 2 + 1, (l + r) / 2 + 1, r);
}
long long getq(int x) {
  x += 131072;
  while (x) {
    if (seg[x] != big) {
      return seg[x];
    }
    x /= 2;
  }
  return big;
}
int bs(long long x, int ng, int ok) {
  if (ok - ng == 1) return ok;
  int mid = (ng + ok) / 2;
  if (getq(mid) > x) {
    ok = mid;
  } else {
    ng = mid;
  }
  return bs(x, ng, ok);
}
int main() {
  for (int i = 0; i < 262144; i++) seg[i] = big;
  int N, M;
  cin >> N >> M;
  long long L, R, C;
  vector<tuple<long long, long long, long long>> V(M);
  for (int i = 0; i < M; i++) {
    cin >> L >> R >> C;
    L--;
    R--;
    V[i] = make_tuple(L, R, C);
  }
  sort(V.begin(), V.end());
  long long val, ind;
  upq(0, 0, 0, 1, 0, 131071);
  for (int i = 0; i < M; i++) {
    tie(L, R, C) = V[i];
    val = getq(L);
    if (val == big) continue;
    val += C;
    ind = bs(val, -1, N);
    if (ind <= R) {
      upq(ind, R, val, 1, 0, 131071);
    }
  }
  if (getq(N - 1) == big)
    cout << -1 << endl;
  else
    cout << getq(N - 1) << endl;
}
