#include <bits/stdc++.h>
using namespace std;
class Edge {
 public:
  long long to;
  long long cost;
  Edge() {}
  Edge(long long _to, long long _cost) {
    to = _to;
    cost = _cost;
  }
};
class Mov {
 public:
  long long id;
  long long cost;
  Mov(long long _id, long long _cost) {
    id = _id;
    cost = _cost;
  }
  Mov(const Mov& m) {
    id = m.id;
    cost = m.cost;
  }
};
class Node {
 public:
  long long cost;
  vector<Edge> edge;
  Node() { cost = LLONG_MAX / 2; }
};
bool has_larger_cost(const Mov& a, const Mov& b) { return a.cost > b.cost; }
int main() {
  long long nnode, nedge;
  cin >> nnode >> nedge;
  vector<Node> node(nnode);
  for (long long x = 0; x < nnode; x++) {
    for (long long y = 1; y < x; y++) {
      node.at(x).edge.emplace_back(y, 0);
    }
  }
  for (long long i = 0; i < nedge; i++) {
    long long from, to, cost;
    cin >> from >> to >> cost;
    from--;
    to--;
    node.at(from).edge.emplace_back(to, cost);
  }
  priority_queue<Mov, vector<Mov>, decltype(&has_larger_cost)> pq(
      &has_larger_cost);
  pq.emplace(0LL, 0LL);
  while (!pq.empty()) {
    auto mov = pq.top();
    pq.pop();
    if (node.at(mov.id).cost <= mov.cost) {
      continue;
    }
    node.at(mov.id).cost = mov.cost;
    for (const auto& e : node.at(mov.id).edge) {
      pq.emplace(e.to, e.cost + mov.cost);
    }
  }
  if (node.back().cost == LLONG_MAX / 2) {
    cout << "-1\n";
  } else {
    cout << node.back().cost << '\n';
  }
  return 0;
}
