#ifndef COMPETITIVE_GRAPH_H
#define COMPETITIVE_GRAPH_H

#include <map>
#include <set>
#include <utility>
#include <queue>

template<typename T>
class djikstra {
private:
    std::map<T, std::set<std::pair<T, T>>> path; //src dst cost
    std::map<T, T> dist; //dst cost
public:
    void compute(T start);

    void add_path(T src, T dst, T cost);

    T get_dist(T dst);
};

#endif //COMPETITIVE_GRAPH_H
#ifndef COMPETITIVE_NIKLIB_H
#define COMPETITIVE_NIKLIB_H

#ifdef BUILT_LOCAL

#include "graph.h"

#endif

#endif //COMPETITIVE_NIKLIB_H
#ifdef BUILT_LOCAL

#include "niklib.h"

#endif

template<typename T>
void djikstra<T>::compute(T start) {
    dist.clear();
    dist[start] = 0;
    std::priority_queue<std::pair<T, T>, std::deque<std::pair<T, T>>, std::greater<>> q;
    for (auto &e:path[start]) q.push(std::make_pair(e.second, e.first));
    while (!q.empty()) {
        auto c = q.top();
        q.pop();
        if (dist.find(c.second) != dist.end()) continue;
        dist[c.second] = c.first;
        for (auto &e:path[c.second]) {
            if (dist.find(e.first) != dist.end()) continue;
            q.push(std::make_pair(c.first + e.second, e.first));
        }
    }
}

template<typename T>
void djikstra<T>::add_path(T src, T dst, T cost) {
    path[src].insert(std::make_pair(dst, cost));
}

template<typename T>
T djikstra<T>::get_dist(T dst) {
    return dist.find(dst) == dist.end() ? -1 : dist[dst];
}

template
class djikstra<long long>;#ifdef BUILT_LOCAL

#include "niklib.h"

#endif

#include <iostream>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <cstdio>
#include <map>
#include <set>
#include <list>

using namespace std;

typedef long long ll;
constexpr ll mod = 1000000007;

int main() {
    int n, m, l, r, c;
    djikstra<ll> d;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> l >> r >> c;
        d.add_path(l, r, c);
    }
    for (int i = 1; i < n; i++) {
        d.add_path(i + 1, i, 0);
    }
    d.compute(1);
    cout << d.get_dist(n) << endl;
}