#include <bits/stdc++.h>
using namespace std;
long long head[300010], lc[300010 * 20], rc[300010 * 20], tot, ncnt;
long long n, m, s, rt1, rt2;
struct Edge {
  long long nxt, to, val;
} e[100010 * 20];
inline void addedge(long long f, long long t, long long val) {
  e[++tot].nxt = head[f];
  e[tot].to = t;
  e[tot].val = val;
  head[f] = tot;
}
void buildOut(long long &o, long long l, long long r) {
  if (l == r) {
    o = l;
    return;
  }
  o = ++ncnt;
  long long mid = (l + r) >> 1;
  buildOut(lc[o], l, mid);
  buildOut(rc[o], mid + 1, r);
  addedge(o, lc[o], 0);
  addedge(o, rc[o], 0);
}
void buildIn(long long &o, long long l, long long r) {
  if (l == r) {
    o = l;
    return;
  }
  o = ++ncnt;
  long long mid = (l + r) >> 1;
  buildIn(lc[o], l, mid);
  buildIn(rc[o], mid + 1, r);
  addedge(lc[o], o, 0);
  addedge(rc[o], o, 0);
}
long long L, R;
void update(long long o, long long l, long long r, long long f, long long val,
            short type) {
  if (L <= l && R >= r) {
    type == 2 ? addedge(f, o, val) : addedge(o, f, val);
    return;
  }
  long long mid = (l + r) >> 1;
  if (L <= mid) update(lc[o], l, mid, f, val, type);
  if (R > mid) update(rc[o], mid + 1, r, f, val, type);
}
const long long inf = 0x7fffffffffffffff;
long long dis[300010];
priority_queue<pair<long long, long long> > q;
long long vis[300010];
void dijkstra(long long s) {
  for (long long i = 1; i <= 300010; i++) dis[i] = inf, vis[i] = 0;
  dis[s] = 0;
  q.push(make_pair(0, s));
  while (q.size()) {
    long long x = q.top().second;
    q.pop();
    if (vis[x]) continue;
    vis[x] = 1;
    for (long long i = head[x]; i; i = e[i].nxt) {
      long long y = e[i].to, z = e[i].val;
      if (!vis[y] && dis[y] > dis[x] + z) {
        dis[y] = dis[x] + z;
        q.push(make_pair(-dis[y], y));
      }
    }
  }
}
inline long long read() {
  long long data = 0, w = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') data = data * 10 + ch - '0', ch = getchar();
  return data * w;
}
long long main() {
  n = read();
  m = read();
  s = 1;
  ncnt = n;
  buildOut(rt1, 1, n);
  buildIn(rt2, 1, n);
  while (m--) {
    long long f, t, val;
    L = read();
    R = read();
    val = read();
    for (long long f = L; f <= R; f++) {
      long long tmp = L;
      L = f + 1;
      long long opt = 2;
      update(rt1, 1, n, f, val, opt);
      L = tmp;
    }
  }
  dijkstra(s);
  printf("%lld\n", dis[n] < inf ? dis[n] : -1);
  return 0;
}
