#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N=100000;

int n,m;
struct seg{
  int l,r,v;
}a[N+9];
vector<int>vl[N+9],vr[N+9];

void into(){
  scanf("%d%d",&n,&m);
  for (int i=1;i<=m;++i){
	scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].v);
	vl[a[i].l].push_back(i);
	vr[a[i].r].push_back(i);
  }
}

multiset<LL>s;
LL dp[N+9];

void Get_dp(){
  for (int i=1;i<=n;++i){
	if (i>1&&s.empty()) {dp[n]=-1;return;}
	dp[i]=i==1?0:*s.begin();
	for (int vs=vr[i].size(),j=0;j<vs;++j) s.erase(s.find(dp[a[vr[i][j]].l]+a[vr[i][j]].v));
	for (int vs=vl[i].size(),j=0;j<vs;++j) s.insert(dp[a[vl[i][j]].l]+a[vl[i][j]].v);
  }
}

void work(){
  Get_dp();
}

void outo(){
  printf("%lld\n",dp[n]);
}

int main(){
  into();
  work();
  outo();
  return 0;
}