#include <bits/stdc++.h>
using namespace std;
template <typename T>
void debug(T arr);
template <typename T>
void debug(T arr, int dim);
void warshall_floyd(int n, vector<vector<long long>> &d) {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
};
int main(void) {
  long long n, m;
  long long l, r, c;
  cin >> n >> m;
  vector<vector<long long>> arr(n, vector<long long>(n, 122337203685477000));
  for (int i = 1; i < n; ++i) {
    arr[i][i - 1] = 0;
  }
  for (int i = 0; i < m; ++i) {
    scanf("%lld %lld %lld", &l, &r, &c);
    arr[l - 1][r - 1] = c;
  }
  warshall_floyd(n, arr);
  if (arr[0][n - 1] == 122337203685477000) {
    cout << -1 << endl;
  } else {
    cout << arr[0][n - 1] << endl;
  }
};
template <typename T>
void debug(T arr) {
  for (auto col : arr) {
    cout << col << " ";
  }
  cout << endl;
};
template <typename T>
void debug(T arr, int dim) {
  for (auto raw : arr) {
    for (auto col : raw) {
      cout << col << " ";
    }
    cout << endl;
  }
};
