#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define inf 100000000000000000 #define mod 1000000007 #define maxn 1000010 #define lowbit(x) (x&-x) #define eps 1e-10 int fa[maxn],a[maxn]; struct node { int x,y; }; vectorq[maxn]; int find(int x) { if(fa[x]==x) return x; return fa[x]=find(fa[x]); } int main() { int n,m,i,j,x,y,z; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) fa[i]=i; for(i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&z); if(x==y) continue; node tmp; tmp.x=y;tmp.y=z; q[x].push_back(tmp); tmp.x=x;tmp.y=z; q[y].push_back(tmp); int t1=find(x),t2=find(y); if(t1!=t2) fa[t1]=t2; } int num=0; for(i=2;i<=n;i++) if(fa[find(i)]!=fa[find(i-1)]) num++; if(num>0) printf("0\n"); else { int ans=1000000000; for(i=1;i<=n;i++) { int t=0; for(j=0;j