#include <bits/stdc++.h>
long long int BIG_NUM = 998244353;
long long int dijkstra(const std::vector<std::map<int, int> >& adjacent,
                       int st) {
  std::priority_queue<std::pair<long long int, int>,
                      std::vector<std::pair<long long int, int> >,
                      std::greater<std::pair<long long int, int> > >
      que;
  std::vector<long long int> d(adjacent.size(), 1e9);
  d[st] = 0;
  que.push(std::pair<long long int, int>(0, st));
  while (!que.empty()) {
    std::pair<long long int, int> p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) {
      continue;
    }
    for (const auto& elem : adjacent[v]) {
      if (d[elem.first] > d[v] + elem.second) {
        d[elem.first] = d[v] + elem.second;
        que.push(std::pair<long long int, int>(d[elem.first], elem.first));
      }
    }
  }
  return d[d.size() - 1] == 1e9 ? -1 : d[d.size() - 1];
}
int main(void) {
  int n, m;
  std::cin >> n >> m;
  std::set<std::array<long long int, 3> > groups;
  for (int i = 0; i < m; i++) {
    std::array<long long int, 3> temp;
    std::cin >> temp[0] >> temp[1] >> temp[2];
    groups.insert(temp);
  }
  std::vector<long long int> important_points;
  for (const auto& elem : groups) {
    if (elem[0] != 1) important_points.push_back(elem[0] - 1);
    important_points.push_back(elem[1]);
  }
  important_points.push_back(1);
  std::sort(important_points.begin(), important_points.end());
  std::vector<std::map<int, int> > adjacent(important_points.size());
  long long int st = 0;
  for (const auto& elem : groups) {
    while (important_points[st] < elem[0]) st++;
    for (int i = st + 1; i < important_points.size(); i++) {
      if (important_points[i] <= elem[1]) {
        if (adjacent[st].find(i) == adjacent[st].end() ||
            adjacent[st][i] > elem[2]) {
          adjacent[st][i] = elem[2];
        }
      }
    }
  }
  std::cout << dijkstra(adjacent, 0) << std::endl;
}
