#include <bits/stdc++.h>
using namespace std;
struct SegTree {
  vector<long long> nod;
  int size;
  SegTree(int x) {
    size = 1;
    while (x > size) {
      size *= 2;
    }
    nod = vector<long long>(size * 2 - 1, LLONG_MAX / 2);
  }
  long long update(int k) {
    k += size - 1;
    long long ret = nod[k];
    while (k > 0) {
      k = (k - 1) / 2;
      ret = min(nod[k], ret);
    }
    return ret;
  }
  void Update(int k, long long a) {
    k += size - 1;
    nod[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      nod[k] = a;
    }
  }
  void query(int a, int b, int k, int l, int r, long long x) {
    if (r <= a || b <= l) {
      return;
    }
    if (a <= l && r <= b) {
      nod[k] = min(nod[k], x);
    } else {
      query(a, b, k * 2 + 1, l, (l + r) / 2, x);
      query(a, b, k * 2 + 2, (l + r) / 2, r, x);
    }
  }
  void deb() {
    for (int i = 0; i < size * 2 - 1; i++) {
      if (i == size * 2 - 2) {
        cout << nod[i] << endl;
      } else {
        cout << nod[i] << ' ';
      }
    }
    return;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<pair<int, int>, int>> da(m);
  for (int i = (0); i < (int)(m); i++) {
    cin >> da[i].first.first >> da[i].first.second >> da[i].second;
    da[i].first.first--;
  }
  sort((da).begin(), (da).end());
  SegTree sg(n);
  sg.query(0, 1, 0, 0, sg.size, (long long)0);
  for (int i = (0); i < (int)(m); i++) {
    long long now = sg.update(da[i].first.first);
    if (now == LLONG_MAX / 2) {
      cout << -1 << endl;
      return 0;
    }
    sg.query(da[i].first.first, da[i].first.second, 0, 0, sg.size,
             now + da[i].second);
  }
  cout << sg.update(n - 1) << endl;
}
