#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const long long LINF = 1001002003004005006ll;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);
  vector<vector<int>> co(n);
  for (int i = 0; i < (m); i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    to[u].push_back(v);
    co[u].push_back(c);
  }
  for (int i = 0; i < (n - 1); i++) {
    to[i + 1].push_back(i);
    co[i + 1].push_back(0);
  }
  int start = 0;
  int end = n - 1;
  vector<pair<int, int>> dist(n, pair<int, int>(INF, -1));
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  dist[start] = pair<int, int>(0, -1);
  q.push(pair<int, int>(0, start));
  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int u = p.second;
    if (dist[u].first < p.first) continue;
    for (int i = 0; i < (to[u].size()); i++) {
      int v = to[u][i], c = co[u][i];
      if (dist[v].first > dist[u].first + c) {
        dist[v] = pair<int, int>(dist[u].first + c, u);
        q.push(pair<int, int>(dist[v].first, v));
      }
    }
  }
  if (dist[end].first == INF)
    cout << -1 << endl;
  else
    cout << dist[end].first << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
