#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
using namespace std;

#define LL long long

const int CN = 1e5+5;
const LL INF = 0x3f3f3f3f3f3f3f2f;

LL read(){
    LL s = 0,ne = 1; char c = getchar();
    for(;c<'0'||c>'9';c=getchar()) if(c=='-') ne=-1;
    for(;c>='0'&&c<='9';c=getchar()) s=(s<<1)+(s<<3)+c-'0';
    return s*ne;
}

class fs{
  public: int to,nxt; LL di; void init(int t,int n,LL d) {to=t;nxt=n;di=d;}
}E[CN * 51];
int hd[CN],ecnt = 0;
void add(int x,int y,LL z) {E[++ecnt].init(y, hd[x], z); hd[x] = ecnt;}

int n,m;

/* DJ */
class DJ{
  public: int id; LL v; bool operator < (const DJ& a)const {return v >s a.v;}
};
priority_queue<DJ> Q; LL d[CN]; bool vis[CN];
LL SP(int st,int ed){
    memset(vis, 0, sizeof(vis));
    memset(d, 0x3f, sizeof(d));
    Q.push((DJ){st, d[st] = 0});
    while(!Q.empty()){
        int u = Q.top().id; Q.pop();
        for(int k = hd[u]; k; k = E[k].nxt){
            int v = E[k].to;
            if(d[v] > d[u] + E[k].di){
                d[v] = d[u] + E[k].di;
                if(!vis[v]) Q.push((DJ){v, d[v]}),vis[v] = true;
            }
        }
    }
    return d[ed] < INF ? d[ed] : -1;
}

int main()
{
    n = read(); m = read();
    while(m--){
        int u = read(),v = read(); LL c = read();
        add(u, v, c); add(v, u, c);
    }
    for(int i=1;i<n;i++) add(i + 1, i, 0);

    printf("%lld", SP(1, n));
}