#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const long long INF = 1e16;
struct P {
  int l, r, c;
  bool operator<(const P& b) const {
    if (l != b.l) return l < b.l;
    if (r != b.r) return r < b.r;
    return c < b.c;
  }
} p[maxn];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) scanf("%d%d%d", &p[i].l, &p[i].r, &p[i].c);
  sort(p, p + m);
  if (p[0].l != 1) return puts("-1"), 0;
  int cur = 1;
  long long ans = 0;
  int i = 0;
  while (cur < n && i < m) {
    if (p[i].l > cur) return puts("-1"), 0;
    long long mn = INF;
    int r = 0;
    while (i < m && p[i].l <= cur) {
      if (p[i].c < mn)
        mn = p[i].c, r = p[i].r;
      else if (p[i].c == mn)
        r = max(r, p[i].r);
      i++;
    }
    ans += mn, cur = r;
  }
  if (cur != n) return puts("-1"), 0;
  printf("%lld\n", ans);
  return 0;
}
