#include<bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

const int N = 11111;
const int M= 2e5+5;

using namespace std;

typedef long long ll;

int head[M],tot;
ll d[M];//到个点的距离；
bool vis[M];//1表示访问过，0表示没有访问过； 

struct Edge
{
   int next;
   int to;
   ll dis;
}edge[M*2];


void dij(int u)
{
   memset(vis,0,sizeof(vis));
   d[u] = 0;
   priority_queue<node> Q;
   
   Q.push({u,0});
   
  node tmp;
  
  while(Q.size()){
     tmp = Q.top();
     Q.pop();
     int x = tmp.id;
     if(vis[x]) 
	   continue;
	   
    for(int i = head[x];~i;i = edge[i].next){
       int y = edge[i].to;
        ll dis = edge[i].dis;
         if(d[y] > d[x] +dis){
           d[y] = d[x] + dis;
               Q.push({y,d[y]});
             }
       }
   }
}
void init(int n)
{
         memset(head,-1,sizeof(head));
         tot = 0;
         for(int i = 1;i <= n;++i)
		    d[i] = INF;
}
int main()
{
    int n,m;
    cin>>n>>m;
    init(n);
    int u,v;ll dis;
    for(int i = 1;i <= n;++i){
          add(i,i - 1,0);
          }
    for(int i = 1;i <= m;++i){
          cin>>u >> v>>dis;
          add(u,v,dis);
       }
         dij(1);
         if(d[n] == INF) cout<<-1<<endl;
         else cout<<d[n]<<endl;
}