#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);++(i))
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);++(i))
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);--(i))
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);--(i))
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
#define out(y,x,h,w) (y) < 0 || (h) <= (y) || (x) < 0 || (w) <= (x)
#define line cout << "------------------------\n" 
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
const long double pi = acos(-1);
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; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& input, vector<T>& v) { for (auto& elemnt : v)input >> elemnt; return input; }

struct p {
    int l, r; ll c;
    bool operator< (p x) {
        if (l == x.l)return r < x.r;
        return l < x.l;
    }
};
int main() {
    init();

    int n, m; cin >> n >> m;
    vector<p> edge(m);
    FOR(i, 0, m) {
        cin >> edge[i].l >> edge[i].r >> edge[i].c;
        --edge[i].l, --edge[i].r;
    }
    SORT(edge);

    VL dis(n, LLINF);
    dis[0] = 0;
    set<pair<int, ll>> set;
    int itr = 0;
    FOR(now, 0, n) {
        while (!set.empty() && (*set.begin()).first < now) {
            set.erase(set.begin());
        }
        if (!set.empty() && now <= (*set.begin()).first) {
            chmin(dis[now], (*set.begin()).second);
        }
        while (itr < m && edge[itr].l <= now) {
            ll cost = dis[now] + edge[itr].c;
            if (set.empty()||(edge[itr].r > (*set.begin()).first || (*set.begin()).second > cost)) {
                set.insert(make_pair(edge[itr].r, cost));
            }
            ++itr;
        }
    }

    cout << (dis[n - 1] == LLINF ? -1 : dis[n - 1]) << "\n";
}
