#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30);
const long long INFLL = (1ll << 60);
const long long MOD = (long long)(1e9 + 7);
void mul_mod(long long& a, long long b) {
  a *= b;
  a %= MOD;
}
void add_mod(long long& a, long long b) {
  b += MOD;
  a += b;
  a %= MOD;
}
long long segtree[816225][2], dp[225816];
bool delayed[816225];
void eval(int i, int l, int r) {
  if (delayed[i]) {
    segtree[i][0] = min(segtree[i][1], segtree[i][0]);
    if (r - l > 1) {
      segtree[2 * i + 1][1] = min(segtree[i][1], segtree[2 * i + 1][1]);
      delayed[2 * i + 1] = false;
      segtree[2 * i + 2][1] = min(segtree[i][1], segtree[2 * i + 2][1]);
      delayed[2 * i + 2] = false;
    }
    delayed[i] = false;
    segtree[i][1] = INFLL;
  }
}
void update(int a, int b, int i, int l, int r, long long v) {
  eval(i, l, r);
  if (b <= l || r <= a) {
    return;
  }
  if (a <= l && r <= b) {
    segtree[i][1] = min(v, segtree[i][1]);
    delayed[i] = true;
    eval(i, l, r);
  } else {
    update(a, b, 2 * i + 1, l, (l + r) / 2, v);
    update(a, b, 2 * i + 2, (l + r) / 2, r, v);
    segtree[i][0] = min(segtree[2 * i + 1][0], segtree[2 * i + 2][0]);
  }
}
long long query(int a, int b, int i, int l, int r) {
  long long vl, vr;
  eval(i, l, r);
  if (b <= l || r <= a) {
    return INFLL;
  }
  if (a <= l && r <= b) {
    return segtree[i][0];
  } else {
    vl = query(a, b, 2 * i + 1, l, (l + r) / 2);
    vr = query(a, b, 2 * i + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}
vector<pair<int, long long> > t[100100];
int main(void) {
  int n, m, l, r, i, j;
  long long c, ans = 0ll;
  fill(dp, dp + 225816, INFLL);
  fill(segtree[0], segtree[816225], INFLL);
  cin >> n >> m;
  for (i = 0; i < m; ++i) {
    cin >> l >> r >> c;
    t[r].push_back(pair<int, long long>(l - 1, c));
  }
  update(0, 1, 0, 0, n, 0ll);
  dp[1] = 0ll;
  for (r = 1; r <= n; ++r) {
    for (j = (t[r].size() - 1); j >= 0; --j) {
      l = t[r][j].first;
      c = t[r][j].second;
      dp[r] = min(dp[r], query(l, r, 0, 0, n) + c);
    }
    for (j = (t[r].size() - 1); j >= 0; --j) {
      l = t[r][j].first;
      update(l, r, 0, 0, n, dp[r]);
    }
  }
  cout << ((dp[n] < INFLL) ? dp[n] : (-1ll)) << endl;
  return 0;
}
