#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  } else {
    return 0;
  }
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  } else {
    return 0;
  }
}
template <class T>
inline T GCD(T a, T b) {
  T c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
template <class T>
inline T LCM(T a, T b) {
  return a * b / GCD(a, b);
}
struct tri {
  long long r, l, c;
};
bool cmp(const tri& a, const tri& b) { return a.r < b.r; }
int main() {
  long long n, m;
  long long li, ri, ci;
  cin >> n >> m;
  vector<tri> v;
  vector<pair<long long, long long>> s;
  for (int i = 0; i < (m); i++) {
    cin >> li >> ri >> ci;
    v.push_back({ri, li, ci});
    s.push_back(make_pair(li, ri));
  }
  sort((v).begin(), (v).end(), cmp);
  sort((s).begin(), (s).end());
  vector<long long> to, cost;
  to.push_back(1ll);
  cost.push_back(0ll);
  for (int i = 0; i < (v.size()); i++) {
    int p = lower_bound((to).begin(), (to).end(), v[i].l) - to.begin();
    to.push_back(v[i].r);
    cost.push_back(cost[p] + v[i].c);
  }
  bool ans = 1;
  long long rr = 1;
  for (int i = 0; i < (s.size()); i++) {
    if (rr < s[i].first) ans = 0;
    rr = max(rr, s[i].second);
  }
  if (ans) {
    int p = lower_bound((to).begin(), (to).end(), n) - to.begin();
    long long mini = 10000000000000000;
    for (int i = p; i < to.size(); i++) {
      chmin(mini, cost[i]);
    }
    cout << mini << endl;
  } else {
    cout << -1 << endl;
  }
}
