#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
Graph G(10);
using P = pair<long long, long long>;
int d[10];
int V;
void dijkstra(long long s, long long n) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + n, 1e+9);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) continue;
    for (int i = 0; i < G[v].size(); ++i) {
      Edge e = G[v][i];
      if (d[e.to] > d[v] + e.weight) {
        d[e.to] = d[v] + e.weight;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int main() {
  long long n, m;
  cin >> n >> m;
  for (int i = 0; i < (m); i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    for (long long j = l; j < r; j++) {
      for (long long k = j + 1; k <= r; k++) {
        long long from = j;
        long long to = k;
        G[from].push_back(Edge(to, c));
        G[to].push_back(Edge(from, c));
      }
    }
  }
  dijkstra(0, n);
  if (d[n - 1] == 0 || d[n - 1] == 1e+9) {
    cout << -1 << endl;
  } else {
    cout << d[n - 1] << endl;
  }
}
