#include <bits/stdc++.h>
const bool debug = false;
using namespace std;
const long long MOD = 1000000007;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  long long lim = 1000000000000000;
  vector<map<long long, long long>> edges(n, map<long long, long long>());
  for (int(i) = 0; (i) < (n - 1); (i)++) {
    edges[i + 1][i] = 0;
  }
  for (int(i) = 0; (i) < (m); (i)++) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    if (edges[l].find(r) == edges[l].end()) {
      edges[l][r] = c;
    } else {
      chmin(edges[l][r], c);
    }
  }
  vector<long long> d(n, lim), chk(n, 0);
  priority_queue<pair<long long, long long>> pq;
  d[0] = 0;
  pq.emplace(0, 0);
  while (!pq.empty()) {
    auto top = pq.top();
    long long now = top.second;
    long long dist = top.first;
    pq.pop();
    if (chk[now] == 0) continue;
    chk[now] = 1;
    for (auto x : edges[now]) {
      if (debug == true) {
        cout << x.second << endl;
      }
      if (chmin(d[x.first], d[now] + x.second)) {
        pq.emplace(d[x.first], x.first);
      }
    }
  }
  cout << (d[n - 1] == lim ? -1 : d[n - 1]) << endl;
  return 0;
}
