#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
template <class T>
void pv(T a, T b) {
  for (T i = a; i != b; ++i) cout << *i << " ";
  cout << endl;
}
template <class T>
void pvp(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << "(" << i->first << ", " << i->second << ") ";
  cout << endl;
}
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;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
using ll = long long;
struct edge {
  ll l, r, c;
};
bool compare_diff(edge lhs, edge rhs) {
  if (lhs.l == rhs.l) {
    return lhs.r < rhs.r;
  } else {
    return lhs.l < rhs.l;
  }
}
signed main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<edge> E;
  ll n, m;
  cin >> n >> m;
  for (ll i = (ll)0; i < (ll)m; ++i) {
    ll l, r, c;
    cin >> l >> r >> c;
    edge e;
    e.l = l;
    e.r = r;
    e.c = c;
    E.push_back(e);
  }
  sort((E).begin(), (E).end(), compare_diff);
  ll kyori[100010];
  memset((kyori), 1, sizeof(kyori));
  ll defkyori = kyori[0];
  kyori[1] = 0;
  for (ll i = (ll)0; i < (ll)m; ++i) {
    if (kyori[E[i].l] == defkyori) {
      cout << -1 << endl;
      return 0;
    }
    for (ll j = (ll)E[i].l + 1; j < (ll)E[i].r + 1; ++j) {
      chmin(kyori[j], kyori[E[i].l] + E[i].c);
    }
  }
  cout << kyori[n] << endl;
  return 0;
};
