			#include <bits/stdc++.h>
			using namespace std;
			#define ll long long
			#define INF 100000000LL
			#define mod 1000000007
			#define pll pair<long,long>
			#define se second
			#define fi first
			#define pb push_back
			#include <ext/pb_ds/assoc_container.hpp>
			#include <ext/pb_ds/tree_policy.hpp>
			using namespace __gnu_pbds;

			typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
			typedef tree<pll,null_type,less<pll>,rb_tree_tag,tree_order_statistics_node_update>pllordered_set;
			
			vector<pll> g[100050];
			set<pll> s;
			vector<ll> dist(100050);
			int main()
			{
				ll n,m;
				cin>>n>>m;
				for(ll i=2;i<=n;i++)
				{
					g[i].pb({0,i-1});
				}
				for(ll i=0;i<m;i++)
				{
					ll u,v,w;
					cin>>u>>v>>w;
					g[u].pb({w,v});
				}
				for(ll i=2;i<=n;i++)
				{
					dist[i]=INF;
					s.insert({dist[i],i});
				}
				dist[1]=0;
				
				s.insert({dist[1],1});
				
				while(!s.empty())
				{
					pll node=*s.begin();
					s.erase(s.begin());
					for(auto j:g[node.se])
					{
						if(dist[node.se]+j.fi<dist[j.se])
						{
							s.erase({dist[j.se],j.se});
							dist[j.se]=dist[node.se]+j.fi;
							s.insert({dist[j.se],j.se});
							
						}
					
					}
				}
				if(dist[n]==INF)
					cout<<-1;
				else
					cout<<dist[n];
				
			}
						
			
