Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
//	"strings"
    "sort"
//    "container/heap"
)

/*********** Input **************/
/*
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
	buf := make([]byte, 0, 1000000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}*/

func scanInt(sc *bufio.Scanner) int {
    sc.Scan()
    v, _ := strconv.Atoi(sc.Text())
    return v
}
/**************  End Input  **************/

func toInt(str string) int {
	n, _ := strconv.Atoi(str)
	return n
}

type Edge struct {
	to      int
	newTime int
}

type State struct {
	node int
	time int
}


/**************  Cell  *****************/

type Cell struct {
	item *State
	next *Cell
}

func NewCell(x *State, cp *Cell) *Cell {
	newCp := new(Cell)
	newCp.item = x
	newCp.next = cp
	return newCp
}

/************  End Cell  ***************/

/**************  Queue  ****************/

type Queue struct {
	size  int
	front *Cell
	rear  *Cell
}

func NewQue() *Queue {
	return new(Queue)
}

func (q *Queue) Push(x *State) {
	newNode := NewCell(x, nil)
	if q.size == 0 {
		q.front = newNode
		q.rear = newNode
	} else {
		q.rear.next = newNode
		q.rear = newNode
	}
	q.size++
}

func (q *Queue) PopLeft() (*State, bool) {
	if q.size == 0 {
		return nil, false
	} else {
		x := q.front.item
		q.front = q.front.next
		q.size--
		if q.size == 0 {
			q.rear = nil
		}
		return x, true
	}
}

func (q *Queue) IsEmpty() bool {
	return q.size == 0
}

/***********  end Queue  ***********/

type EdgeSorter struct {
    edges []Edge
    lessfunc []lessFunc
    used int
    length int
}

type lessFunc func(i, j *Edge)bool

func (ES EdgeSorter) Len() int {
    return len(ES.edges)
}

func (ES EdgeSorter) Swap(i, j int) {
    ES.edges[i], ES.edges[j] = ES.edges[j], ES.edges[i]
}

func (ES EdgeSorter) Less(i, j int) bool {
    var k int
    p, q := &ES.edges[i], &ES.edges[j]
    for k=0; k<len(ES.lessfunc)-1;k++ {
        less := ES.lessfunc[k]
        switch {
            case less(p, q):
                return true
            case less(q, p):
                return false
        }
    }
    return ES.lessfunc[k](p, q)
}

/*********** PriorityQueue ***********/
/*
type PriorityQueue struct {
    arr []*State
    size int
}

func (pq PriorityQueue) Len() int {
    return len(pq.arr)
}

func (pq PriorityQueue) Swap(i, j int) {
    pq.arr[i], pq.arr[j] = pq.arr[j], pq.arr[i]
}

func (pq PriorityQueue) Less(i, j int) bool{
    return pq.arr[i].time < pq.arr[j].time
}

func (pq *PriorityQueue) Push(x interface{}) {
    item := x.(*State)
    pq.arr = append(pq.arr, item)
    pq.size++
}

func (pq *PriorityQueue) Pop() interface{} {
    n := pq.size
    item := pq.arr[n - 1]
    pq.arr = pq.arr[:n - 1]
    pq.size--
    return item
}
*/


