#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const int mx = 100010;
const long long mod = 1e9 + 7;
vector<long long> bfs(vector<vector<pair<long long, int>>> v, int x) {
  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      q;
  vector<long long> dist(v.size(), longinf);
  dist[x] = 0;
  q.push({0LL, x});
  while (q.size()) {
    pair<long long, int> p = q.top();
    q.pop();
    long long d = p.first, now = p.second;
    if (dist[now] < d) {
      continue;
    }
    for (auto to : v[now]) {
      if (dist[to.second] > d + to.first) {
        dist[to.second] = d + to.first;
        q.push({dist[to.second], to.second});
      }
    }
  }
  return dist;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<long long, int>>> v(n);
  for (int i = (int)(0); i < (int)(m); ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    v[x].emplace_back(z, y);
    for (int j = x + 1; j < y; j++) {
      v[y].emplace_back(0, j);
    }
  }
  vector<long long> dist = bfs(v, 0);
  long long ans = dist[n - 1];
  if (ans == longinf) ans = -1;
  cout << ans << endl;
  return 0;
}
