Problem: Problem

There are N islands and M bridges. Numbers from 1 to N are assigned to each of the N islands. Numbers from 1 to M are also assigned to each of the M bridges.

Gaccho is currently on the first island (at time 0). Gaccho can move from the ai-th island to the bi-th island in one direction by using the i-th bridge.

However, at time 0, all the bridges were full of tide and sank into the sea. The i-th bridge will be able to cross the tide at time ci. And soon after the time ci, the tide rises again and the i-th bridge sinks again. When it sinks again, I don't know when the tide will pull and cross. So, Gaccho decided to think that such a bridge would never be crossed.

Gaccho wants to see the scenery of the 1st to N-1st islands for as long as possible, but since he is staying at the Nth island, he finally arrives at the Nth island. There must be. Also, because he keeps his parents waiting on the boat, Gacho must leave by boat and go home as soon as he arrives at the Nth island.

The time it takes for Gaccho to cross the bridge and travel around the island is very short, so you can assume it is 0. Find the maximum amount of time Gaccho can be on any of the 1st to N-1 islands. However, if you can't move to the Nth island no matter how you move, output -1 instead.

Constraints

* 2 ≤ N ≤ 105
* 1 ≤ M ≤ 2 × 105
* 1 ≤ ai <N
* 1 ≤ bi ≤ N
* 1 ≤ ci ≤ 109
* ai ≠ bi

Input

The input is given in the following format.


N M
a1 b1 c1
a2 b2 c2
...
aM bM cM


On the first line, two integers N and M are given, separated by blanks.
Three integers ai, bi, ci are given in each line i from the second line to the M + 1 line, separated by blanks.

Output

If Gaccho can move to the Nth island, it will output the maximum amount of time that Gaccho can be on any of the 1st to N-1th islands. If it cannot move to the Nth island, it prints -1 instead.

Examples

Input

3 2
1 2 10
2 3 20


Output

20


Input

4 4
1 2 27
1 3 37
2 3 47
3 1 57


Output

-1


Input

3 3
1 2 13
2 3 17
2 3 15


Output

17


Input

3 2
1 2 20
2 3 10


Output

-1


Input

3 2
1 2 10
2 3 10


Output

10

Solution (in CPP):

Solution:
#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;
}