#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 10;
const long long inf = 2e18;
int N, M;
int h[maxn], e[maxn], ne[maxn], w[maxn], idx;
bool vis[maxn];
long long dis[maxn];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    heap;
void add(int a, int b, int c) {
  e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
long long Dijkstra() {
  for (int i = 1; i <= N; i++) dis[i] = inf;
  heap.push({0, 1});
  dis[1] = 0;
  while (heap.size()) {
    auto cur = heap.top();
    heap.pop();
    long long d = cur.first, s = cur.second;
    if (vis[s] == 1) continue;
    vis[s] = 1;
    for (int i = h[s]; i != -1; i = ne[i]) {
      long long j = e[i];
      if (d + w[i] < dis[j]) {
        dis[j] = d + w[i];
        heap.push({dis[j], j});
      }
    }
  }
  if (dis[N] == inf) return -1;
  return dis[N];
}
int main() {
  memset(h, -1, sizeof(h));
  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    add(i, i - 1, 0);
  }
  int a, b, c;
  for (int i = 0; i < M; i++) {
    scanf("%d%d%d", &a, &b, &c);
    add(a, b, c);
  }
  cout << Dijkstra() << endl;
  return 0;
}
