#include <bits/stdc++.h>
using namespace std;
struct node {
  long long l, r, c;
};
bool comp(node a, node b) { return a.l < b.l; }
void updateRangeUtil(long long si, long long ss, long long se, long long us,
                     long long ue, long long diff, long long tree[],
                     long long lazy[]) {
  if (us > ue) return;
  if (lazy[si] != 10000000000000000) {
    tree[si] = min(tree[si], lazy[si]);
    if (ss != se) {
      lazy[si * 2 + 1] = min(lazy[2 * si + 1], lazy[si]);
      lazy[si * 2 + 2] = min(lazy[2 * si + 2], lazy[si]);
    }
    lazy[si] = 10000000000000000;
  }
  if (ss > se || ss > ue || se < us) return;
  if (ss >= us && se <= ue) {
    tree[si] = min(tree[si], diff);
    if (ss != se) {
      lazy[si * 2 + 1] = min(diff, lazy[si]);
      lazy[si * 2 + 2] = min(diff, lazy[si]);
    }
    return;
  }
  long long mid = (ss + se) / 2;
  updateRangeUtil(si * 2 + 1, ss, mid, us, ue, diff, tree, lazy);
  updateRangeUtil(si * 2 + 2, mid + 1, se, us, ue, diff, tree, lazy);
  tree[si] = min(tree[si * 2 + 1], tree[si * 2 + 2]);
}
long long getSumUtil(long long ss, long long se, long long qs, long long qe,
                     long long si, long long tree[], long long lazy[]) {
  if (qs > qe) return 10000000000000000;
  if (lazy[si] != 10000000000000000) {
    tree[si] = min(tree[si], lazy[si]);
    if (ss != se) {
      lazy[si * 2 + 1] = min(lazy[2 * si + 1], lazy[si]);
      lazy[si * 2 + 2] = min(lazy[2 * si + 2], lazy[si]);
    }
    lazy[si] = 10000000000000000;
  }
  if (ss > se || ss > qe || se < qs) return 10000000000000000;
  if (ss == se) return tree[si];
  long long mid = (ss + se) / 2;
  if (qs > mid)
    return getSumUtil(mid + 1, se, qs, qe, 2 * si + 2, tree, lazy);
  else
    return getSumUtil(ss, mid, qs, qe, 2 * si + 1, tree, lazy);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, m;
  cin >> n >> m;
  node hold[m];
  for (long long i = 0; i < m; i++) {
    cin >> hold[i].l >> hold[i].r >> hold[i].c;
    hold[i].l--;
    hold[i].r--;
  }
  long long ht = ceil(log2((long double)n));
  ht = (1ll << (ht + 1)) - 1;
  long long st[ht], lazy[ht];
  for (long long i = 0; i < ht; i++)
    st[i] = 10000000000000000, lazy[i] = 10000000000000000;
  sort(hold, hold + m, comp);
  updateRangeUtil(0, 0, n - 1, 0, 0, 0, st, lazy);
  for (long long i = 0; i < m; i++) {
    long long l1 = hold[i].l;
    long long r1 = hold[i].r;
    long long temp = getSumUtil(0, n - 1, l1, l1, 0, st, lazy);
    updateRangeUtil(0, 0, n - 1, l1 + 1, r1, temp + hold[i].c, st, lazy);
  }
  long long ans = getSumUtil(0, n - 1, n - 1, n - 1, 0, st, lazy);
  if (ans >= 10000000000000000)
    cout << -1;
  else
    cout << ans;
  return 0;
}
