import sys # sys.setrecursionlimit(1000005) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = -1-(-1 << 31) inf = -1-(-1 << 62) md = 10**9+7 # md = 998244353 from collections import defaultdict n,m,k=LI() to=[[] for _ in range(n)] uv2z=defaultdict(int) for _ in range(m): u,v,z=LI() u,v=u-1,v-1 to[u].append(v) to[v].append(u) uv2z[u,v]=uv2z[v,u]=z def dfs(root=0): aa[root]=1 stack = [(u, root) for u in to[root]] lmin=rmin=inf lmax=rmax=-inf while stack: u, p = stack.pop() z = uv2z[u, p] if aa[u]: if aa[u]==aa[p]: # 2x=au(z-bu-bp) x2=aa[u]*(z-bb[u]-bb[p]) if x2 < 2 or x2 > k*2 or x2&1: print(0) exit() x=x2//2 if xx[root]==None: xx[root]=x elif xx[root]!=x: print(0) exit() elif bb[u]!=bb[p]: print(0) exit() else: parent[u]=p aa[u]=-aa[p] bb[u]=z-bb[p] if aa[u]==1: lmin,lmax=min(lmin,bb[u]),max(lmax,bb[u]) else: rmin, rmax = min(rmin, bb[u]), max(rmax, bb[u]) for v in to[u]: if aa[v]:continue stack.append((v,u)) # x+lmin>=1 # x>=1-lmin # -x+rmin>=1 # x<=rmin-1 # x+lmax<=k # x<=k-lmax # -x+rmax<=k # x>=rmax-k if xx[root]==None: mn=max(1-lmin,rmax-k,1) mx=min(rmin-1,k-lmax,k) a1=max(0,mx-mn+1) mn=max(1-lmin,rmax-k+1,1) mx=min(rmin-1,k-1-lmax,k-1) a2=max(0,mx-mn+1) return a1,a2 x=xx[root] if x+lmin<1 or -x+rmin<1: print(0) exit() a1=a2=0 if x+lmax<=k and -x+rmax<=k:a1=1 if x+lmax