#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e9 + 10;
const long long linf = (long long)1e18 + 10;
const long long mod = (long long)(1e9 + 7);
const long long dx[] = {0, 1, 0, -1};
const long long dy[] = {1, 0, -1, 0};
const long long ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double eps = 1e-10;
long long mop(long long a, long long b = mod - 2, long long m = mod) {
  long long r = 1;
  a %= m;
  for (; b; b >>= 1) {
    if (b & 1) r = r * a % m;
    a = a * a % m;
  }
  return r;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
bool ool(long long x, long long y, long long h, long long w) {
  return ((x < 0) || (h <= x) || (y < 0) || (w <= y));
}
bool deq(double a, double b) { return abs(a - b) < eps; }
struct oreno_initializer {
  oreno_initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} oreno_initializer;
long long n, m, l, r, c, dp[100100], res = linf;
vector<pair<pair<long long, long long>, long long> > p;
signed main() {
  cin >> n >> m;
  for (long long(i) = (0); (i) < (n); ++(i)) dp[i] = linf;
  for (long long(i) = (0); (i) < (m); ++(i)) {
    cin >> l >> r >> c;
    l--, r--;
    p.push_back({{l, r}, c});
  }
  sort((p).begin(), (p).end());
  for (long long(i) = (0); (i) < (m); ++(i)) {
    if (p[i].first.first == 0)
      dp[i] = min(dp[i], p[i].second);
    else
      break;
  }
  for (long long(i) = (0); (i) < (m); ++(i))
    if (dp[i] != linf) {
      long long pl = p[i].first.first, id = p[i].first.second;
      pair<pair<long long, long long>, long long> tmp = {{pl + 1, -1ll}, -1ll};
      long long fr = lower_bound(p.begin() + i, p.end(), tmp) - p.begin();
      for (long long(j) = (fr); (j) < (m); ++(j)) {
        l = p[j].first.first, r = p[j].first.second, c = p[j].second;
        if (l <= id && id < r)
          dp[j] = min(dp[j], dp[i] + c);
        else if (r <= id)
          break;
      }
    }
  for (long long(i) = (0); (i) < (m); ++(i))
    if (p[i].first.second == n - 1) res = min(res, dp[i]);
  cout << (res == linf ? -1 : res) << "\n";
}
