#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int l[maxn], r[maxn], c[maxn];
vector<pair<int, int> > itvs;
vector<int> st[maxn];
struct BIT {
  long long bit[maxn];
  void add(int x, long long v) {
    while (x < maxn) {
      bit[x] = min(bit[x], v);
      x += x & -x;
    }
  }
  long long sum(int x) {
    long long ret = 0x3f3f3f3f3f3f3f3fll;
    while (x > 0) {
      ret = min(ret, bit[x]);
      x -= x & -x;
    }
    return ret;
  }
} tree;
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &l[i], &r[i], &c[i]);
    itvs.push_back(make_pair(l[i], 0));
    itvs.push_back(make_pair(r[i], 1));
  }
  sort(itvs.begin(), itvs.end());
  itvs.erase(unique(itvs.begin(), itvs.end()), itvs.end());
  for (int i = 0; i < m; i++) {
    l[i] = lower_bound(itvs.begin(), itvs.end(), make_pair(l[i], 0)) -
           itvs.begin();
    st[l[i]].push_back(i);
    r[i] = lower_bound(itvs.begin(), itvs.end(), make_pair(r[i], 1)) -
           itvs.begin();
  }
  memset(tree.bit, 0x3f, sizeof(tree.bit));
  tree.add(itvs.size(), 0);
  long long ans = 0x3f3f3f3f3f3f3f3fll;
  for (int i = 0; i < itvs.size(); i++) {
    int pos = itvs[i].first;
    int op = itvs[i].second;
    if (!i && pos != 1) {
      printf("-1\n");
      return 0;
    }
    if (op == 0) {
      long long val = tree.sum(itvs.size() - i);
      if (val >= 0x3f3f3f3f3f3f3f3fll) continue;
      for (int j = 0; j < st[i].size(); j++) {
        int rht = r[st[i][j]];
        tree.add(itvs.size() - rht + 1, val + c[st[i][j]]);
      }
    } else {
      if (pos == n) {
        ans = min(ans, tree.sum(itvs.size() - i + 1));
      }
    }
  }
  if (ans == 0x3f3f3f3f3f3f3f3fll) ans = -1;
  printf("%lld\n", ans);
  return 0;
}
