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++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

class Edge
{
public:
    int to, c;
    Edge(int to, int c){
        this->to = to;
        this->c = c;
    }
};

const int MAX = INT_MAX / 2;

void solve(const vector<vector<Edge> >& edges, int start, vector<int>& ans)
{
    int n = edges.size();
    ans.resize(n, MAX);
    ans[start] = 0;
    multimap<int, int> mm;
    mm.insert(make_pair(0, start));

    while(!mm.empty()){
        int t = mm.begin()->first;
        int pos = mm.begin()->second;
        mm.erase(mm.begin());
        if(ans[pos] < t)
            continue;

        for(const Edge& e : edges[pos]){
            if(t <= e.c && e.c < ans[e.to]){
                ans[e.to] = e.c;
                mm.insert(make_pair(e.c, e.to));
            }
        }
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<Edge> > edges(n);
    vector<vector<Edge> > revEdges(n);
    for(int i=0; i<m; ++i){
        int a, b, c;
        cin >> a >> b >> c;
        -- a;
        -- b;
        edges[a].push_back(Edge(b, c));
        revEdges[b].push_back(Edge(a, c));
    }

    vector<int> t;
    solve(edges, 0, t);
    if(t[n-1] == MAX){
        cout << -1 << endl;
        return 0;
    }

    int ans = 0;
    for(const Edge& e : revEdges[n-1]){
        if(t[e.to] <= e.c)
            ans = max(ans, e.c);
    }
    cout << ans << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> tri;

class UnionFind{
public:
  vi p,r;
  UnionFind(int n):p(n,-1), r(n,0){ }
  int find(int x){ return (p[x]<0)?x:(p[x] = find(p[x])); }
  bool same(int x,int y){ return find(x)==find(y); }
  void unite(int x, int y){
    x = find(x); y = find(y);
    if(x == y) return;

    if(r[x] < r[y]){
      p[x] = y;
    }else{
      p[y] = x;
    }
    if(r[x] == r[y]) r[x]++;
  }
};

int main(){
  int n,m;
  cin >> n >> m;
  vector<tri> e;
  rep(i,m){
    int a,b,c;
    cin >> a >> b >> c;
    a--; b--;
    e.push_back( tri(c, pii(a,b)) );
  }

  sort(e.begin(), e.end());

  int ans = -1;
  UnionFind uf(n);
  rep(x,m){
    int c = e[x].first;
    map<int, vector<int> > g;
    queue<int> q;
    
    g[e[x].second.first].push_back(e[x].second.second);
    if(uf.same(0, e[x].second.first)) q.push(e[x].second.second);
    while(x+1 < m && c == e[x+1].first){
      x++;
      g[e[x].second.first].push_back(e[x].second.second);
      if(uf.same(0, e[x].second.first)) q.push(e[x].second.second);
    }

    while(q.size()){
      int v = q.front(); q.pop();
      if(v == n-1) ans = c;

      if(!uf.same(0,v)){
	uf.unite(0,v);
	for(int u : g[v]){
	  q.push(u);
	}
      }
    }    
  }

  cout << ans << endl;
}