#include <bits/stdc++.h>
void swap(int* a, int* b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
}
int dup(int a, int d) {
  if (a % d == 0)
    return a / d;
  else
    return a / d + 1;
}
int mod(int a, int m) {
  if (a < 0)
    return a % m + m;
  else
    return a % m;
}
int GCD(int a, int b) {
  int c = 1, tmp = ((a) < (b) ? (b) : (a));
  b = ((a) < (b) ? (a) : (b));
  a = tmp;
  while (c != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int LCM(int a, int b) { return a * b / GCD(a, b); }
long long POW(long long a, long long n) {
  long long ans = 1;
  while (n > 0) {
    if (n & 1) ans = ans * a % 998244353;
    a = a * a % 998244353;
    n >>= 1;
  }
  return ans;
}
int icmp(const void* a, const void* b) { return *(int*)a - *(int*)b; }
int lcmp(const void* a, const void* b) {
  if (*(long long*)a > *(long long*)b) return 1;
  if (*(long long*)a < *(long long*)b) return -1;
  return 0;
}
int ccmp(const void* a, const void* b) { return *(char*)a - *(char*)b; }
int scmp(const void* a, const void* b) {
  return strcmp(*(char**)a, *(char**)b);
}
long long fac[200010], finv[200010], inv[200010];
void comi() {
  int i;
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (i = 2; i < 200010; i++) {
    fac[i] = fac[i - 1] * i % 998244353;
    inv[i] = 998244353 - inv[998244353 % i] * (998244353 / i) % 998244353;
    finv[i] = finv[i - 1] * inv[i] % 998244353;
  }
}
int com(int n, int k) {
  if (n < k || n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % 998244353) % 998244353;
}
int pid = 0, rpid[200010];
long long cpid[200010], que[200010];
int judge(int a, int b) {
  if (pid < b) return 1;
  if (pid < a || b == 0) return 0;
  return cpid[que[a]] < cpid[que[b]] ? 1 : 0;
}
void push(long long a, int n) {
  int i = ++pid;
  rpid[i] = n, que[0] = i;
  for (cpid[i] = a; judge(0, i / 2); i /= 2) que[i] = que[i / 2];
  que[i] = pid;
}
int pop() {
  int ans = rpid[que[1]], i, j = 2, max = que[--pid];
  for (i = 1; judge(i, pid); i = j)
    j = i * 2 + 1 - judge(i * 2, i * 2 + 1), que[i] = que[j];
  que[i / 2] = max;
  return ans;
}
long long Dijkstra(int v, int e, int* a, int* b, int* l) {
  int i, ver = 1, flag[200010] = {0}, pre[e + 1], last[e + 1];
  long long cost[v + 1];
  for (i = 1; i < v + 1; i++) cost[i] = 9223372036854775807, last[i] = -1;
  cost[1] = 0;
  for (i = 0; i < e; i++) pre[i] = last[a[i]], last[a[i]] = i;
  while (flag[ver] == 0) {
    flag[ver]++;
    for (i = last[ver]; i + 1; i = pre[i])
      if (cost[b[i]] > cost[ver] + l[i]) {
        cost[b[i]] = cost[ver] + l[i];
        push(cost[b[i]], b[i]);
      }
    while (flag[ver] && pid) ver = pop();
  }
  return cost[v] < 9223372036854775807 ? cost[v] : -1;
}
int main() {
  int N, M, i, ans = 0;
  scanf("%d%d", &N, &M);
  int L[N + M - 1], R[N + M - 1], C[N + M - 1];
  for (i = 0; i < N - 1; i++) L[i] = i + 2, R[i] = i + 1, C[i] = 0;
  for (i = N - 1; i < N + M - 1; i++) scanf("%d%d%d", &L[i], &R[i], &C[i]);
  printf("%lld\n", Dijkstra(N, N + M - 1, L, R, C));
  return 0;
}
