#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
#define lcm(x, y) ((x) * (y) / __gcd((x), (y)))
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define debug(x) cout << #x << " : " << x << endl
#define part cout << "----------------------------------\n";

#define fastinput                     \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

vector<LL> lazy;
vector<priority_queue<pair<LL, LL>>> q;
const LL INF = 10000000000000000;
LL extract;

void build_tree(LL v, LL tl, LL tr)
{
    if (tl == tr)
    {
        if (tl == 0)
        {
            // t[v] = 0;
            //cout<<"found it\n";
           // debug(v);
            lazy[v] = 0;
        }
        else
        {
            //t[v] = INF;
            lazy[v] = INF;
        }
    }
    else
    {
        LL tm = (tl + tr) / 2;
        build_tree(2 * v, tl, tm);
        build_tree(2 * v + 1, tm + 1, tr);
        lazy[v] = INF;
    }
}

void push_value(LL v)
{
    lazy[2 * v] = min(lazy[v], lazy[2 * v]);
    lazy[2 * v + 1] = min(lazy[v], lazy[2 * v + 1]);
    lazy[v] = INF;
}

void update_range(LL v, LL tl, LL tr, LL left, LL right, LL value)
{
    if (right < left)
    {
        return;
    }
    else if (tl == left && tr == right)
    {
        lazy[v] = min(lazy[v], value);
    }
    else
    {
        LL tm = (tl + tr) / 2;
        if (lazy[v] != INF)
        {
            push_value(v);
        }
        update_range(2 * v, tl, tm, left, min(tm, right), value);
        update_range(2 * v + 1, tm + 1, tr, max(left, tm + 1), right, value);
    }
}

void find_value(LL v, LL tl, LL tr, LL pos)
{
    if (tl == tr)
    {
        extract = lazy[v];
    }
    else
    {
        LL tm = (tl + tr) / 2;
        if (lazy[v] != INF)
        {
            push_value(v);
        }
        if(pos<=tm)
        {
            find_value(2*v,tl,tm,pos);
        }
        else
        {
            find_value(2*v+1,tm+1,tr,pos);
        }
        
    }
}

int main()
{
    fastinput;
    LL n, i, j, k, m, temp;
    cin >> n >> m;
    LL left, right, cost;
    const LL INF = 10000000000000000;
    //  t.resize(4 * n + 1);
    lazy.assign(4 * n + 1, -1);
    q.resize(n + 1);
    while (m--)
    {
        cin >> left >> right >> cost;
        left--;
        right--;
        q[left].push({cost, right});
    }
    build_tree(1,0,n-1);

    /*for(i=0;i<4*n+1;i++)
    {
        cout<<"lazy["<<i<<" is "<<lazy[i]<<endl;
    }*/

    for (i = 0; i < n; i++)
    {        
        find_value(1,0,n-1,i);
        LL dis_now=extract;
        //debug(i);
       // debug(dis_now);
        while(!q[i].empty())
        {
            cost=q[i].top().first;
            LL proposed=dis_now+cost;
            LL right=q[i].top().second;
          //  debug(cost);
           // debug(proposed);
          //  debug(right);
            q[i].pop();
            if(i+1<=right)
            {
                update_range(1,0,n-1,i+1,right,proposed);
            }
        }    
       // part;
    }

    find_value(1,0,n-1,n-1);
    if(extract==INF)
    {
        cout<<-1<<endl;
    }
    else
    {
        cout<<extract<<endl;
    }
    

    return 0;
}


Measured memory_bytes: 9472