
import std.stdio;
import std.typecons;

alias Tuple!(int, "from", int, "to", int, "day") Edge;

void main() {
  int nodes_len, edges_len;
  readf("%d %d\n", &nodes_len, &edges_len);
  Edge[] edges = new Edge[](edges_len);
  for(int from, to, day, i = 0; i < edges_len; i++) {
    readf("%d %d %d\n", &from, &to, &day);
    edges[i] = Edge(from, to, day);
  }
  
  int[] dist = new int[](nodes_len);
  foreach(ref d; dist) d = int.min;
  dist[0] = 0;

  while(true) {
    bool update = false;
    foreach(e; edges) {
      if (dist[e.from] != int.max && dist[e.to] < (dist[e.from] + e.day)) {
        dist[e.to] = dist[e.from] + e.day;
        update = true;
      }
    }
    if (!update) break;
  }
  writeln(dist[nodes_len - 1]);
}