#include using namespace std; const long mod = 1000000007; long power(long k, long n) { long ans = 1; while (n) { if (n % 2) { ans *= k; ans %= mod; } k *= k; k %= mod; n /= 2; } return ans; } int main() { long k, m, n, ans = 0; cin >> k >> m >> n; set> s; for (long i = 0; i < m; i++) { long p, q, r; cin >> p >> q >> r; s.insert(make_tuple(p, q, r)); } vector t(n); for (long l = 0; l < power(k, n); l++) { long add = 1; for (long i = 0; i < n; i++) t.at(i) = l / power(k, i) % k + 1; if (t.at(0) != 1 || t.at(n - 1) != 1) continue; for (long i = 0; i < n - 2; i++) { if (s.find(make_tuple(t.at(i), t.at(i + 1), t.at(i + 2))) == s.end()) { add = 0; break; } } ans += add; } cout << ans << endl; }