#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
using namespace std;
const int N = 1e5 + 5;

int n, m;
vector <pair <ll, int> > s[N];

ll d[N];
priority_queue <pair <ll, int> > pq;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int l, r, c;

    cin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        cin >> l >> r >> c;
        s[l].pb({c, r});
    }

    for(int i = 1; i <= n; i++)
        sort(s[i].begin(), s[i].end());

    int done_to = 1;
    for(auto t: s[1])
        pq.push({-t.first, t.second});
    d[n] = -1;
    while(!pq.empty())
    {
        pair <ll, int> t = pq.top();
        pq.pop();

        for(; done_to + 1 <= t.second;)
        {
            done_to++;
            d[done_to] = -t.first;
            for(auto p: s[done_to])
                pq.push({t.first - p.first, p.second});
        }
    }

    cout << d[n] << endl;

    return 0;
}
