#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr long long MOD = 1e9 + 7;
template <typename T>
using pq = priority_queue<T>;
template <typename T>
using pqr = priority_queue<T, vector<T>, greater<T>>;
const long long INF = LLONG_MAX / 2;
using P = pair<long long, long long>;
using vec = vector<long long>;
template <typename T>
using mat = vector<vector<T>>;
template <typename T1, typename T2>
ostream& operator<<(ostream& stream, const pair<T1, T2>& p) {
  return stream << p.first << "," << p.second;
}
template <typename T>
void print(const vector<T> vec) {
  for (long long i = (0); i <= static_cast<long long>(vec.size() - 1 - 1); i++)
    cout << vec[i] << ' ';
  cout << vec[vec.size() - 1] << '\n';
}
template <typename Arg>
void print(const Arg arg) {
  cout << arg << '\n';
}
template <typename Head, typename... Args>
void print(const Head head, const Args... args) {
  cout << head << " ";
  print(args...);
}
template <typename T, typename U>
void init(vector<T>& v, vector<U>& w) {
  for (long long i = (0); i <= static_cast<long long>(v.size() - 1); i++)
    cin >> v[i] >> w[i];
}
template <typename T>
void init(vector<T>& v) {
  for (long long i = (0); i <= static_cast<long long>(v.size() - 1); i++)
    cin >> v[i];
}
template <typename T>
T sum_(vector<T> vec, T init = 0) {
  return std::accumulate((vec).begin(), (vec).end(), T(init));
}
template <typename T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
long long index(const T& vec, const typename T::iterator it) {
  return distance(vec.begin(), it);
}
template <typename T = long long>
mat<T> mmat(long long n, long long m = 0) {
  return mat<T>(n, vector<T>(m));
}
template <typename T = long long>
vector<T> ivec(long long n) {
  vector<T> v(n);
  init(v);
  return v;
}
void yn(bool tf) { print(tf ? "Yes" : "No"); }
void YN(bool tf) { print(tf ? "YES" : "NO"); }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
struct RMQRUQ {
  long long n;
  vector<long long> dat, lazy;
  RMQRUQ() {}
  RMQRUQ(long long n_) {
    n = 1;
    while (n < n_) n *= 2;
    dat.assign(n * 2, INT_MAX);
    lazy.assign(n * 2, INT_MAX);
  }
  void eval(long long len, long long k) {
    if (lazy[k] == INT_MAX) return;
    if (k * 2 + 1 < n * 2 - 1) {
      lazy[2 * k + 1] = min(lazy[k], lazy[2 * k + 1]);
      lazy[2 * k + 2] = min(lazy[k], lazy[2 * k + 2]);
    }
    dat[k] = min(lazy[k], dat[k]);
    lazy[k] = INT_MAX;
  }
  ll update(long long a, long long b, ll x, long long k, long long l,
            long long r) {
    this->eval(r - l, k);
    if (b <= l || r <= a) return dat[k];
    if (a <= l && r <= b) {
      lazy[k] = min(x, lazy[k]);
      return lazy[k];
    }
    return dat[k] = min(update(a, b, x, 2 * k + 1, l, (l + r) / 2),
                        update(a, b, x, 2 * k + 2, (l + r) / 2, r));
  }
  ll update(long long a, long long b, ll x) { return update(a, b, x, 0, 0, n); }
  ll query(long long a, long long b, long long k, long long l, long long r) {
    this->eval(r - l, k);
    if (b <= l || r <= a) return INT_MAX;
    if (a <= l && r <= b) return dat[k];
    ll vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  ll query(long long a, long long b) { return query(a, b, 0, 0, n); }
};
struct Edge {
  long long l;
  long long r;
  long long c;
};
bool operator<(const Edge& x, const Edge& y) {
  if (x.l != y.l) return x.l < y.l;
  return x.r < y.r;
}
signed main() {
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  vector<Edge> v;
  long long l, r, c;
  for (long long i = (0); i <= static_cast<long long>(m - 1); i++) {
    cin >> l >> r >> c;
    v.push_back(Edge{l, r, c});
  }
  sort((v).begin(), (v).end());
  RMQRUQ tree(n + 1);
  tree.update(1, 2, 0);
  for (auto& e : (v)) {
    tree.update(e.l + 1, e.r + 1, tree.query(e.l, e.l + 1) + e.c);
  }
  long long ans = tree.query(n, n + 1);
  if (ans == INT_MAX)
    print(-1);
  else
    print(ans);
  return 0;
}
