#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-9;
const long long INF = 10e17;
double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; }
std::istream &operator>>(std::istream &in, set<int> &o) {
  int a;
  in >> a;
  o.insert(a);
  return in;
}
std::istream &operator>>(std::istream &in, queue<int> &o) {
  long long a;
  in >> a;
  o.push(a);
  return in;
}
bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }
struct Edge {
  long long to, cost;
};
class Dijkstra {
  long long vector_count;
  vector<long long> distances;
  vector<vector<Edge>> edges;

 public:
  Dijkstra(long long n) {
    vector_count = n;
    distances.resize(n, INF);
    edges.resize(n);
  }
  void insertTowWay(long long from, long long to, long long cost) {
    edges[from].push_back({to, cost});
    edges[to].push_back({from, cost});
  }
  void insert(long long from, long long to, long long cost) {
    edges[from].push_back({to, cost});
  }
  void dijkstra(long long k) {
    distances[k] = 0;
    priority_queue<pair<long long, long long>,
                   vector<pair<long long, long long>>,
                   greater<pair<long long, long long>>>
        que;
    que.push(pair<long long, long long>(0, k));
    while (!que.empty()) {
      pair<long long, long long> p = que.top();
      que.pop();
      int v = p.second;
      if (distances[v] < p.first) {
        continue;
      }
      for (auto e : edges[v]) {
        if (distances[e.to] > distances[v] + e.cost) {
          distances[e.to] = distances[v] + e.cost;
          que.push(pair<long long, long long>(distances[e.to], e.to));
        }
      }
    }
  }
  long long distance(long long to) { return distances[to]; }
};
int main() {
  int n, m;
  cin >> n >> m;
  Dijkstra dijkstra(n);
  for (int _ = 0; _ < (m); ++_) {
    int l, r;
    long long cost;
    cin >> l >> r >> cost;
    l--;
    r--;
    for (int s = l; s <= r; s++) {
      for (long long t = l; t <= r; t++) {
        if (s == t) continue;
        dijkstra.insert(s, t, cost);
      }
    }
  }
  dijkstra.dijkstra(0);
  long long now = dijkstra.distance(n - 1);
  long long ans = (now == INF) ? -1 : now;
  cout << ans << endl;
}
