#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) using ll = long long; const int inf=1e9+7; const ll longinf=1LL<<60 ; const ll mod=1e9+7 ; int cnt[202020]; vector> v[202020]; ll ans; void dfs(int x, int p, int c){ if(c==2)++ans; for(auto to:v[x]){ if(to.first==p)continue; if(++cnt[to.second]==1)++c; if(c<=2)dfs(to.first,x,c); if(--cnt[to.second]==0)--c; } } int main(){ int n,k; cin>>n>>k; rep(i,n-1){ int x,y,z; cin>>x>>y>>z; --x;--y; v[x].emplace_back(y,z); v[y].emplace_back(x,z); } rep(i,n)dfs(i,-1,0); cout<