#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
const long long LINF = 1e18;
#define dump(x)  cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define FOREACH(x,a) for(auto& (x) : (a) )
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template<typename T>
void print(const vector<T> &x) {
    int n = x.size();
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x) {
    int n = x.size();
    rep(i,n) {
        rep(j,x[i].size()) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

template<typename T>
void print(const vector<T> &x, int n) {
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x, int n, int m) {
    rep(i,n) {
        rep(j,m) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void input_init() {
    cin.tie(0); ios::sync_with_stdio(false);
}

struct LazySegmentTree {
  private:
    int n;
    vector<ll> node, lazy;
    vector<bool> lazyFlag;

  public:
    LazySegmentTree(vector<ll> v) {
        int sz = (int)v.size();
        n = 1;
        while(n < sz) n *= 2;
        node.resize(2 * n - 1);
        lazy.resize(2 * n - 1, LINF);
        lazyFlag.resize(2 * n - 1, false);

        for(int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        for(int i = n - 2; i >= 0; i--) node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
    }

    void lazyEvaluate(int k, int l, int r) {
        if(lazyFlag[k]) {
            node[k] = lazy[k];
            if(r - l > 1) {
                lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
                lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
            }
            lazyFlag[k] = false;
        }
    }

    void update(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
        if(r < 0) r = n;
        lazyEvaluate(k, l, r);
        if(b <= l || r <= a)
            return;
        if(a <= l && r <= b) {
            lazy[k] = x;
            lazyFlag[k] = true;
            lazyEvaluate(k, l, r);
        } else {
            update(a, b, x, 2 * k + 1, l, (l + r) / 2);
            update(a, b, x, 2 * k + 2, (l + r) / 2, r);
            node[k] = min(node[2 * k + 1], node[2 * k + 2]);
        }
    }

    ll find(int a, int b, int k = 0, int l = 0, int r = -1) {
        if(r < 0) r = n;
        lazyEvaluate(k, l, r);
        if(b <= l || r <= a) return LINF;
        if(a <= l && r <= b) return node[k];
        ll vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
        ll vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
        return min(vl, vr);
    }
};

typedef struct Edge {
    ll l,r,c;
} Edge;

int main(int argc, char const *argv[]) {
    int n,m; cin>>n>>m;
    vector<ll> dp(n+1,LINF);
    vector<Edge> a(m);
    rep(i,m) {
        cin>>a[i].l>>a[i].r>>a[i].c;
    }
    sort(a.begin(), a.end(), [](const Edge &a, const Edge &b) {
        return a.r < b.r;
    });

    LazySegmentTree seg(vector<ll>(n+1,LINF));
    seg.update(1,2,0);
    rep(i,m) {
        ll l,r,c;
        l = a[i].l; r = a[i].r; c = a[i].c;
        ll x = seg.find(l,r+1);
        chmin(dp[r],x+c);
        seg.update(r,r+1,min(dp[r],x+c));
    }
    if (dp[n]!=LINF) cout << dp[n] << endl;
    else cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 8192