/*
floyd求图的最小环:O(n^3)
无权图边权为1即可
对于无向图:
在flody外层循环至k时
我们求得了任意的i,j,允许经过[1...k-1]这些点的最短路径
如果此时对于任意的i,j,有g[k][i],dist[i][j],g[j][k]存在
且i,j,k两两不同,则说明存在一个k->i->j->k的一个环
对于有向图:
只要初始化dist[i][j] = INF
跑一遍floyd,dist[i][i]即为一个环
*/
#include <cstdio>
#include <algorithm>
using namespace std;
#define INF 0x3fffffff
int g[105][105]; //g[i][j] != INF表示i,j两个点有边
int dist[105][105];
int floyd(int n)
{
int res = INF;
for (int k = 1; k <= n; k++)
{
//除去k点的那个环剩下的那条最短路中一定不能有k
//所以先找环再松弛
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if( i == j || i == k || j == k ) continue; //i,j,k必须不同
if( g[k][i] == INF || g[j][k] == INF || dist[i][j] == INF ) continue;
//路径必须都存在
res = min(res,g[k][i] + dist[i][j] + g[j][k]); //求环
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
return res;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if( i == j ) dist[i][j] = 0;
else dist[i][j] = INF;
g[i][j] = INF;
}
}
for (int i = 0; i < m; i++)
{
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
dist[x][y] = min(dist[x][y],v); //可能有重边
dist[y][x] = dist[x][y];
g[x][y] = g[y][x] = dist[x][y];
}
int ans = INF;
ans = floyd(n);
if( ans == INF ) printf("It's impossible.\n");
else printf("%d\n",ans);
return 0;
}