#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> v[105000];
vector<long long> cost(100050, 1e18);
long long use[105000] = {0};
long long ans = 10000000333;
long long N, M;
long long ser(long long pre, long long now, long long C, long long cou) {
  if (cou > N) {
    return 0;
  } else if (pre != -1)
    cost[now] = min(cost[now], C);
  for (long long i = 0; i < v[now].size(); i++) {
    if (v[now][i].first != pre)
      ser(now, v[now][i].first, C + v[now][i].second, cou + 1);
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  for (long long i = 0; i < N; i++) {
    v[i + 1].emplace_back(make_pair(i, 0));
  }
  long long bbb = 1e18;
  long long tmp, tmp1, tmp2;
  for (long long i = 0; i < M; i++) {
    cin >> tmp >> tmp1 >> tmp2;
    tmp--;
    tmp1--;
    v[tmp].emplace_back(make_pair(tmp1, tmp2));
  }
  ser(-1, 0, 0, 0);
  if (cost[N - 1] == bbb) {
    cout << -1 << endl;
    return 0;
  }
  cout << cost[N - 1] << endl;
}
