#include <bits/stdc++.h>
using namespace std;
int t[800040], a[800040];
void push_down(int rt) {
  t[rt] = min(t[rt], a[rt]);
  a[rt * 2 + 1] = min(a[rt * 2 + 1], a[rt]);
  a[rt * 2 + 2] = min(a[rt * 2 + 2], a[rt]);
  t[rt * 2 + 1] = min(t[rt * 2 + 1], a[rt * 2 + 1]);
  t[rt * 2 + 2] = min(t[rt * 2 + 2], a[rt * 2 + 2]);
  t[rt] = min(t[rt * 2 + 1], t[rt * 2 + 2]);
  a[rt] = 0x3f3f3f3f;
}
void update(int rt, int l, int r, int nl, int nr, int x) {
  if (l <= nl && nr <= r)
    a[rt] = min(a[rt], x);
  else if (nr < l || nl > r)
    return;
  else {
    push_down(rt);
    int mid = (nl + nr) / 2;
    update(rt * 2 + 1, l, r, nl, mid, x);
    update(rt * 2 + 2, l, r, mid + 1, nr, x);
    push_down(rt);
  }
}
int query(int rt, int l, int r, int nl, int nr) {
  push_down(rt);
  if (l <= nl && nr <= r)
    return t[rt];
  else if (nr < l || nl > r)
    return 0x3f3f3f3f;
  else {
    int mid = (nl + nr) / 2;
    return min(query(rt * 2 + 1, l, r, nl, mid),
               query(rt * 2 + 2, l, r, mid + 1, nr));
  }
}
int n, m;
int l[200010], r[200010], c[200010], tot = 0;
int main() {
  memset(t, 0x3f, sizeof(t));
  memset(a, 0x3f, sizeof(a));
  cin >> n >> m;
  update(0, 1, 1, 0, n, 0);
  for (int i = 0; i < m; i++) {
    int _l, _r, _c;
    cin >> _l >> _r >> _c;
    if (_l == 1) {
      update(0, _l, _r, 0, n, _c);
    } else {
      l[tot] = _l, r[tot] = _r, c[tot++] = _c;
    }
  }
  for (int i = 0; i < tot; i++) {
    int mn = query(0, l[i], r[i], 0, n);
    mn += c[i];
    update(0, l[i], r[i], 0, n, mn);
  }
  int ans = query(0, n, n, 0, n);
  if (ans == 0x3f3f3f3f)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
