#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define endl "\n"
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

struct LazyRMQ
{
private:
    ll n;
    vector<ll> node, lazy;
    vector<bool> lazyFlag;

public:
    LazyRMQ(vector<ll> v)
    {
        ll sz = (ll)v.size();
        n = 1;
        while (n < sz)
        {
            n *= 2;
        }
        node.resize(2 * n - 1);
        lazy.resize(2 * n - 1, LLINF);
        lazyFlag.resize(2 * n - 1, false);
        for (ll i = 0; i < sz; i++)
        {
            node[i + n - 1] = v[i];
        }
        for (ll i = n - 2; i >= 0; i--)
        {
            node[i] = min(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    void eval(ll now, ll l, ll r)
    {
        if (lazyFlag[now])
        {
            node[now] = min(lazy[now], node[now]);
            if (r - l > 1)
            {
                // lazy[2 * now + 1] = lazy[2 * now + 2] = lazy[now];
                chmin(lazy[2 * now + 1], lazy[now]);
                chmin(lazy[2 * now + 2], lazy[now]);
                lazyFlag[2 * now + 1] = lazyFlag[2 * now + 2] = true;
            }
            lazyFlag[now] = false;
        }
    }

    void update(ll a, ll b, ll x, ll now = 0, ll l = 0, ll r = -1)
    {
        if (r < 0)
        {
            r = n;
        }
        eval(now, l, r);
        if (b <= l || r <= a)
        {
            return;
        }
        if (a <= l && r <= b)
        {
            lazy[now] = x;
            lazyFlag[now] = true;
            eval(now, l, r);
        }
        else
        {
            update(a, b, x, 2 * now + 1, l, (l + r) / 2);
            update(a, b, x, 2 * now + 2, (l + r) / 2, r);
            node[now] = min(node[2 * now + 1], node[2 * now + 2]);
        }
    }

    ll getmin(ll a, ll b, ll now = 0, ll l = 0, ll r = -1)
    {
        if (r < 0)
        {
            r = n;
        }
        eval(now, l, r);
        if (b <= l || r <= a)
        {
            return LLINF;
        }
        if (a <= l && r <= b)
        {
            return node[now];
        }
        ll vl = getmin(a, b, 2 * now + 1, l, (l + r) / 2);
        ll vr = getmin(a, b, 2 * now + 2, (l + r) / 2, r);
        return min(vl, vr);
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, M;
    cin >> N >> M;
    vector<pair<pair<ll, ll>, ll>> LR(M);
    for (ll i = 0; i < M; i++)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        LR[i] = make_pair(make_pair(a, b), c);
    }
    sort(ALL(LR));
    LazyRMQ dp(vector<ll>(101010, LLINF));
    dp.update(0, 1, 0);
    for (ll i = 0; i < M; i++)
    {
        ll l = LR[i].first.first;
        ll r = LR[i].first.second;
        ll c = LR[i].second;
        dp.update(l + 1, r + 1, dp.getmin(l, l + 1) + c);
    }
    ll ans = dp.getmin(N - 1, N);
    cout << (ans == LLINF ? -1 : ans) << endl;
}

Measured memory_bytes: 8808