#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define M 998244353
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n,m,i,j,c;
    cin>>n>>m;
    pair <pair<ll,ll> ,ll> q[m];
    for(i=0;i<m;i++)
        cin>>q[i].ff.ff>>q[i].ff.ss>>q[i].ss;
    sort(q,q+m);
    
    multiset <pair<ll,ll>> ms;
    multiset <ll> cst;
    j = 0;
    cst.insert(0);
    ms.insert(mp(1,0));
    for(i=1;i<=n;i++)
    {
        if(cst.size() == 0)
        {
            cout<<-1;
            return 0;
        }
        if(i == n)
        {
            cout<<*cst.begin();
            return 0;
        } 
        while(j<m and q[j].ff.ff == i)
        {
            c = *cst.begin();
            cst.insert(c+q[j].ss);
            ms.insert(mp(q[j].ff.ss, c+q[j].ss));
            j++;
        }
        while(ms.size() and ms.begin()->ff == i)
        {
            cst.erase(cst.find(ms.begin()->ss));
            ms.erase(ms.begin());
        }

    }
    return 0;
}
