// Original C++ solution (slower):
#include <iostream>

#include <queue>

#include <cstdio>

#include <vector>

typedef long long      ll;

using namespace std;

const int maxn = 1e5 + 10;

typedef pair<ll, ll>   pil;

typedef vector<pil>    vil;

#define pb    push_back

#define mp    make_pair

ll       n, m, dis[maxn], vis[maxn];

const ll inf = 0x3f3f3f3f3f3f3f3f;

struct node

{

    ll d, id;

};

vector<pil> V[maxn];

void init()

{

    for (ll i = 2; i <= n; i++)

        V[i].pb(mp(i - 1, 0));

}

priority_queue<pil, vil, greater<pil> > q;

void solve()

{

    int s = 1;

    for (int i = 1; i <= n; i++)

        dis[i] = inf;

    dis[s] = 0;

    q.push(mp(dis[s], s));

    while (!q.empty())

    {

        pil p   = q.top(); q.pop();

        int mid = p.second;

        if (vis[mid])

            continue;

        vis[mid] = 1;

        for (int i = 0; i < V[mid].size(); i++)

        {

            int y = V[mid][i].first, d = V[mid][i].second;

            if (!vis[y] && dis[mid] + d < dis[y])

            {

                dis[y] = dis[mid] + d;

                q.push(mp(dis[y], y));

            }

        }

    }

    // for (int i = 1; i <= n; i++)

    //     cout << dis[i] << endl;

    if (dis[n] != inf)

        cout << dis[n] << endl;

    else

        cout << "-1\n";

}



int main()

{

    ios::sync_with_stdio(0);

    cin >> n >> m;

    init();

    for (ll i = 0, u, v, w; i < m; i++)

    {

        cin >> u >> v >> w;

        V[u].pb(mp(v, w));

    }

    // for (int i = 1; i <= n; i++)

    // {

    //     for (int j = 0; j < V[i].size(); j++)

    //     {

    //         cout << i << endl;

    //         cout << V[i][j].first << " " << V[i][j].second << endl;

    //     }

    // }

    solve();

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>



#define SORT(v) sort(v.begin(),v.end())

#define si(n) scanf("%d",&n)

#define sii(n,m) scanf("%d %d",&n,&m)

#define sl(n) scanf("%lld",&n)

#define sll(n,m) scanf("%lld %lld",&n,&m)

#define ss(cad) scanf("%s",cad)

#define PB push_back

#define fst first

#define scn second

#define DBG(x) cerr << #x << " = " << (x) << endl; 

#define M 1000000007

#define N_MAX 100010



using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef vector<bool> vb;

typedef vector<ll> vl;

typedef pair<int,int> pi;

typedef vector<pi> vp;



int main()

{

	int n, m;

	sii(n, m);

	vector<pair<pi, int> > vec;

	vl in(m);

	vl dist(n, -1);

	vl curr(m, -1);



	vp puntos;

	for(int i = 0; i < m; i++)

	{

		int val;

		si(val);

		val--;

		puntos.push_back({val, i});

		si(val);

		val--;

		puntos.push_back({val, i});

		sl(in[i]);

	}



	SORT(puntos);

	multiset<ll> lista;

	dist[0] = 0;

	int pos = 0;

	while(pos < puntos.size() && puntos[pos].fst == 0)

	{

		lista.insert(in[puntos[pos].scn]);

		curr[puntos[pos].scn] = in[puntos[pos].scn];

		pos++;

	}



	bool sw = 1;

	for(int i = 1; i < n; i++)

	{

		if(!lista.empty())

			dist[i] = *lista.begin();

		else

		{

			sw = 0;

			break;

		}



		while(pos < puntos.size() && puntos[pos].fst == i)

		{

			if(curr[puntos[pos].scn] != -1)

			{

				lista.erase(lista.find(curr[puntos[pos].scn]));

				curr[puntos[pos].scn] = -1;

			}

			else

			{

				lista.insert(dist[i] + in[puntos[pos].scn]);

				curr[puntos[pos].scn] = dist[i] + in[puntos[pos].scn];

			}

			pos++;

		}

	}



	if(sw)

		printf("%lld\n", dist[n-1]);

	else

		printf("-1\n");

    

    return 0;

}


// Fastest known solution for this problem: