#include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
using namespace std;
const long long MAX_D = 10000000000000000LL;
int main(void) {
  int n, m;
  map<pair<int, int>, long long> graph;
  int l[100001], r[100001];
  long long c[100001];
  long long d[100001] = {};
  for (long long i = 1, i_len = (100001); i < i_len; ++i) {
    d[i] = MAX_D;
  }
  cin >> n >> m;
  for (long long i = 0, i_len = (m); i < i_len; ++i) {
    cin >> l[i] >> r[i] >> c[i];
    graph[make_pair(l[i] - 1, r[i] - 1)] = c[i];
  }
  for (long long i = 1, i_len = (n); i < i_len; ++i) {
    graph[make_pair(i, i - 1)] = -1;
  }
  bool used[100001] = {};
  while (true) {
    int v = -1;
    for (int u = 0; u < n; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) {
        v = u;
      }
    }
    if (v == -1) {
      break;
    }
    used[v] = true;
    for (int u = 0; u < n; u++) {
      int x = graph[make_pair(v, u)];
      if (x != 0) {
        d[u] = min(d[u], d[v] + (x == -1 ? 0 : x));
      }
    }
  }
  if (d[n - 1] >= MAX_D) {
    cout << -1 << endl;
  } else {
    cout << d[n - 1] << endl;
  }
  return 0;
}
