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 <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;
}