#include <bits/stdc++.h>
using namespace std;
using in = int64_t;
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const in inf = 1e9 + 7;
using P = pair<in, in>;
vector<in> dx = {0, 1, -1, 0};
vector<in> dy = {1, 0, 0, -1};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  in n, m;
  cin >> n >> m;
  using T = tuple<in, in, in>;
  in x, y, z;
  priority_queue<P, vector<P>, greater<P>> que;
  priority_queue<T, vector<T>, greater<T>> hen;
  for (in i = 0; i < (in)(m); i++) {
    cin >> x >> y >> z;
    x--, y--;
    hen.emplace(x, y, z);
  }
  vector<in> d(n, -1);
  d[0] = 0;
  in now = 1;
  while (1) {
    tie(x, y, z) = hen.top();
    if (x == 0) {
      que.emplace(z, y);
      hen.pop();
    } else
      break;
  }
  while (!que.empty() && d[n - 1] == -1) {
    tie(x, y) = que.top();
    if (y < now) {
      que.pop();
      continue;
    }
    d[now] = x;
    while (!hen.empty()) {
      tie(x, y, z) = hen.top();
      if (x != now) break;
      hen.pop();
      que.emplace(z + d[now], y);
    }
    now++;
  }
  cout << d[n - 1] << endl;
}
