#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
struct Edge {
  int to, next, w;
} e[maxn];
int n, m, cnt, p[maxn], Dis[maxn];
int In[maxn];
bool visited[maxn];
void Add_Edge(const int x, const int y, const int z) {
  e[++cnt].to = y;
  e[cnt].next = p[x];
  e[cnt].w = z;
  p[x] = cnt;
}
bool Spfa(const int S) {
  int i, t, temp;
  queue<int> Q;
  memset(visited, 0, sizeof(visited));
  memset(Dis, 0x3f, sizeof(Dis));
  memset(In, 0, sizeof(In));
  Q.push(S);
  visited[S] = true;
  Dis[S] = 0;
  while (!Q.empty()) {
    t = Q.front();
    Q.pop();
    visited[t] = false;
    for (i = p[t]; i; i = e[i].next) {
      temp = e[i].to;
      if (Dis[temp] > Dis[t] + e[i].w) {
        Dis[temp] = Dis[t] + e[i].w;
        if (!visited[temp]) {
          Q.push(temp);
          visited[temp] = true;
          if (++In[temp] > n) return false;
        }
      }
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int x, y, _;
    scanf("%d%d%d", &x, &y, &_);
    while (y > x) {
      Add_Edge(x, y, _);
      y--;
    }
  }
  Spfa(1);
  if (Dis[n] >= 0x3f)
    printf("-1\n");
  else
    printf("%d\n", Dis[n]);
  return 0;
}
