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 <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

struct edge{int time, to;};



typedef pair<int, int> pii;
typedef pair<int, pii> piii;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    map<pii,int> mpp;
    // source code
    int N, M;
    cin >> N >> M;
    vector<int> used(N, 0);
    vector<vector<pair<int, int>>> G(N); // time, to
    rep(i, M){
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        G[a].pb(mp(c, b));
    }
    priority_queue<piii, vector<piii>, greater<piii> > pq;
    for(auto e: G[0]){
        pq.push(mp(e.first, mp(0, e.second)));
        mpp[pii(0, e.second)] = 1;
    }
    int ans = -1;
    used[0] = 1;
    int cur = -1;
    while(!pq.empty()){
        auto t = pq.top();
        pq.pop();
        int time = t.first, from = t.second.first, to = t.second.second;
        //        cout << time << ", " << from <<  ", " << to <<  endl;
        cur =  time;
        //        cout <<  cur <<  endl;
        mpp[pii(from, to)] = 1;
        
        used[to] = 1;
        if(to == N - 1 && used[from] == 1){
            
            ans = max(ans, time);
        }
        
        for(auto e: G[to])
        {
            if( mpp.count( pii(to,e.second) )==0 && not (used[e.second] == 1 && e.second!=N-1)){
                //                cout << "mpp" <<  endl;
                //
                if(cur <= e.first){
                    pq.push(mp(e.first, mp(to, e.second)));
                    mpp[pii(to,e.second)]=1;
                }
            }
            
        }
        
    }
    
    output(ans, 0);
    
    
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
const double PI  = 3.1415926535897932384626433832795028841971;
const int    INF = 1000000007;
const double EPS = 1e-10;
const int    MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> PP;

int n, m;
vector<P> e[100000];
int dist[100000];
priority_queue<P,vector<P>, greater<P> > que;

int main(){
    cin >> n >> m;
    rep(i,n) dist[i] = INF;
    dist[0] = 0;
    rep(i,m){
        int a, b, c;
        scanf("%d%d%d",&a,&b,&c);
        a--; b--;
        e[a].push_back(P(b,c));
    }
    que.push(P(0,0));
    while(que.size() != 0){
        P p = que.top();
        que.pop();
        if(dist[p.second] < p.first) continue;
        rep(i,e[p.second].size()){
            P to = e[p.second][i];
            if(to.second < dist[p.second]) continue;
            if(dist[to.first] <= to.second) continue;
            dist[to.first] = to.second;
            que.push(P(dist[to.first],to.first));
        }
    }
    int ans = 0;
    if(dist[n-1] == INF){
        cout << -1 << endl;
        return 0;
    }
    rep(i,n-1){
        if(dist[i] == INF) continue;
        rep(j,e[i].size()){
            if(e[i][j].first != n-1) continue;
            if(e[i][j].second < dist[i]) continue;
            ans = max(ans,e[i][j].second);
        }
    }
    cout << ans << endl;
}