#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<long>> E(N, vector<long>(N, 0L));
  for (int i = 0; i < M; i++) {
    long L, R, C;
    cin >> L >> R >> C;
    for (int j = L; j <= R - 1; j++) {
      for (int k = j + 1; k <= R; k++) {
        if (E[j - 1][k - 1] == 0L || E[j - 1][k - 1] > C) {
          E[j - 1][k - 1] = C;
          E[k - 1][j - 1] = C;
        }
      }
    }
  }
  vector<int> S;
  vector<long> d(N, LONG_MAX);
  int s, e;
  s = 1;
  e = N;
  d[s - 1] = 0L;
  while (true) {
    long tmpmin = LONG_MAX;
    int tmpindex = -1;
    for (int i = 0; i < N; i++) {
      int flag = 0;
      for (int j = 0; j < S.size(); j++) {
        if (i == S[j]) {
          flag = 1;
        }
      }
      if (flag != 1) {
        if (d[i] < tmpmin) {
          tmpmin = d[i];
          tmpindex = i;
        }
      }
    }
    if (tmpindex == -1) {
      cout << -1;
      return 0;
    }
    S.push_back(tmpindex);
    for (int i = 0; i < N; i++) {
      int flag = 0;
      for (int j = 0; j < S.size(); j++) {
        if (i == S[j]) {
          flag = 1;
        }
      }
      if (E[tmpindex][i] != 0L && flag == 0) {
        if (d[i] > d[tmpindex] + E[tmpindex][i]) {
          d[i] = d[tmpindex] + E[tmpindex][i];
        }
      }
    }
    if (S.size() == N) {
      break;
    }
  }
  cout << d[e - 1];
  return 0;
}
