#include <bits/stdc++.h>
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define int long long
using namespace std;
using cd = complex <double>;
 
typedef pair <int, long long> pii;
 
const long long INF = 1e18;
const int mod = 998244353;//1000000007;
const double Pi = acos(-1);
 
void Fastio()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

int n, m;
int Dist[100005];
vector <pii> Adj[100005];
priority_queue <pair <int, int>, vector <pii>, greater <pii> > pq;

signed main()
{
	cin >> n >> m;
	Dist[1] = 0;
	for(int i = 2; i <= n; i++)
	{
		Dist[i] = INF;
		Adj[i].eb(mp(i - 1, 0));
	}
	for(int i = 1; i <= m; i++)
	{
		int u, v, w;
		cin >> u >> v >> w;
		Adj[u].eb(mp(v, w));
		Adj[v].eb(mp(u, w));
	}
	pq.push(mp(0, 1));
	while(pq.empty() == false)
	{
		int node = pq.top().se, d = pq.top().fi;
		pq.pop();
		if(d == Dist[node])
		{
			for(auto x : Adj[node])
			{
				if(Dist[x.fi] > x.se + Dist[node])
				{
					Dist[x.fi] = x.se + Dist[node];
					pq.push(mp(Dist[x.fi], x.fi));
				}
			}
		}
	}
	if(Dist[n] == INF)
	{
		Dist[n] = -1;
	}
	cout << Dist[n];
}