#include <bits/stdc++.h>
int main(void) {
  unsigned int i;
  unsigned int n, m;
  scanf("%u %u", &n, &m);
  unsigned int tail[n + m - 1], head[n + m - 1], cost[n + m - 1];
  for (i = 0; i < m; i++) {
    scanf("%u %u %u", &tail[i], &head[i], &cost[i]);
    tail[i] -= 1;
    head[i] -= 1;
  }
  for (i = 0; i < n - 1; i++) {
    tail[i + m] = i + 1;
    head[i + m] = i;
    cost[i + m] = 0;
  }
  unsigned long dp[n];
  unsigned long inf = -1;
  unsigned int act = 0;
  char unexplr[n];
  dp[0] = 0;
  unexplr[0] = 0;
  for (i = 1; i < n; i++) {
    dp[i] = inf;
    unexplr[i] = 1;
  }
  unsigned long tmp;
  while (unexplr[n - 1]) {
    for (i = 0; i < m + n - 1; i++) {
      if (act == tail[i] && unexplr[head[i]]) {
        tmp = dp[act] + cost[i];
        if (tmp < dp[head[i]]) {
          dp[head[i]] = tmp;
        }
      }
    }
    tmp = inf;
    for (i = 0; i < n; i++) {
      if (unexplr[i] && dp[i] < tmp) {
        act = i;
        tmp = dp[i];
      }
    }
    unexplr[act] = 0;
  }
  if (dp[n - 1] < inf) {
    printf("%lu\n", dp[n - 1]);
  } else {
    printf("-1");
  }
  return 0;
}
