#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];
vector<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_back({s[i].r, s[i].w});
    if (s[i].l == 1) {
      ones.insert(s[i].r);
      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);
    for (auto val : G[s[i].l]) {
      long long v = val.first;
      long long ww = val.second;
      dis[v] = min(dis[v], dis[s[i].l] + ww);
      ones.insert(v);
    }
  }
  if (dis[n] == (1e18)) {
    cout << -1 << endl;
    return 0;
  }
  cout << dis[n] << endl;
  return 0;
}
