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<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct E{
  ll to,cost;
  E(ll to,ll cost):to(to),cost(cost){}
};
typedef pair<ll,ll> P;
int main(){
  ll n,m,i,j,k,a,b,c;
  cin>>n>>m;
  vector<E> v[n];
  vector<E> can;
  for(i=0;i<m;i++){
    cin >> a >> b >> c;
    a--;b--;
    if(b==n-1) can.push_back(E(a,c));
    else v[a].push_back(E(b,c));
  }
  ll g[n];
  memset(g,-1,sizeof(g));
  priority_queue<P,vector<P>,greater<P> > q;
  q.push(P(1,0));
  while(!q.empty()){
    P p=q.top();q.pop();
    b=p.first;a=p.second;
    if(~g[a]&&g[a]<=b) continue;
    g[a]=b;
    for(i=0;i<v[a].size();i++){
      if(b<=v[a][i].cost)
	if(!~g[v[a][i].to]||g[v[a][i].to]>v[a][i].cost)
	  q.push(P(v[a][i].cost,v[a][i].to));
    }
  }
  ll ans=-1;
  for(i=0;i<can.size();i++)
    if(~g[can[i].to]&&g[can[i].to]<=can[i].cost) ans=max(ans,can[i].cost);
  cout << ans << endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
#define	to first
#define co second
using namespace	std;
int n,m,r;
vector <pair<int,int> > side[100000];
void dfs(int where,int time){
  if(where==n-1) r=max(r,time);
  for(int i=0;i<side[where].size();i++)
    if(side[where][i].co>=time)
      dfs(side[where][i].to,side[where][i].co);
}
int main(){
  cin>>n>>m;
  for(int i=0,a,b,c;i<m;i++){
    cin>>a>>b>>c;
    side[a-1].push_back(make_pair(b-1,c));
  }
  dfs(0,0);
  if(!r) cout<<-1<<endl;
  else cout<<r<<endl;
  return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
const int inf=2e9;
#define pb push_back
int main(){
  int n,m;
  cin>>n>>m;
  vvvi cost(n,vvi(n));
  //  rep(i,n)cost[i][n-1]=-1;
  rep(i,m){
    int a,b,c;
    cin>>a>>b>>c;
    a--;b--;
    cost[a][b].pb(c);
    //    if(b==n-1)cost[a][b]=max(cost[a][b],c);
    //else cost[a][b]=min(cost[a][b],c);
  }
  vi used(n);
  vi d(n,inf);
  d[0]=0;
  used[n-1]=true;
  while(1){
    int v=-1;
    rep(i,n)if(!used[i]&&(v==-1||d[i]<d[v]))v=i;
    if(v==-1)break;
    used[v]=true;
    rep(i,n-1)rep(j,cost[v][i].size())if(d[i]>cost[v][i][j])d[i]=cost[v][i][j];
  }
  int out=-1;
  rep(i,n-1)rep(j,cost[i][n-1].size())if(d[i]<=cost[i][n-1][j])out=max(out,cost[i][n-1][j]);
  cout<<out<<endl;
  //rep(i,n)cout<<d[i]<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int,P> P3;
typedef pair<double, P3> P4;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int n, m, dmin[MAX_N],dmax[MAX_N];
vector<vector<P> > g;

int dijkstra(){
    fill(dmax, dmax+n, -1);
    fill(dmin, dmin+n, IINF);
    priority_queue<P, vector<P>, greater<P> > que;
    que.push({0,0});
    while(!que.empty()){
        P p = que.top();
        que.pop();
        int t = p.first, v = p.second;
        if(dmin[v] < t)continue;
        for(auto &e : g[v]){
            int u = e.first, c = e.second;
            if(c < t)continue;
            if(dmin[u] > c){
                dmin[u] = c;
                que.push({c,u});
            }
            dmax[u] = max(dmax[u],c);
        }
    }
}

int main() {
    cin >> n >> m;
    g.resize(n);
    REP(i,m){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        g[a].push_back({b,c});
    }
    dijkstra();
    if(dmin[n-1]==IINF){
        cout << -1 << endl;
    }
    else{
        cout << dmax[n-1] << endl;
    }
    return 0;
}