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

#include <queue>

#include <cstdio>

#include <vector>

typedef long long      ll;

using namespace std;

const int maxn = 1e5 + 10;

typedef pair<ll, ll>   pil;

typedef vector<pil>    vil;

#define pb    push_back

#define mp    make_pair

ll       n, m, dis[maxn], vis[maxn];

const ll inf = 0x3f3f3f3f3f3f3f3f;

struct node

{

    ll d, id;

};

vector<pil> V[maxn];

void init()

{

    for (ll i = 2; i <= n; i++)

        V[i].pb(mp(i - 1, 0));

}

priority_queue<pil, vil, greater<pil> > q;

void solve()

{

    int s = 1;

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

        dis[i] = inf;

    dis[s] = 0;

    q.push(mp(dis[s], s));

    while (!q.empty())

    {

        pil p   = q.top(); q.pop();

        int mid = p.second;

        if (vis[mid])

            continue;

        vis[mid] = 1;

        for (int i = 0; i < V[mid].size(); i++)

        {

            int y = V[mid][i].first, d = V[mid][i].second;

            if (!vis[y] && dis[mid] + d < dis[y])

            {

                dis[y] = dis[mid] + d;

                q.push(mp(dis[y], y));

            }

        }

    }

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

    //     cout << dis[i] << endl;

    if (dis[n] != inf)

        cout << dis[n] << endl;

    else

        cout << "-1\n";

}



int main()

{

    ios::sync_with_stdio(0);

    cin >> n >> m;

    init();

    for (ll i = 0, u, v, w; i < m; i++)

    {

        cin >> u >> v >> w;

        V[u].pb(mp(v, w));

    }

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

    // {

    //     for (int j = 0; j < V[i].size(); j++)

    //     {

    //         cout << i << endl;

    //         cout << V[i][j].first << " " << V[i][j].second << endl;

    //     }

    // }

    solve();

}


// Faster C++ solution for the same problem (measured speedup: 1.7486520593821238x):
#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;

}