#include <bits/stdc++.h>
double EPS = 1e-9;
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
  if (x < y) x = y;
}
long long int MOD = 998244353;
int N, M;
vector<pair<pair<int, int>, long long> > datas;
bool check = true;
const int BT = 1 << 17;
int n;
struct segtree {
  vector<long long> seg;
  void init() {
    seg.resize(BT * 2);
    for (int(i) = 0; (i) < (int)(BT * 2 - 1); ++(i)) seg[i] = 1e15;
  }
  void add(int pos, long long x) {
    pos += BT - 1;
    seg[pos] = x;
    while (pos > 0) {
      pos = (pos - 1) / 2;
      seg[pos] = min(seg[pos * 2 + 1], seg[pos * 2 + 2]);
    }
  }
  long long minQ(int a, int b, int k = 0, int l = 0, int r = BT) {
    if (b <= l || r <= a) return 1e15;
    if (a <= l && r <= b) return seg[k];
    return min(minQ(a, b, k * 2 + 1, l, (l + r) / 2),
               minQ(a, b, k * 2 + 2, (l + r) / 2, r));
  }
} segm;
int main() {
  cin >> N >> M;
  for (int(i) = 0; (i) < (int)(M); ++(i)) {
    int l, r;
    long long cos;
    cin >> l >> r >> cos;
    datas.push_back(make_pair((make_pair((l), (r))), (cos)));
  }
  sort((datas).begin(), (datas).end());
  bool flag = true;
  if (datas[0].first.first != 1) flag = false;
  bool check = false;
  for (int(i) = 0; (i) < (int)(M - 1); ++(i)) {
    if (datas[i].first.second < datas[i + 1].first.first) flag = false;
    if (datas[i].first.second == N) check = true;
  }
  if (!check) flag = false;
  if (flag) {
    segm.init();
    segm.add(0, 0);
    for (int(i) = 0; (i) < (int)(M); ++(i)) {
      int x = datas[i].first.first, y = datas[i].first.second;
      long long cos = datas[i].second;
      long long hoge = segm.minQ(x - 1, y);
      long long fuga = segm.minQ(y - 1, y);
      segm.add(y - 1, min(hoge + cos, fuga));
    }
    cout << segm.minQ(N - 1, N) << endl;
  } else {
    cout << -1 << endl;
  }
}
