#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using cd = complex<double>;
const int maxn = 1e5 + 10;
vector<pair<int, int>> vec[5 * maxn] = {};
ll dist[5 * maxn] = {};
int current_special_node = 4 * maxn;
void add_edge(int x, int y, int c) { vec[x].emplace_back(c, y); }
void operation(int st, int dr, int cost) {
  int me = current_special_node++;
  for (st += maxn, dr += maxn + 1; st < dr; st /= 2, dr /= 2) {
    if (st % 2) {
      add_edge(st, me, cost);
      add_edge(me, st + 2 * maxn, 0);
      ++st;
    }
    if (dr % 2) {
      --dr;
      add_edge(dr, me, cost);
      add_edge(me, dr + 2 * maxn, 0);
    }
  }
}
void initialise() {
  for (int i = 1; i < maxn; ++i) {
    add_edge(2 * i, i, 0);
    add_edge(2 * i + 1, i, 0);
    add_edge(i + 2 * maxn, 2 * i + 2 * maxn, 0);
    add_edge(i + 2 * maxn, 2 * i + 1 + 2 * maxn, 0);
  }
  for (int i = 0; i < maxn; ++i) add_edge(i + 3 * maxn, i + maxn, 0);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  initialise();
  for (int i = 0, x, y, z; i < m; ++i) {
    cin >> x >> y >> z;
    operation(x - 1, y - 1, z);
  }
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      pq;
  memset(dist, 0x3f, sizeof(dist));
  dist[maxn] = 0;
  pq.push(make_pair(0ll, maxn));
  while (!pq.empty()) {
    int cur = pq.top().second, cur_dist = pq.top().first;
    cerr << cur << ' ' << cur_dist << endl;
    pq.pop();
    if (dist[cur] != cur_dist) continue;
    for (const auto next : vec[cur]) {
      if (cur_dist + next.first < dist[next.second]) {
        dist[next.second] = cur_dist + next.first;
        pq.push(make_pair(dist[next.second], next.second));
      }
    }
  }
  cout << (dist[maxn + n - 1] == 0x3f3f3f3f3f3f3f3fll ? -1 : dist[maxn + n - 1])
       << endl;
  return 0;
}
