#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
const long long linf = 1e18 + 7;
long long mx[maxn << 3], lazy[maxn << 3];
int le[maxn << 3], ri[maxn << 3];
vector<pair<int, int> > hve[maxn];
void push_up(int id) { mx[id] = max(mx[id << 1], mx[(id << 1) | 1]); }
void push_down(int id) {
  if (lazy[id] != -1) {
    mx[id << 1] = min(mx[id << 1], lazy[id]);
    mx[(id << 1) | 1] = min(mx[(id << 1) | 1], lazy[id]);
    lazy[id << 1] = min(lazy[id << 1], lazy[id]);
    lazy[(id << 1) | 1] = min(lazy[(id << 1) | 1], lazy[id]);
    lazy[id] = -1;
  }
}
void build(int l, int r, int id) {
  le[id] = l;
  ri[id] = r;
  lazy[id] = -1;
  if (l == r) {
    if (l == 1) {
      mx[id] = 0;
    } else {
      mx[id] = linf;
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, id << 1);
  build(mid + 1, r, (id << 1) | 1);
  push_up(id);
}
void changeit(int l, int r, int id, long long v) {
  int l1 = le[id], r1 = ri[id];
  if (l > r || l1 > r || l > r1) return;
  if (l <= l1 && r1 <= r) {
    mx[id] = min(mx[id], v);
    if (lazy[id] == -1)
      lazy[id] = v;
    else
      lazy[id] = min(lazy[id], v);
    return;
  }
  push_down(id);
  changeit(l, r, id << 1, v);
  changeit(l, r, (id << 1) | 1, v);
  push_up(id);
}
long long query(int p, int id) {
  int l1 = le[id], r1 = ri[id];
  if (l1 == r1) {
    return mx[id];
  }
  push_down(id);
  int mid = (l1 + r1) >> 1;
  long long re;
  if (p <= mid)
    re = query(p, id << 1);
  else
    re = query(p, (id << 1) | 1);
  push_up(id);
  return re;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int x, y, c;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &c);
    hve[x].push_back(make_pair(c, y));
  }
  build(1, n, 1);
  long long v;
  int prep, p;
  for (int i = 1; i <= n; i++) {
    v = query(i, 1);
    prep = i + 1;
    sort(hve[i].begin(), hve[i].end());
    for (int j = 0; j < hve[i].size(); j++) {
      p = hve[i][j].second;
      c = hve[i][j].first;
      if (p >= prep) {
        changeit(prep, p, 1, v + c);
        prep = p + 1;
      }
    }
  }
  long long ans = query(n, 1);
  if (ans == linf)
    puts("-1");
  else
    printf("%lld\n", ans);
  return 0;
}
