#pragma comment(linker, "/stack:200000000")
        #pragma GCC optimize ("Ofast")
        #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
        #include <iostream>
        #include <iomanip>
        #include <sstream>
        #include <cstring>
        #include <vector>
        #include <deque>
        #include <queue>
        #include <set>
        #include <map>
        #include <valarray>
        #include <iterator>
        #include <functional>
        #include <limits>
        #include <algorithm>
        #include <numeric>
        #include <cmath>
        #include <cassert>
        #include <unordered_map>
        #include <unordered_set>
        #include <stack>
        using namespace std;
        #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
         #define trace(x) cerr << #x << ": " << x << " " << endl;
     
        typedef int64_t ll;
     
        #define endl '\n'
        #define int ll
        ll mod=998244353;
        ll mod1=1e9+5;
        ll power(ll a,ll b)
        {
            if(b==0) return 1;
            else if(b%2==0)
                return power((((a%mod)*(a%mod))%mod),b/2)%mod;
            else return ((a%mod)*(power((((a%mod)*(a%mod))%mod),b/2)%mod))%mod;
        }
        vector<pair<int,int>> v[100001];
     int32_t main()
        {
            IOS
       
        int n,m;
        cin>>n>>m;
        
        for(int i=0;i<m;++i)
        {
            int a,b,c;
            cin>>a>>b>>c;
            v[a].push_back({c,b});
        }
        for(int i=1;i<=n;++i)
        {
            sort(v[i].begin(),v[i].end());
        }
        int dis[n+1];
        memset(dis,-1,sizeof(dis));
        pair<int,int> upd[n+1];
        memset(upd,-1,sizeof(upd));
        dis[1]=0;
        for(int i=1;i<=n;++i)
        {
            if(dis[i]==-1)
                continue;

            for(int j=0;j<v[i].size();++j)
            {
                int k=v[i][j].second;
                int len=k-i+1;
                int cos=v[i][j].first;
                if(dis[k]==-1)
                {
                    while(dis[k]==-1)
                    {
                        dis[k]=dis[i]+cos;
                        upd[k].first=i;
                        upd[k].second=len;
                        k--;
                    }
                    if(k==i)
                        continue;
                    else
                    {
                        if(upd[k].first==i)
                            continue;
                        else
                        {
                            while(k!=i)
                            {
                                if(k<=i)
                                    break;
                                if(dis[k]>dis[i]+cos)
                                {
                                    dis[k]=dis[i]+cos;
                                    upd[k].first=i;
                                    upd[k].second=len;
                                    k--;
                                    continue;
                                }
                                else
                                {
                                    k=k-upd[k].second+1;
                                }
                                
                            }
                        }
                    }
                }
                else
                {
                    if(upd[k].first==i)
                        continue;
                            while(k!=i)
                            {
                                if(k<=i)
                                    break;
                                if(dis[k]>dis[i]+cos)
                                {
                                    dis[k]=dis[i]+cos;
                                    upd[k].first=i;
                                    upd[k].second=len;
                                    k--;
                                    continue;
                                }
                                else
                                {
                                    k=k-upd[k].second+1;
                                }
                                
                            }
                }
            }
        }
        cout<<dis[n];



        }

Measured memory_bytes: 7936