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 <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <list>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(d) string(1,d)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007

ll n, m;
vector<pair<ll, pair<ll, ll> > > v;

vector<int> uf, usz;
int nc;

void init(int n) {
  vector<int> uf_(n);
  vector<int> usz_(n, 1);
  uf = uf_;
  usz = usz_;
  nc = n;

  for (int i = 0; i < uf.size(); i++) {
    uf[i] = i;
  }
}

int find(int a) {
  return (uf[a] == a) ? a : uf[a] = find(uf[a]);
}

void union_(int a, int b) {
  a = find(a);
  b = find(b);

  if (a != b) {
    if (usz[a] >= usz[b]) {
      swap(a, b);
    }
    uf[a] = b;
    usz[b] += usz[a];
    nc--;
  }
}

int check(int a, int b) {
  return (find(a) == find(b)) ? 1 : 0;
}

int get_size(int a) {
  return usz[find(a)];
}

ll d[100005];

ll check(ll p) {
  clr(d, 0);
  init(n);
  d[n-1] = 1;
  rep(i, 0, v.sz) {
    if(v[i].se.fi==n-1&&v[i].fi == p){
      union_(v[i].se.fi, v[i].se.se);
      d[v[i].se.se] = 1;
    }
    if (v[i].se.fi!=n-1) {
      if (d[v[i].se.fi] == 1) {
        union_(v[i].se.fi, v[i].se.se);
        d[v[i].se.se] = 1;
      }
    }
  }
  if (d[0] == 1)return 1;
  return 0;
}

int main() {
  cin >> n >> m;
  vector<ll> ve;
  rep(i, 0, m) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--; b--;
    v.pb(mp(c, mp(b, a)));
    if (b == n - 1) {
      ve.pb(c);
    }
  }
  sort(all(v));
  reverse(all(v));
  sort(all(ve));
  if(ve.sz==0){
    cout << -1 << endl;
    return 0;
  }
  ll mx = -1;
  for(int i = ve.sz-1; i >= 0; i--){
    if(check(ve[i])==1){
      mx = max(mx,ve[i]);
    }
  }
  cout << mx << endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int,int> P;

vector<P> G[100000];
int hour[100000];

int main(){
    int N,M;
    cin>>N>>M;
    for(int i=0;i<M;i++){
        int a,b,c;
        cin>>a>>b>>c;
        a--;b--;
        G[a].emplace_back(c,b);
    }
    for(int i=0;i<N;i++){
        sort(G[i].begin(),G[i].end());
    }
    priority_queue<P,vector<P>,greater<P>> que;
    fill(hour,hour+N,1000000001);
    que.emplace(0,0);
    hour[0]=0;
    hour[N-1]= -1;
    while(!que.empty()){
        P p = que.top(); que.pop();
        int t=p.first,v=p.second;
        if(t!=N-1&&hour[v]<t) continue;
        if(t==N-1&&hour[v]>t) continue;
        for(auto ite=lower_bound(G[v].begin(),G[v].end(),P(t,0));ite!=G[v].end();ite++){
            int t2=ite->first, to=ite->second;
            if(to==N-1){
                if(hour[to]<t2){
                    que.emplace(t2,to);
                    hour[to]=t2;
                }
            }else{
                if(hour[to]>t2){
                    que.emplace(t2,to);
                    hour[to]=t2;
                }
            }
        }
    }
    cout<<hour[N-1]<<endl;
    return 0;
}