#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int> >;
struct Edge {
  long long cost;
  int to;
};
int main() {
  int INF = 1e9;
  int N, M;
  cin >> N >> M;
  vector<int> Ls(M);
  vector<int> Rs(M);
  vector<int> Cs(M);
  vector<vector<int> > mat_E(N, vector<int>(N, INF));
  for (int mi = 0; mi < M; mi++) {
    cin >> Ls[mi] >> Rs[mi] >> Cs[mi];
    Ls[mi]--;
    Rs[mi]--;
  }
  for (int mi = 0; mi < M; mi++) {
    for (int li = Ls[mi]; li < Rs[mi]; li++) {
      for (int ri = li + 1; ri <= Rs[mi]; ri++) {
        if (Cs[mi] < mat_E[li][ri]) {
          mat_E[li][ri] = Cs[mi];
          mat_E[ri][li] = Cs[mi];
        }
      }
    }
  }
  vector<vector<Edge> > list_Es(N);
  Edge e;
  for (int li = 0; li < N; li++) {
    for (int ri = li + 1; ri < N; ri++) {
      if (mat_E[li][ri] < INF) {
        e.cost = mat_E[li][ri];
        e.to = ri;
        list_Es[li].push_back(e);
        e.to = li;
        list_Es[ri].push_back(e);
      }
    }
  }
  vector<long long> dis(N, INF);
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      que;
  dis[0] = 0;
  for (int ei = 0; ei < list_Es[0].size(); ei++) {
    que.push(pair<long long, int>(list_Es[0][ei].cost, list_Es[0][ei].to));
  }
  pair<long long, int> to;
  while (!que.empty()) {
    to = que.top();
    que.pop();
    if (to.first < dis[to.second]) {
      dis[to.second] = to.first;
      for (int ei = 0; ei < list_Es[to.second].size(); ei++) {
        e = list_Es[to.second][ei];
        if (dis[e.to] == INF) {
          que.push(pair<long long, int>(dis[to.second] + e.cost, e.to));
        }
      }
    }
  }
  if (dis[N - 1] == INF) dis[N - 1] = -1;
  cout << dis[N - 1];
  return 0;
}
