#include <bits/stdc++.h>
using namespace std;
const long mod = 1e9 + 7;
const int size = 2e5;
const long inf = 1e15;
int N, M;
pair<int, int> range[size];
long lgh[size];
long d[size];
void dijkstra(int a) {
  priority_queue<pair<long, int>> pq;
  for (int i = 0; i < N + M; i++) {
    d[i] = inf;
    pq.push({-d[i], i});
  }
  d[a] = 0;
  pq.push({0, a});
  int v;
  long temp;
  for (int i = 0; i < N + M; i++) {
    v = pq.top().second;
    pq.pop();
    if (v < N)
      for (int u = N; u < N + M; u++) {
        if (range[u - N].first <= v && v <= range[u - N].second) {
          temp = d[v] + lgh[u - N];
          if (d[u] > temp) {
            d[u] = temp;
            pq.push({-d[u], u});
          }
        }
      }
    else
      for (int u = range[v - N].first; u < range[v - N].second; u++) {
        temp = d[v] + lgh[v - N];
        if (d[u] > temp) {
          d[u] = temp;
          pq.push({-d[u], u});
        }
      }
  }
  return;
}
int main() {
  cin >> N >> M;
  int l, r;
  long c;
  for (int i = 0; i < M; i++) {
    cin >> l >> r >> c;
    l--;
    range[i] = {l, r};
    lgh[i] = c;
  }
  dijkstra(0);
  cout << (d[N - 1] == inf ? -1 : d[N - 1] / 2) << endl;
}
