#include <bits/stdc++.h>
using namespace std;
int par[int(0x7ffffff)];
int Rank[int(0x7ffffff)];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    Rank[i] = 0;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y]) Rank[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long N, M;
struct edge {
  long long from, to, cost;
};
struct range {
  long long L, R, C;
};
bool cmp(const range& a, const range& b) {
  if (a.L == b.L) return a.C < b.C;
  return a.L < b.L;
}
range road[100000];
edge es[100000];
long long d[100000];
long long E = 0;
void shortest_path(int s) {
  for (int i = 0; i < N; i++) d[i] = (1e9) * N;
  d[s] = 0;
  while (true) {
    bool update = false;
    for (int i = 0; i < E; i++) {
      edge e = es[i];
      if (d[e.from] != (1e9) * N && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (!update) break;
  }
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    long long L, R, C;
    cin >> L >> R >> C;
    L--;
    R--;
    road[i] = {L, R, C};
  }
  sort(road, road + M, cmp);
  long long start = road[0].L, to = road[0].R;
  long long cost = road[0].C;
  for (int i = 0; i < M; i++) {
    for (int s = start; s <= to; s++) {
      for (int t = s + 1; t <= to; t++) {
        es[E++] = {s, t, cost};
        es[E++] = {t, s, cost};
      }
    }
    if (i + 1 >= M) break;
    start = road[i + 1].L;
    to = road[i + 1].R;
    cost = road[i + 1].C;
  }
  shortest_path(0);
  if (d[N - 1] == (1e9) * N) {
    cout << -1 << endl;
    return 0;
  }
  cout << d[N - 1] << endl;
}
