#include <bits/stdc++.h>

using namespace std;

const long long N = 2e5 + 5, M = 998244353;
int n, m, j;
long long dist[N];
pair<pair<int, int>, int> p[N];
priority_queue<pair<long long, int> > pq;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int u, v, c;
        cin >> u >> v >> c;
        p[i] = {{u, v}, c};
    }

    for (int i = 1; i <= n; i++)
    {
        dist[i] = LONG_LONG_MAX;
    }

    sort(p, p + m);

    pq.push({0, 1});
    dist[1] = 0;

    while (!pq.empty())
    {
        int u = pq.top().second;
        long long dis = -pq.top().first;
        pq.pop();

        while (j < m && p[j].first.first <= u)
        {
            if (dist[p[j].first.second] > dis + p[j].second)
            {
                dist[p[j].first.second] = dis + p[j].second;
                pq.push({-dist[p[j].first.second], p[j].first.second});
            }
            j++;
        }
    }

    cout << (dist[n] >= 1e17 ? -1 : dist[n]) << "\n";
}