#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_set>
#include <unordered_map>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
map<int, pair<int, int>>T;//修复那条
int vallige[1003];//村庄
int N, M;
int find(int x) {
if (vallige[x] == x)
return x;
return vallige[x] = find(vallige[x]);
}
bool IS(int t) {
for (auto a : T) {
if (a.first > t)
break;
else {
if (find(a.second.first) != find(a.second.second))
vallige[find(a.second.first)] = vallige[find(a.second.second)];
}
}
//验证合理性
int root = find(vallige[1]);
for (int i = 1; i <= N; i++) {
if (find(vallige[i]) != root)
return false;
}
return true;
}
int main() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
int a, b, c;
cin >> a >> b >> c;
T[c] = { a,b };
}
//二分查找满足要求的t
int l = 0, r = 100005;
while (l + 1 != r) {
int mid = (l + r) / 2;
for (int i = 1; i <= N; i++) {
vallige[i] = i;
}
if (IS(mid)) {
r = mid;
}
else {
l= mid;
}
}
if (r == 100005)
cout << -1 ;
else
cout << r ;
return 0;
}