#include <bits/stdc++.h>
using namespace std;
int main() {
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      q;
  int n, m, l, r;
  vector<pair<pair<int, int>, long long> > e;
  long long c, dis[100001];
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lld", &l, &r, &c);
    e.push_back({{l, r}, c});
  }
  sort(e.begin(), e.end());
  for (int i = 0; i <= n; i++) {
    dis[i] = 1000000000000000;
  }
  dis[1] = 0;
  int p = 0;
  l = 0;
  q.push({0LL, 1});
  while (!q.empty() && l < n && p < m) {
    while (!q.empty() && q.top().second <= l) {
      q.pop();
    }
    l = q.top().second;
    q.pop();
    while (e[p].first.first <= l && p < m) {
      if (dis[e[p].first.second] > dis[l] + e[p].second) {
        dis[e[p].first.second] = dis[l] + e[p].second;
        q.push({dis[e[p].first.second], e[p].first.second});
      }
      p++;
    }
  }
  if (dis[n] == 1000000000000000) {
    printf("-1\n");
  } else {
    printf("%lld\n", dis[n]);
  }
}
