#include <bits/stdc++.h>
using namespace std;
int n, m, g[105][105], a, b, c;
int dis[105];
bool vis[105];
void dijk(int s)
{
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
while (true)
{
int k = 0;
for (int i = 1; i <= n; i++)
if (vis[i] == false && dis[k] > dis[i])
k = i;
if (k == 0)
break;
vis[k] = 1;
for (int i = 1; i <= n; i++)
if (i != k && vis[i] == false && g[k][i] != 0 && dis[i] > dis[k] + g[k][i])
dis[i] = dis[k] + g[k][i];
}
}
int main()
{
cin >> n >> m;
while (m--)
{
cin >> a >> b >> c;
g[a][b] = g[b][a] = c;
}
dijk(1);
int ans = -1;
for (int i = 1; i <= n; i++)
ans = max(ans, dis[i]);
bool f = true;
for (int i = 1; i <= n; i++)
if (dis[i] == dis[0])
{
f = false;
break;
}
if (f)
cout << ans;
else
cout << -1;
return 0;
}