#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  char ch;
  bool ok;
  for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') ok = 1;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  if (ok) x = -x;
}
const int maxn = 5e5 + 10;
int cnt, pre[maxn * 20], nxt[maxn * 20], h[maxn * 10], val[maxn * 20],
    pos[maxn * 10], w[maxn * 2], tot;
int id, n, m, s;
long long dis[maxn * 10];
bool vis[maxn * 10];
struct oo {
  int x;
  long long y;
};
priority_queue<oo> q;
bool operator<(oo a, oo b) { return a.y > b.y; }
void add(int x, int y, int z) {
  pre[++cnt] = y, nxt[cnt] = h[x], h[x] = cnt, val[cnt] = z;
}
void build_in(int x, int l, int r) {
  int mid = (l + r) >> 1;
  id = max(id, x);
  if (l == r) return pos[l] = x, void();
  build_in(x << 1, l, mid), build_in(x << 1 | 1, mid + 1, r);
  add(x, x << 1, 0), add(x, x << 1 | 1, 0);
}
void build_out(int x, int l, int r) {
  add(x, x + id, 0);
  int mid = (l + r) >> 1;
  if (x != 1) add(x + id, (x >> 1) + id, 0);
  if (l == r) return;
  build_out(x << 1, l, mid), build_out(x << 1 | 1, mid + 1, r);
}
void change_in(int x, int l, int r, int a, int b, int c, int d) {
  if (a <= l && b >= r) return add(c, x, d), void();
  int mid = (l + r) >> 1;
  if (a <= mid) change_in(x << 1, l, mid, a, b, c, d);
  if (b > mid) change_in(x << 1 | 1, mid + 1, r, a, b, c, d);
}
void change_out(int x, int l, int r, int a, int b, int c, int d) {
  if (a <= l && b >= r) return add(x + id, c, d), void();
  int mid = (l + r) >> 1;
  if (a <= mid) change_out(x << 1, l, mid, a, b, c, d);
  if (b > mid) change_out(x << 1 | 1, mid + 1, r, a, b, c, d);
}
void find(int x, int l, int r, int a, int b) {
  if (a <= l && b >= r) return w[++tot] = x, void();
  int mid = (l + r) >> 1;
  if (a <= mid) find(x << 1, l, mid, a, b);
  if (b > mid) find(x << 1 | 1, mid + 1, r, a, b);
}
void dijkstra(int s) {
  memset(dis, 63, sizeof dis);
  q.push((oo){s, dis[s] = 0});
  while (!q.empty()) {
    int x = q.top().x;
    q.pop();
    if (vis[x]) continue;
    vis[x] = 1;
    for (register int i = h[x]; i; i = nxt[i])
      if (!vis[pre[i]] && dis[pre[i]] > dis[x] + val[i]) {
        dis[pre[i]] = dis[x] + val[i];
        q.push((oo){pre[i], dis[pre[i]]});
      }
  }
}
int main() {
  read(n), read(m);
  s = 1;
  build_in(1, 1, n), build_out(1, 1, n);
  for (register int i = 1, x, y, v; i <= m; i++) {
    read(x), read(y), read(v);
    tot = 0;
    find(1, 1, n, x, y);
    for (register int j = 1; j <= tot; j++)
      change_in(1, 1, n, x, y, w[j] + id, v);
    for (register int j = 1; j <= tot; j++) change_out(1, 1, n, x, y, w[j], v);
  }
  dijkstra(pos[1]);
  printf("%lld\n", dis[pos[n]] >= 1e18 ? -1 : dis[pos[n]]);
}
