#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
int n, m;
pair<pair<long long, long long>, long long> ed[100005];
vector<pair<long long, long long> > tree[3 * 100005];
void upd(int node, int a, int b, int l, int r, pair<long long, long long> val) {
  if (a > r || b < l || a > b) return;
  if (l <= a && b <= r) {
    tree[node].push_back(val);
    return;
  }
  upd(node * 2, a, (a + b) / 2, l, r, val);
  upd(node * 2 + 1, 1 + (a + b) / 2, b, l, r, val);
}
long long dist[100005];
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    pq;
void que(int node, int a, int b, int i, pair<long long, long long> top) {
  if (a > i || b < i || a > b) return;
  for (auto u : tree[node]) {
    pq.push({top.first + u.second, u.first});
  }
  if (a == b) return;
  que(node * 2, a, (a + b) / 2, i, top);
  que(node * 2 + 1, 1 + (a + b) / 2, b, i, top);
}
long long dijk() {
  memset(dist, -1, sizeof dist);
  pq.push({0, 1});
  while (!pq.empty()) {
    pair<int, int> top = pq.top();
    pq.pop();
    int cn = top.second;
    if (dist[cn] != -1) continue;
    dist[cn] = top.first;
    if (cn == n) return dist[cn];
    que(1, 1, n, cn, top);
  }
  return -1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < (signed)(m); i++) {
    cin >> ed[i].first.first >> ed[i].first.second >> ed[i].second;
    upd(1, 1, n, ed[i].first.first, ed[i].first.second,
        {ed[i].first.second, ed[i].second});
  }
  cout << dijk() << "\n";
  return 0;
}
