#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using i32 = int;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
long long power(long long a, long long b, long long p) {
  if (!b) return 1;
  long long t = power(a, b / 2, p);
  t = t * t % p;
  if (b & 1) t = t * a % p;
  return t;
}
long long exgcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long px, py;
  long long d = exgcd(b, a % b, px, py);
  x = py;
  y = px - a / b * py;
  return d;
}
template <class T>
inline void freshmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <class T>
inline void freshmax(T &a, const T &b) {
  if (a < b) a = b;
}
namespace io {
const int BUFSIZE = 1 << 20;
int isize, osize;
char ibuf[BUFSIZE + 10], obuf[BUFSIZE + 10];
char *is, *it, *os = obuf, *ot = obuf + BUFSIZE;
char getchar() {
  if (is == it) {
    is = ibuf;
    it = ibuf + read(STDIN_FILENO, ibuf, BUFSIZE);
    if (is == it) return EOF;
  }
  return *is++;
}
char getalpha() {
  char c = getchar();
  while (!isalpha(c)) c = getchar();
  return c;
}
void putchar(char c) {
  *os++ = c;
  if (os == ot) {
    write(STDOUT_FILENO, obuf, BUFSIZE);
    os = obuf;
  }
}
int inp() {
  int x = 0, f = 0;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar()) {
    if (ch == '-') f = 1;
  }
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return f ? -x : x;
}
long long inp_ll() {
  long long x = 0;
  int f = 0;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') f = 1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return f ? -x : x;
}
template <class T>
bool read(T &x) {
  x = 0;
  char ch = getchar();
  if (ch == EOF) return 0;
  for (; !isdigit(ch);) {
    ch = getchar();
    if (ch == EOF) return 0;
  }
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return 1;
}
template <class T>
void write(T x) {
  static char s[22];
  static char *it = s + 20;
  static char *end = s + 20;
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  do {
    *--it = x % 10 + '0';
    x /= 10;
  } while (x);
  for (; it < end; ++it) putchar(*it);
}
template <>
void write(const char *s) {
  for (; *s; ++s) putchar(*s);
}
template <>
void write(char c) {
  putchar(c);
}
template <class T, class V>
void write(T x, V y) {
  write(x);
  write(y);
}
template <class T>
void writeln(T x) {
  write(x);
  putchar('\n');
}
struct ender {
  ~ender() {
    if (os != obuf) write(STDOUT_FILENO, obuf, os - obuf);
  }
} __ender;
}  // namespace io
template <class T>
void print(const T &a) {
  for (auto x : a) printf("%d ", x);
  puts("");
}
const int MAXN = 500010;
const int MAXK = 20;
const i64 INF = 2000000000000000000LL;
const int MOD = 998244353;
using namespace io;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int F[MAXN];
int father(int x) { return F[x] == x ? x : F[x] = father(F[x]); }
template <class weight_type>
struct weighted_directed_graph {
  int n;
  vector<vector<pair<int, weight_type>>> v;
  weighted_directed_graph(int n = 0) { init(n); }
  void init(int n) {
    assert(n >= 0);
    this->n = n;
    v = vector<vector<pair<int, weight_type>>>(n + 1);
  }
  void addedge(int x, int y, weight_type z) {
    assert(1 <= x && x <= n);
    assert(1 <= y && y <= n);
    v[x].push_back({y, z});
  }
  vector<weight_type> dis;
  weight_type shortest_path(int s, int t) {
    dis = vector<weight_type>(n + 1, INF);
    dis[s] = 0;
    priority_queue<pair<weight_type, int>, vector<pair<weight_type, int>>,
                   greater<pair<weight_type, int>>>
        Q;
    Q.push({0, s});
    while (!Q.empty()) {
      int d, x;
      tie(d, x) = Q.top();
      Q.pop();
      if (dis[x] != d) continue;
      for (auto e : v[x]) {
        int y, z;
        tie(y, z) = e;
        if (dis[y] > d + z) Q.push({dis[y] = d + z, y});
      }
    }
    return dis[t];
  }
};
int n, m;
int a[MAXN];
weighted_directed_graph<long long> G;
void addedge(int a, int b, int c) { G.addedge(a, b, c); }
void build(int k, int L, int R) {
  if (L == R) {
    addedge(L, n + m + k, 0);
    addedge(n + m + 4 * n + k, L, 0);
    return;
  }
  int mid = (L + R) / 2;
  build(k << 1, L, mid);
  build(k << 1 | 1, mid + 1, R);
  addedge(n + m + (k << 1), n + m + k, 0);
  addedge(n + m + (k << 1 | 1), n + m + k, 0);
  addedge(n + m + 4 * n + k, n + m + 4 * n + (k << 1), 0);
  addedge(n + m + 4 * n + k, n + m + 4 * n + (k << 1 | 1), 0);
}
void go(int k, int L, int R, int x, int y, int z, int id) {
  if (L == x && R == y) {
    addedge(n + m + k, n + id, z);
    addedge(n + id, n + m + 4 * n + k, 0);
    return;
  }
  int mid = (L + R) / 2;
  if (y <= mid)
    go(k << 1, L, mid, x, y, z, id);
  else if (x > mid)
    go(k << 1 | 1, mid + 1, R, x, y, z, id);
  else {
    go(k << 1, L, mid, x, mid, z, id);
    go(k << 1 | 1, mid + 1, R, mid + 1, y, z, id);
  }
}
void solve() {
  n = inp();
  m = inp();
  G.init(n + m + 4 * n + 4 * n);
  build(1, 1, n);
  for (int i = 1; i <= m; ++i) {
    int x = inp();
    int y = inp();
    int z = inp();
    go(1, 1, n, x, y, z, i);
  }
  long long ans = G.shortest_path(1, n);
  if (ans == INF) ans = -1;
  writeln(ans);
}
int main() {
  solve();
  return 0;
}
