#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
vector<long long> d(100000, 1000000000000000000LL);
void dijkstra(long long s, vector<vector<long long>> edge) {
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      que;
  d[s] = 0;
  que.push(pair<long long, long long>(0, s));
  while (!que.empty()) {
    pair<long long, long long> p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first) continue;
    for (long long i = 0; i < edge[v].size(); i++) {
      long long cost = edge[v][i];
      if (d[i] > d[v] + cost) {
        d[i] = d[v] + cost;
        que.push(pair<long long, long long>(d[i], i));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, pair<long long, long long>>> clr(m);
  for (int i = 0; i < ((int)m); i++) {
    cin >> clr[i].second.first >> clr[i].second.second >> clr[i].first;
    clr[i].second.first--;
    clr[i].second.second--;
  }
  sort(clr.begin(), clr.end());
  vector<vector<long long>> edge(n,
                                 vector<long long>(n, 1000000000000000000LL));
  vector<bool> visited(n, false);
  for (int i = 0; i < ((int)m); i++) {
    long long l = clr[i].second.first;
    long long r = clr[i].second.second;
    long long c = clr[i].first;
    for (int j = l; j < r; j++) {
      for (int k = j + 1; k <= r; k++) {
        if (edge[j][k] != 1000000000000000000LL)
          continue;
        else
          edge[j][k] = c;
      }
    }
    for (long long j = l; j <= r; j++) visited[j] = true;
  }
  dijkstra(0, edge);
  if (d[n - 1] == 1000000000000000000LL)
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
}
