#include <bits/stdc++.h>
using namespace std;

#define int long long
using ll = long long;
#define itn int
#define For(i, a, b) for (int i = (a); i <= static_cast<int>(b); i++)
#define Forr(i, a, b) for (int i = (a); i >= static_cast<int>(b); i--)
#define rep(i, n) For(i, 0, n - 1)
#define repall(i, arr) for (auto& i : (arr))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define dump(x) cerr << #x << " = " << (x) << '\n'
#define dump2(x, y) \
    cerr << #x << " = " << (x) << " " << #y << " = " << (y) << '\n'
#define SZ(x) ((int)(x).size())
#define bit(n) (1LL << (n))
constexpr int MOD = 1e9 + 7;

template <typename T>
using pq = priority_queue<T>;
template <typename T>
using pqr = priority_queue<T, vector<T>, greater<T>>;
const int INF = LLONG_MAX / 2;
using P = pair<int, int>;
using vec = vector<int>;
template <typename T>
using mat = vector<vector<T>>;

// clang-format off
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) { rep (i, vec.size() - 1) 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) { rep (i, v.size()) cin >> v[i] >> w[i]; }
template <typename T>
void init(vector<T>& v) { rep (i, v.size()) cin >> v[i]; }
template <typename T>
T sum_(vector<T> vec, T init = 0) { return std::accumulate(all(vec), 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>
int index(const T& vec, const typename T::iterator it) { return distance(vec.begin(), it); }
template <typename T = int>
mat<T> mmat(int n, int m=0) { return mat<T>(n, vector<T>(m)); }
template <typename T = int>
vector<T> ivec(int n) { vector<T> v(n); init(v); return v; }
// clang-format on

void yn(bool tf) { print(tf ? "Yes" : "No"); }
void YN(bool tf) { print(tf ? "YES" : "NO"); }

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

struct RMQRUQ {
    int n;
    vector<int> dat, lazy;

    RMQRUQ() {}
    RMQRUQ(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        dat.assign(n * 2, INF);
        lazy.assign(n * 2, INF);
    }

    void eval(int len, int k) {
        if (lazy[k] == INF) 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] = INF;
    }

    // [a, b)
    ll update(int a, int b, ll x, int k, int l, int 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(int a, int b, ll x) { return update(a, b, x, 0, 0, n); }

    // [a, b)
    ll query(int a, int b, int k, int l, int r) {
        this->eval(r - l, k);
        if (b <= l || r <= a) return INF;
        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(int a, int b) { return query(a, b, 0, 0, n); }
};

// -------------------------------------------------------------------
// sort 1 2 3 4
// pqr 1 2 3 must impl >

struct Edge {
    int l;
    int r;
    int 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);
    int n, m;
    cin >> n >> m;

    vector<Edge> v;
    int l, r, c;
    rep (i, m) {
        cin >> l >> r >> c;
        v.pb(Edge{l, r, c});
    }
    sort(all(v));

    RMQRUQ tree(n + 2);
    tree.update(1, 2, 0);

    repall (e, v) { tree.update(e.l, e.r + 1, tree.query(e.l, e.l + 1) + e.c); }

    int ans = tree.query(n, n + 1);
    if (ans == INF)
        print(-1);
    else
        print(ans);

    return 0;
}

Measured memory_bytes: 8816