#include <bits/stdc++.h>
using namespace std;
#define fo(i,a,b) for(int i=(a);i<(b);i++)
#define MOD 1000000007
#define MP make_pair
#define PB push_back
typedef long long ll;
int N, M, D, ans;
struct thing {
ll a; int b;
inline void OR (thing& o) { a |= o.a, b |= o.b; }
inline bool get (int x) {
if (x<60) return a&(1ll<<x);
else return b&(1<<(x-60));
}
inline void set (int x) {
if (x<60) a |= (1ll<<x);
else b |= (1<<(x-60));
}
inline bool any () { return a||b; }
} edge[90][2], can[2][1<<21];
int main () {
scanf("%d %d %d", &N, &M, &D);
fo(i, 0, M) {
int a, b, c; scanf("%d %d %d", &a, &b, &c), a--, b--;
edge[a][c].set(b);
edge[b][c].set(a);
}
can[0][0].set(0);
fo(d, 0, D) {
fo(i, 0, 1<<d) {
can[(d+1)%2][i] = can[(d+1)%2][i+(1<<d)] = {0ll, 0};
fo(x, 0, N) if (can[d%2][i].get(x)) {
can[(d+1)%2][i].OR(edge[x][0]);
can[(d+1)%2][i+(1<<d)].OR(edge[x][1]);
}
}
}
fo(i, 0, 1<<D) ans += can[D%2][i].any();
printf("%d\n", ans);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZm8oaSxhLGIpIGZvcihpbnQgaT0oYSk7aTwoYik7aSsrKQojZGVmaW5lIE1PRCAxMDAwMDAwMDA3CiNkZWZpbmUgTVAgbWFrZV9wYWlyCiNkZWZpbmUgUEIgcHVzaF9iYWNrCnR5cGVkZWYgbG9uZyBsb25nIGxsOwoKaW50IE4sIE0sIEQsIGFuczsKCnN0cnVjdCB0aGluZyB7CglsbCBhOyBpbnQgYjsKCWlubGluZSB2b2lkIE9SICh0aGluZyYgbykgeyBhIHw9IG8uYSwgYiB8PSBvLmI7IH0KCWlubGluZSBib29sIGdldCAoaW50IHgpIHsKCQlpZiAoeDw2MCkgcmV0dXJuIGEmKDFsbDw8eCk7CgkJZWxzZSByZXR1cm4gYiYoMT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