#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return x * flag;
}
const long long INF = 1e15;
const int N = 1e5 + 5;
long long s[N << 2];
inline void pushup(int p) { s[p] = min(s[p << 1], s[p << 1 | 1]); }
inline void build(int p, int l, int r) {
  if (l == r) {
    s[p] = INF;
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  pushup(p);
}
inline long long query(int p, int l, int r, int x, int y) {
  if (x <= l && r <= y) return s[p];
  int mid = (l + r) >> 1;
  long long ans = INF;
  if (x <= mid) ans = min(ans, query(p << 1, l, mid, x, y));
  if (y > mid) ans = min(ans, query(p << 1 | 1, mid + 1, r, x, y));
  return ans;
}
inline void update(int p, int l, int r, int x, int v) {
  if (l == r) {
    if (v < s[p]) s[p] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(p << 1, l, mid, x, v);
  else
    update(p << 1 | 1, mid + 1, r, x, v);
  pushup(p);
}
int n, m;
struct node {
  int l, w;
};
vector<node> G[N];
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read(), z = read();
    G[y].push_back((node){x, z});
  }
  build(1, 1, n);
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < G[i].size(); ++j) {
      int l = G[i][j].l, w = G[i][j].w;
      if (l == 1)
        update(1, 1, n, i, w);
      else {
        long long res = query(1, 1, n, l, i);
        if (res != INF) update(1, 1, n, i, w + res);
      }
    }
  }
  long long ans = query(1, 1, n, n, n);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
