#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e+15;
int main() {
  long long n;
  int m;
  cin >> n >> m;
  vector<pair<long long, pair<long long, long long>>> lrc(m);
  for (int i = 0; i < m; ++i) {
    cin >> lrc[i].first >> lrc[i].second.first >> lrc[i].second.second;
  }
  sort(lrc.begin(), lrc.end());
  map<long long, long long> mp1, mp2;
  mp1[1] = 0;
  mp2[0] = 1;
  mp1[2] = inf;
  mp2[inf] = 2;
  mp1[n] = inf + 10;
  for (int i = 0; i < m; ++i) {
    long long l, r, c;
    l = lrc[i].first;
    r = lrc[i].second.first;
    c = lrc[i].second.second;
    auto it1 = mp1.upper_bound(l);
    it1--;
    long long x = it1->second + c;
    auto it2 = mp2.upper_bound(x);
    long long y = it2->second;
    if (r < y) continue;
    long long a = mp1[y];
    mp1[r + 1] = mp1[y];
    mp2.erase(r + 1);
    mp2[mp1[y]] = r + 1;
    mp1[y] = x;
    mp2.erase(y);
    mp2[x] = y;
  }
  if (mp1[n] == inf + 10)
    cout << -1 << "\n";
  else
    cout << mp1[n] << "\n";
  return 0;
}
