#include <bits/stdc++.h>
const int MAXN = 1e5 + 5;
struct Edge {
  int u, v, next;
  long long w;
};
struct Point {
  int v;
  long long w;
  bool operator<(const Point &b) const { return w == b.w ? v < b.v : w > b.w; }
};
std::priority_queue<Point> que;
long long sample;
int n, m, head[MAXN], cnt, u[MAXN], v[MAXN];
bool vis[MAXN];
long long dis[MAXN], w[MAXN];
Edge edge[MAXN * 3];
void AddEdge(int u, int v, long long w) {
  edge[++cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt;
}
void Dijkstra() {
  Point tmp, temp;
  memset(dis, 0x3f3f3f, sizeof(dis));
  temp.v = 0;
  temp.w = 0;
  sample = dis[0];
  dis[0] = 0;
  que.push(temp);
  while (!que.empty()) {
    temp = que.top();
    que.pop();
    if (vis[temp.v]) continue;
    vis[temp.v] = 1;
    for (int i = head[temp.v]; i; i = edge[i].next) {
      if (dis[edge[i].v] > dis[temp.v] + edge[i].w) {
        dis[edge[i].v] = dis[temp.v] + edge[i].w;
        tmp.v = edge[i].v;
        tmp.w = dis[edge[i].v];
        que.push(tmp);
      }
    }
  }
}
bool cover(int s1, int t1, int s2, int t2) {
  if (t2 <= t1) return 0;
  return (s2 >= s1 && t2 <= t1) ||
         (((s2 <= s1 && t2 >= s1) || (s2 <= t1 && t2 >= t1)));
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %lld", &u[i], &v[i], &w[i]);
    if (u[i] == 1) AddEdge(0, i, w[i]);
  }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= m; j++)
      if (i != j && cover(u[i], v[i], u[j], v[j])) AddEdge(i, j, w[j]);
  Dijkstra();
  long long ans = LLONG_MAX;
  bool flag = 0;
  for (int j = 1; j <= m; j++)
    if (v[j] == n) {
      ans = std::min(ans, dis[j]);
    }
  printf("%lld", (ans == LLONG_MAX || ans == sample) ? -1 : ans);
  return 0;
}
