// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

ll tr[4100000];

struct rp {

    int x, y;

    ll z;

} a[110000];

vector<int> v[110000];

int n, m, all;



void getnew(int root) {

    root /= 2;

    while (root) {

        tr[root] = min(tr[root * 2], tr[root * 2 + 1]);

        root /= 2;

    }

}



ll getmin(int l, int r) {

    ll ans = 1e15;

    while (r - l > 1) {

        if (l % 2 == 0)

            ans = min(ans, tr[l + 1]);

        if (r % 2 == 1)

            ans = min(ans, tr[r - 1]);

        l >>= 1, r >>= 1;

    }

    return ans;

}



int main() {

    scanf("%d%d", &n, &m);

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

        scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z);

    for (int i = 1; i <= m; i++) {

        v[a[i].y].push_back(i);

    }

    all = 1;

    while (all <= n)

        all <<= 1;

    all--;

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

        tr[i] = 1e15;

    for (int i = 1; i <= m; i++) {

        if (a[i].x == 1) {

            tr[all + a[i].y] = a[i].z;

            getnew(all + a[i].y);

        }

    }

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

        for (auto j:v[i]) {

            ll now = getmin(all + a[j].x - 1, all +a[j].y + 1) + a[j].z;

            if (now < tr[all + a[j].y]) {

                tr[all + a[j].y] = now;

                getnew(all + a[j].y);

            }

        }

    }

  //  printf("%lld..\n", tr[all + n]);

    if (tr[all + n] < 1e15) {

        printf("%lld\n", tr[all + n]);

    } else puts("-1");

    return 0;

}


// 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: