#include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template void chmin(T &a,const T &b){if(a>b) a=b;} template void chmax(T &a,const T &b){if(a rank,p; vector sz; DisjointSet(){} DisjointSet(int n):N(n),rank(n),p(n),sz(n){ for(int i=0;irank[y]) swap(x,y); p[x]=y; sz[y]+=sz[x]; if(rank[x]==rank[y]){ ++rank[y]; } } int leader(int x){ if(x!=p[x]) p[x]=leader(p[x]); return p[x]; } int size(int x){ return sz[leader(x)]; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N,M; cin>>N>>M; vector> edges; rep(i,M){ int a,b,c; cin>>a>>b>>c; a--;b--; edges.push_back(mkt(a,b,c)); } vector ord(M,0); rep(i,M) ord[i]=i; sort(all(ord),[&](int a,int b){ if(get<2>(edges[a])!=get<2>(edges[b])) return get<2>(edges[a]) > get<2>(edges[b]); return a> g(N); for(auto i:ord){ auto [a,b,c]=edges[i]; if(c>=lastCost){ g[a].push_back(b); g[b].push_back(a); } } queue Q; vector dist(N,MOD); dist[0]=0; Q.push(0); while(!Q.empty()){ int now=Q.front(); Q.pop(); for(auto nex:g[now]){ if(dist[nex]==MOD){ dist[nex]=dist[now]+1; Q.push(nex); } } } cout<