#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
struct node {
  long long l, r, w;
} s[N];
int n, m;
bool cmp(node a, node b) {
  if (a.l == b.l) {
    return a.r < b.r;
  }
  return a.l < b.l;
}
long long dis[N];
queue<pair<long long, long long>> G[N];
set<long long> ones;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) dis[i] = (1e18);
  dis[1] = 0;
  for (int i = 1; i <= m; i++) {
    cin >> s[i].l >> s[i].r >> s[i].w;
    G[s[i].l].push({s[i].r, s[i].w});
    if (s[i].l == 1) {
      ones.insert(s[i].r);
      dis[s[i].r] = min(dis[s[i].r], s[i].w);
    }
  }
  sort(s + 1, s + m + 1, cmp);
  for (int i = 1; i <= m; i++) {
    auto it = ones.lower_bound(s[i].l);
    if (it == ones.end()) continue;
    dis[s[i].l] = min(dis[s[i].l], dis[*it]);
    ones.insert(s[i].l);
    int cnt = (int)G[s[i].l].size();
    for (int j = 1; j <= cnt; j++) {
      auto v = G[s[i].l].front();
      G[s[i].l].pop();
      it = ones.lower_bound(v.first);
      if (it != ones.end()) {
        dis[v.first] = min(dis[v.first], dis[*it]);
        ones.insert(v.first);
      }
      if (dis[v.first] > dis[s[i].l] + v.second) {
        dis[v.first] = dis[s[i].l] + v.second;
      }
      G[s[i].l].push({v.first, dis[v.first]});
      ones.insert(v.first);
    }
  }
  if (dis[n] == (1e18)) {
    cout << -1 << endl;
    return 0;
  }
  cout << dis[n] << endl;
  return 0;
}
