// Slower C++ solution:
#include<iostream>

#include<cstring>

#include<queue>

#include<map>

#include<set>

#include<stack>

#include<cmath>

#include<cstdio>

#include<iomanip>

#include<sstream>

#include<algorithm>

 

using namespace std;

#define read(x) scanf("%d",&x)

#define Read(x,y) scanf("%d%d",&x,&y)

#define sRead(x,y,z)  scanf("%d%d%d",&x,&y,&z)

#define gc(x)  scanf(" %c",&x);

#define  INF (ll)1e14//无穷设置的大着点

#define mmt(x,y)  memset(x,y,sizeof x)

#define write(x) printf("%d\n",x)

const int N = 11000;

const int M=  2e5+5;

using namespace std;

typedef long long ll;

struct Edge

{

    int next;

    int to;

    ll dis;

}edge[M*2];

int head[M],tot;

ll d[M];bool vis[M];

inline void add(int from,int to,ll dis){

    edge[++tot].next = head[from];

    edge[tot].to = to;

    edge[tot].dis = dis;

    head[from] = tot;

}

struct node

{

    ll val;int id;

    node(){}

    node(int a,ll b):id(a),val(b){}

    bool operator <(node A)const{

        return val > A.val;

    }

};

void dij(int u)

{

    mmt(vis,0);

    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]});

            }

        }

    }

}

inline void init(int n)

{

    mmt(head,-1);

    tot = 0;

    for(int i = 1;i <= n;++i) d[i] = INF;

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    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;

}

// Faster C++ solution for the same problem (measured speedup: 2.0001294331405957x):
#include<iostream>

#include <bits/stdc++.h>

//1000000000000223

#define ll long long

#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

int n, m, l, r, c;

ll t[400009], inf=2e14;

vector<pair<int, pair<int,int> > > v;

void update(int v, int s, int e, int pos,ll val)

{

    if(s==e)

    {

        t[v]=min(t[v],val);

        return ;

    }

    int m=(s+e)/2;

    pos<=m ? update(2*v,s,m,pos,val) : update(2*v+1,m+1,e,pos,val);

    t[v]=min(t[2*v],t[2*v+1]);

}

ll solve(int v, int s, int e, int l, int r)

{

    if(s>r||e<l)

        return inf;

    if(s>=l &&e<=r)

        return t[v];

    int m=(s+e)/2;

    return min(solve(2*v,s,m,l,r),solve(2*v+1,m+1,e,l,r));

}

int main()

{

    IO

    cin>>n>>m;

    while(m--)

    {

        cin>>l>>r>>c;

        v.push_back({l,{r,c}});

    }

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

        t[i]=inf;

    sort(v.begin(),v.end());

    if(v[0].first!=1)

        return cout<<-1, 0 ;

    for(auto i : v)

    {

        if(i.first==1)

        {

            update(1,1,n,i.second.first,i.second.second);

            continue;

        }

        ll x=solve(1,1,n,i.first,n);

        update(1,1,n,i.second.first,i.second.second+x);

    }

    ll x=solve(1,1,n,n,n);

    if(x>=inf)

        x=-1;

    cout<<x;



    return 0;

}