#include <bits/stdc++.h>
using namespace std;
int n, m, i, j;
struct node {
  int l, r, c;
} b[200001 * 4], a[200001];
bool cmp(node a, node b) { return a.l < b.l; }
void build(int x, int l, int r) {
  b[x].l = l;
  b[x].r = r;
  b[x].c = 1000000001;
  if (l == r) {
    if (l == 1) b[x].c = 0;
    return;
  }
  int mid = l + r >> 1;
  if (l <= mid) build(x * 2, l, mid);
  if (mid + 1 <= r) build(x * 2 + 1, mid + 1, r);
}
int query(int x, int y) {
  if (b[x].l == b[x].r) return b[x].c;
  int ans = b[x].c;
  if (y <= b[x * 2].r) {
    ans = min(ans, query(x * 2, y));
  } else {
    ans = min(ans, query(x * 2 + 1, y));
  }
  return ans;
}
void update(int x, int y, int l, int r) {
  if (l <= b[x].l && b[x].r <= r) {
    b[x].c = min(b[x].c, y);
    return;
  }
  if (l <= b[x * 2].r) update(x * 2, y, l, r);
  if (r >= b[x * 2 + 1].l) update(x * 2 + 1, y, l, r);
}
int main(void) {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].c);
  }
  sort(a + 1, a + m + 1, cmp);
  build(1, 1, n);
  for (i = 1; i <= m;) {
    j = i;
    int t = query(1, a[i].l);
    while (a[j].l == a[i].l && j <= m) {
      update(1, t + a[j].c, a[j].l + 1, a[j].r);
      j++;
    }
    i = j;
  }
  printf("%d\n", query(1, n) == 1000000001 ? -1 : query(1, n));
  return 0;
}