func main() {
    sc := bufio.NewScanner(os.Stdin)
    sc.Split(bufio.ScanWords)
	n, m := scanInt(sc), scanInt(sc)
	edges := make([]EdgeSorter, 0)
	for i := 0; i < n; i++ {
		edges = append(edges, EdgeSorter{})
	}
	for i := 0; i < m; i++ {
        a, b, c := scanInt(sc) - 1, scanInt(sc) - 1, scanInt(sc)
		edges[a].edges = append(edges[a].edges, Edge{to: b, newTime: c})
	}

    byNewTime := func(p1 *Edge, p2 *Edge) bool{
        return p1.newTime >= p2.newTime
    }

    for i, _ := range edges {
        v := &edges[i]
        v.lessfunc = append(v.lessfunc, byNewTime)
        sort.Sort(v)
        v.used = 0
        v.length = len(v.edges)
    }
    var minTime [100000]int
    for i := 0; i < n; i++ {
        minTime[i] = 2000000000
    }
    minTime[0] = 0
    que := Queue{}
    que.Push(&State{node: 0, time: 0})
	ans := -1
	for !que.IsEmpty() {
		state, _ := que.PopLeft()
		for edges[state.node].used < edges[state.node].length {
            e := edges[state.node].edges[edges[state.node].used]
			if e.newTime >= state.time {
                if e.to == n - 1 && e.newTime > ans {
                    ans = e.newTime
             

C++: #include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)

struct E {
  int from, to, c;
};

using G = vector<vector<E>>;

int main() {
  int n, m;
  cin >> n >> m;
  G g(n);
  set<pair<int, int>> st;
  REP(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (b == n - 1)
      st.insert({a, c});
    else
      g[a].push_back({a, b, c});
  }
  using P = pair<int, int>;
  constexpr int INF = 1e9 + 1;
  vector<int> d(n, INF);
  d[0] = 0;
  priority_queue<P, vector<P>, greater<>> q;
  q.push({0, 0});
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (p.first > d[p.second])
      continue;
    for (auto e : g[p.second]) {
      if (e.c >= d[p.second] && d[e.to] > e.c) {
        d[e.to] = e.c;
        q.push({e.c, e.to});
      }
    }
  }

  int ans = -1;
  for (auto a : st) {
    if (d[a.first] <= a.second)
      ans = max(ans, a.second);
  }
  cout << ans << endl;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
pair<int,pair<int,int> > p[2000000];
int m;
int tansa(int,int);
int main(){
  int n;

  cin >> n >> m;
  for(int i=0;i<m;i++){
    cin>>p[i].second.second>>p[i].first>>p[i].second.first; 
  }
  sort(p,p+m);
  for(int i=m-1;i>=0;i--){
    if(p[i].first==n){
      int ans;
      if(p[i].second.second==1){
	cout << p[i].second.first << endl;
	return 0;
      }
      ans=tansa(p[i].second.second,p[i].second.first);
      if(ans==-1)continue;
      cout << p[i].second.first << endl;
      return 0;
    }
    else{
      cout << -1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
int tansa(int iru,int time){
  int ans;
  for(int i=m-1;i>=0;i--){
    if(iru==p[i].first&&time>=p[i].second.first){
      if(p[i].second.second==1)return 1;
      
      ans=tansa(p[i].second.second,p[i].second.first);
      if(ans==1){
	return 1;
      }
      
      else continue;
    }
  }
  // else if(iru>p[i].first){
  return -1;
  //  }
  //}
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<int,pii> piii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define mp make_pair
#define pb push_back
#define INF 999999999
#define MAX_V 100000

struct st{int time,to;};
vector<st> G[MAX_V];

int main(){
	int n,m;
	cin>>n>>m;
	rep(i,m){
		int a,b,c;
		cin>>a>>b>>c;
		a--,b--;
		G[a].pb(st{c,b});
	}
	
	priority_queue<piii,vector<piii>,greater<piii> > pq;
	for(auto & edge:G[0]) pq.push(mp( edge.time,mp(0,edge.to) ));
	
	int d[MAX_V];
	rep(i,MAX_V)d[i] = INF;
	d[0]=0;
	
	while( pq.size() ){
		piii edge = pq.top();
		int time = edge.first, from = edge.second.first, to = edge.second.second;
		pq.pop();
		
		if( d[from]>time )continue;
		
		if(d[to] == INF){
			for( auto &edge : G[to] ){
				pq.push(mp( edge.time,mp(0,edge.to) ));
			}
		}
		
		if(d[to]>time) d[to] = time;
	}

	
	int ans = -1;
	rep(i,n){
		for(auto &edge:G[i]){
			if( edge.to==n-1 && d[i]<=edge.time ){
				ans = max(edge.time,ans);
			}
		}
	}
	cout<<ans<<endl;
}