#include <bits/stdc++.h>
using namespace std;
long long maxm = 100010;
long long maxn = 100010;
long long INF = 1000000010;
vector<vector<long long> > cost(maxn, vector<long long>(maxn));
vector<long long> d(maxn);
vector<vector<long long> > lmc(maxm, vector<long long>(3));
vector<bool> used(maxn);
int m, n;
void cos();
void dijkstra(int s);
void solve() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> lmc.at(i).at(0) >> lmc.at(i).at(1) >> lmc.at(i).at(2);
  }
  cos();
  dijkstra(1);
  if (d.at(n) == INF) {
    cout << -1 << endl;
  } else {
    cout << d.at(n) << endl;
  }
}
void cos() {
  for (int i = 0; i < m; i++) {
    for (int j = lmc.at(i).at(0); j < lmc.at(i).at(1); j++) {
      for (int k = j + 1; k <= lmc.at(i).at(1); k++)
        if (cost.at(j).at(k) == 0) {
          cost.at(j).at(k) = lmc.at(i).at(2);
        } else {
          cost.at(j).at(k) = min(cost.at(j).at(k), lmc.at(i).at(2));
        }
    }
  }
}
void dijkstra(int s) {
  for (long long i = 0; i < m; i++) {
    d.at(i) = INF;
    used.at(i) = false;
  }
  d.at(s) = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < n; u++) {
      if (!used.at(u) && (v == -1 || d.at(u) < d.at(v))) {
        v = u;
      }
    }
    if (v == -1) break;
    used.at(v) = true;
    for (int u = 0; u < n; u++) {
      d.at(u) = min(d.at(u), d.at(v) + cost.at(v).at(u));
    }
  }
}
int main() {
  solve();
  return 0;
}
