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 pair<int,int> pii;

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

#define CHMAX(a,b) a=max((a),(b))

int n,m;
int dist[125252];
vector<pii> g[125252];
bool used[125252];

const int INF = 1145141919;

int main(){
  scanf("%d%d",&n,&m);
  REP(i,m){
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    --a;--b;
    g[a].push_back(pii(b,c));
  }
  // dijkstra in 0~n-2
  REP(i,n)dist[i] = INF;
  // pair<-dist,pos>
  priority_queue<pii> Q;
  Q.push(pii(-0,0));
  dist[0] = 0;

  int ans = -1;
  while(!Q.empty()){
    pii P = Q.top(); Q.pop();
    int d = -P.first;
    int pos = P.second;
    // ???????????¢
    if(used[pos])continue;
    used[pos] = true;
    // ????????¨??¢?´¢
    for(const pii& P:g[pos]){
      int to = P.first;
      int c = P.second;
      // ????????§???
      if(c<d)continue;
      if(to==n-1){
        // ??´??????
        CHMAX(ans,c);
      }else{
        // ????????´??°
        if(dist[to] <= c)continue;
        dist[to] = c;
        Q.push(pii(-c,to));
      }
    }
  }
  printf("%d\n",ans);
  return 0;
}

C++: #include<bits/stdc++.h>

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

using namespace std;

typedef long long ll;

struct Edge {
  int dst;
  ll w;
};

struct Data {
  int cur;
  ll w;
  bool operator < ( const Data &data ) const {
    if( w != data.w ) return w < data.w;
    return cur < data.cur;
  }
};

const int MAX_V = 100010;

int V,E;
vector<Edge> G[MAX_V],rG[MAX_V];
ll maxi[MAX_V];
bool valid[MAX_V];
map<int,set<int> > mp;

bool check(ll limit) {
  memset(maxi,-1,sizeof maxi);
  priority_queue<Data> Q;
  /*
  for(set<int>::iterator itr=mp[limit].begin();itr!=mp[limit].end();itr++) {
    maxi[*itr] = limit;
    Q.push((Data){*itr,limit});
  }
  */
  rep(i,(int)G[V-1].size()) {
    if( G[V-1][i].w < limit ) continue;
    maxi[G[V-1][i].dst] = G[V-1][i].w;
    Q.push((Data){G[V-1][i].dst,G[V-1][i].w});
  }
  while( !Q.empty() ) {
    Data data = Q.top(); Q.pop();
    if( data.cur == 0 ) {
      return true;
    }
    rep(i,(int)G[data.cur].size()) {
      Edge &e = G[data.cur][i];
      if( e.dst == V-1 ) continue;
      if( !valid[e.dst] ) continue;
      if( !( data.w >= e.w ) )  continue;
      if( maxi[e.dst] < e.w ) {
	maxi[e.dst] = e.w;
	Q.push((Data){e.dst,e.w});
      }
    }
  }
  return false;
}


void pre_calc() {
  memset(valid,false,sizeof valid);
  deque<int> deq;
  deq.push_back(0);
  valid[0] = valid[V-1] = true;
  while( !deq.empty() ) {
    int cur = deq.front(); deq.pop_front();
    if( cur == V-1 ) continue;
    rep(i,(int)rG[cur].size()) {
      int next = rG[cur][i].dst;
      if( !valid[next] ) {
	valid[next] = true;
	deq.push_back(next);
      }
    }
  }
}


void compute() {
  pre_calc();
  vector<ll> vec;

  rep(i,(int)G[V-1].size()) {
    if( valid[G[V-1][i].dst] ) {
      vec.push_back(G[V-1][i].w);
    }
  }
  if( vec.empty() ) {
    puts("-1");
    return;
  }
  sort(vec.begin(),vec.end());
  int L = 0, R = (int)vec.size();
  while( R - L ) {
    int M = ( L + R ) / 2;
    if( check(vec[M]) ) L = M + 1;
    else                R = M;
  }
  if( L-1 < 0 ) {
    puts("-1");
  } else {
    cout << vec[L-1] << endl;
  }
}

int main() {
  cin >> V >> E;
  rep(i,E) {
    int a,b,c;
    cin >> a >> b >> c;
    --a, --b;
    G[b].push_back((Edge){a,c});
    rG[a].push_back((Edge){b,c});
  }
  compute();
  return 0;
}