#include <bits/stdc++.h>
using namespace std;
const int N = 100005, M = 100005;
const long long inf = 1000000000000ll;
int n, m;
vector<pair<int, long long> > vec[N];
long long f[N];
long long sgt1[N << 2], sgt2[N << 2];
multiset<long long> sgt[N << 2];
void build(int l, int r, int now) {
  int mid = l + r >> 1;
  sgt1[now] = sgt2[now] = inf;
  sgt[now].insert(inf);
  if (l < r) build(l, mid, now << 1), build(mid + 1, r, now << 1 | 1);
}
void pushup(int now) {
  sgt1[now] = min(sgt1[now << 1], sgt1[now << 1 | 1]);
  sgt2[now] = min(min(sgt2[now << 1], sgt2[now << 1 | 1]),
                  sgt1[now] + *sgt[now].begin());
}
void change1(int left, int right, int l, int r, int now, long long val) {
  if (l == left && r == right) {
    sgt[now].insert(val);
    sgt2[now] = sgt1[now] + *sgt[now].begin();
    if (l < r)
      sgt2[now] = min(sgt2[now], sgt2[now << 1]),
      sgt2[now] = min(sgt2[now], sgt2[now << 1 | 1]);
  } else {
    int mid = l + r >> 1;
    if (right <= mid)
      change1(left, right, l, mid, now << 1, val);
    else if (left > mid)
      change1(left, right, mid + 1, r, now << 1 | 1, val);
    else
      change1(left, mid, l, mid, now << 1, val),
          change1(mid + 1, right, mid + 1, r, now << 1 | 1, val);
    pushup(now);
  }
}
void change2(int left, int right, int l, int r, int now, long long val) {
  if (l == left && r == right) {
    sgt[now].erase(sgt[now].find(val));
    sgt2[now] = sgt1[now] + *sgt[now].begin();
    if (l < r)
      sgt2[now] = min(sgt2[now], sgt2[now << 1]),
      sgt2[now] = min(sgt2[now], sgt2[now << 1 | 1]);
  } else {
    int mid = l + r >> 1;
    if (right <= mid)
      change2(left, right, l, mid, now << 1, val);
    else if (left > mid)
      change2(left, right, mid + 1, r, now << 1 | 1, val);
    else
      change2(left, mid, l, mid, now << 1, val),
          change2(mid + 1, right, mid + 1, r, now << 1 | 1, val);
    pushup(now);
  }
}
void change3(int pos, int l, int r, int now, long long val) {
  int mid = l + r >> 1;
  if (l == r)
    sgt1[now] = min(sgt1[now], val),
    sgt2[now] = min(sgt2[now], sgt1[now] + *sgt[now].begin());
  else {
    if (pos <= mid)
      change3(pos, l, mid, now << 1, val);
    else
      change3(pos, mid + 1, r, now << 1 | 1, val);
    pushup(now);
  }
}
pair<long long, long long> query(int left, int right, int l, int r, int now) {
  if (l == left && r == right)
    return make_pair(sgt1[now], sgt2[now]);
  else {
    int mid = l + r >> 1;
    pair<long long, long long> pi;
    if (right <= mid)
      pi = query(left, right, l, mid, now << 1);
    else if (left > mid)
      pi = query(left, right, mid + 1, r, now << 1 | 1);
    else {
      pair<long long, long long> pi1 = query(left, mid, l, mid, now << 1);
      pair<long long, long long> pi2 =
          query(mid + 1, right, mid + 1, r, now << 1 | 1);
      pi.first = min(pi1.first, pi2.first);
      pi.second = min(pi1.second, pi2.second);
    }
    pi.second = min(pi.second, pi.first + *sgt[now].begin());
    return pi;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  build(1, n, 1);
  for (int i = 1; i <= m; i++) {
    int l, r;
    long long c;
    scanf("%d%d%lld", &l, &r, &c);
    vec[r].push_back(make_pair(l, c));
    change1(l, r, 1, n, 1, c);
  }
  f[1] = 0ll, change3(1, 1, n, 1, f[1]);
  for (int i = 2; i <= n; i++) {
    f[i] = query(1, i - 1, 1, n, 1).second;
    change3(i, 1, n, 1, f[i]);
    for (int j = 0; j < vec[i].size(); j++)
      change2(vec[i][j].first, i, 1, n, 1, vec[i][j].second);
  }
  if (f[n] > inf / 2)
    puts("-1");
  else
    printf("%lld\n", f[n]);
  return 0;
}
