#include <bits/stdc++.h>
using namespace std;
const long long inf = 1000000000000000018;
const int maxn = 100005;
int n, m, l, r;
long long c;
long long d[maxn];
vector<int> topush;
set<int> pos;
vector<pair<int, long long> > edge[maxn];
bool c_cmp(pair<int, long long> &x, pair<int, long long> &y) {
  if (x.second == y.second) return x.first > y.first;
  return x.second < y.second;
}
signed main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d %d %lld", &l, &r, &c);
    edge[l].push_back(make_pair(r, c));
  }
  for (int i = 1; i <= n; ++i) d[i] = inf;
  d[1] = 0;
  pos.insert(1);
  for (l = 1; l < n; ++l) {
    sort(edge[l].begin(), edge[l].end(), c_cmp);
    for (int j = 0; j < int(edge[l].size()); ++j) {
      r = edge[l][j].first, c = edge[l][j].second;
      for (auto it = pos.lower_bound(l); it != pos.lower_bound(r);
           it = next(it))
        d[r] = min(d[r], d[*it] + c);
      if (d[r] != inf) topush.push_back(r);
    }
    for (int j : topush) pos.insert(j);
    topush.clear();
  }
  if (d[n] >= inf) d[n] = -1LL;
  printf("%lld\n", d[n]);
}
