#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int t[MAXN * 16][2], hd[MAXN * 16], ver[MAXN * 24], nx[MAXN * 24],
    pos[MAXN * 8][2], vis[MAXN * 16];
int ra, rb, tot, eg, op, x, y, u, v, n, m, s;
long long edge[MAXN * 24], dis[MAXN * 16], w;
priority_queue<pair<long long, int> > q;
void add_edge(int x, int y, long long z) {
  ver[++eg] = y;
  nx[eg] = hd[x], edge[eg] = z;
  hd[x] = eg;
  return;
}
int buildt(int l, int r, int op) {
  int cur = ++tot;
  if (l == r) {
    pos[l][op] = cur;
    return cur;
  }
  int mid = (l + r) >> 1;
  t[cur][0] = buildt(l, mid, op), t[cur][1] = buildt(mid + 1, r, op);
  if (!op) {
    add_edge(t[cur][0], cur, 0), add_edge(t[cur][1], cur, 0);
  } else {
    add_edge(cur, t[cur][0], 0), add_edge(cur, t[cur][1], 0);
  }
  return cur;
}
void init(int p1, int p2, int l, int r) {
  if (l == r) {
    add_edge(p2, p1, 0);
    return;
  }
  int mid = (l + r) >> 1;
  init(t[p1][0], t[p2][0], l, mid), init(t[p1][1], t[p2][1], mid + 1, r);
  return;
}
void addin(int l, int r, int p, int xl, int xr, int s, long long w) {
  if (xr < l || r < xl) {
    return;
  }
  if (xl <= l && r <= xr) {
    add_edge(p, s, w);
    return;
  }
  int mid = (l + r) >> 1;
  addin(l, mid, t[p][0], xl, xr, s, w);
  addin(mid + 1, r, t[p][1], xl, xr, s, w);
  return;
}
void addout(int l, int r, int p, int s, int xl, int xr, long long w) {
  if (xr < l || r < xl) {
    return;
  }
  if (xl <= l && r <= xr) {
    add_edge(s, p, w);
    return;
  }
  int mid = (l + r) >> 1;
  addout(l, mid, t[p][0], s, xl, xr, w);
  addout(mid + 1, r, t[p][1], s, xl, xr, w);
  return;
}
void add(int l1, int r1, int l2, int r2, long long w) {
  int pos1 = tot + 1, pos2 = tot + 2;
  tot += 2;
  addin(1, n, ra, l1, r1, pos1, 0);
  add_edge(pos1, pos2, w);
  addout(1, n, rb, pos2, l2, r2, 0);
  return;
}
void dijkstra(int x) {
  q.push(make_pair(0, x));
  dis[x] = 0;
  while (!q.empty()) {
    pair<long long, int> a = q.top();
    q.pop();
    if (vis[a.second]) {
      continue;
    }
    vis[a.second] = 1;
    for (int i = hd[a.second]; i; i = nx[i]) {
      if (dis[ver[i]] > dis[a.second] + edge[i]) {
        dis[ver[i]] = dis[a.second] + edge[i];
        q.push(make_pair(a.first - edge[i], ver[i]));
      }
    }
  }
  return;
}
int main() {
  memset(dis, 0x3f, sizeof(dis));
  scanf("%d%d", &n, &m);
  s = 1;
  ra = buildt(1, n, 0), rb = buildt(1, n, 1);
  init(ra, rb, 1, n);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%lld", &x, &y, &w);
    add(x, y, x, y, w);
  }
  dis[pos[s][1]] = 0;
  dijkstra(pos[s][0]);
  printf("%lld ", dis[pos[n][1]] > 1e17 ? -1 : dis[pos[n][1]]);
  return 0;
}
