#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long int INF = 10000000000000000;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<long long int, long long int>>> edge(
      n + 1, vector<pair<long long int, long long int>>(0));
  for (int i = 0; i < n; i++) edge[i + 1].push_back({i, 0});
  int a, b, c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    edge[a].push_back({b, c});
  }
  vector<long long int> d(n + 1, INF);
  vector<bool> used(n + 1, false);
  d[1] = 0;
  while (true) {
    long long int v = -1;
    for (int i = 1; i <= n; i++) {
      if (!used[i] && (v == -1 || d[i] < d[v])) v = i;
    }
    if (v == -1) break;
    used[v] = true;
    for (int i = 0; i < edge[v].size(); i++) {
      d[edge[v][i].first] = min(d[edge[v][i].first], d[v] + edge[v][i].second);
    }
  }
  if (d[n] == INF)
    cout << -1;
  else
    cout << d[n];
  return 0;
}
