#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template <class T>
constexpr T INF() {
  return ::std::numeric_limits<T>::max();
}
template <class T>
constexpr T HINF() {
  return INF<T>() / 2;
}
template <typename T_char>
T_char TL(T_char cX) {
  return tolower(cX);
};
template <typename T_char>
T_char TU(T_char cX) {
  return toupper(cX);
};
const int vy[] = {-1, -1, -1, 0, 1, 1, 1, 0},
          vx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int popcnt(unsigned long long n) {
  int cnt = 0;
  for (int i = 0; i < 64; i++)
    if ((n >> i) & 1) cnt++;
  return cnt;
}
int d_sum(LL n) {
  int ret = 0;
  while (n > 0) {
    ret += n % 10;
    n /= 10;
  }
  return ret;
}
int d_cnt(LL n) {
  int ret = 0;
  while (n > 0) {
    ret++;
    n /= 10;
  }
  return ret;
}
LL gcd(LL a, LL b) {
  if (b == 0) return a;
  return gcd(b, a % b);
};
LL lcm(LL a, LL b) {
  LL g = gcd(a, b);
  return a / g * b;
};
struct INIT {
  INIT() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    cout << fixed << setprecision(13);
  }
} INIT;
struct SegmentTree {
 private:
  int n;
  vector<int> node;

 public:
  SegmentTree(vector<int> v) {
    int sz = v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1, HINF<int>());
    for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
  }
  void update(int x, int val) {
    x += (n - 1);
    node[x] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = min(node[2 * x + 1], node[2 * x + 2]);
    }
  }
  int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return HINF<int>();
    if (a <= l && r <= b) return node[k];
    int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
int n, m;
int l[202020], r[202020], c[202020];
int idx[202020];
bool calc(int a, int b) {
  if (l[a] != l[b])
    return l[a] < l[b];
  else
    return r[a] < r[b];
}
int main() {
  cin >> n >> m;
  for (LL i = (0); i < (m); i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--, r[i]--;
  }
  for (LL i = (0); i < (n); i++) idx[i] = i;
  sort(idx, idx + m, calc);
  SegmentTree seg(vector<int>(n, HINF<int>()));
  seg.update(0, 0);
  for (LL i = (0); i < (m); i++) {
    int num = seg.getmin(r[idx[i]], r[idx[i]] + 1);
    num = min(num, seg.getmin(l[idx[i]], r[idx[i]]) + c[idx[i]]);
    seg.update(r[idx[i]], num);
  }
  int ans = seg.getmin(n - 1, n);
  cout << (ans == HINF<int>() ? -1 : ans) << endl;
}
