#include <bits/stdc++.h>
using namespace std;
vector<long long> v[10500];
long long ans = 100000000333;
long long cost[10500][10500] = {0};
long long N, M;
long long ser(long long pre, long long now, long long C, long long cou) {
  if (cou >= N - 1) {
    return 0;
  }
  if (now == N - 1) {
    ans = min(ans, C);
    return 0;
  }
  for (long long i = 0; i < v[now].size(); i++) {
    if (v[now][i] != pre)
      ser(now, v[now][i], C + cost[now][v[now][i]], cou + 1);
  }
  return 0;
}
int main() {
  cin >> N >> M;
  for (long long i = 0; i < N; i++) {
  }
  long long tmp, tmp1, tmp2;
  for (long long i = 0; i < M; i++) {
    cin >> tmp >> tmp1 >> tmp2;
    tmp--;
    tmp1--;
    for (long long i = tmp; i <= tmp1; i++) {
      for (long long j = i; j <= tmp1; j++) {
        if (cost[i][j] == 0) {
          if (i != j) {
            v[i].push_back(j);
            v[j].push_back(i);
          }
        }
        if (cost[i][j] == 0 || ((cost[i][j] > tmp2) && cost[i][j] != 0)) {
          cost[i][j] = tmp2;
          cost[j][i] = tmp2;
        }
      }
    }
  }
  ser(-1, 0, 0, 0);
  if (ans == 100000000333) {
    cout << -1 << endl;
  }
  cout << ans << endl;
}
