from collections import deque class mf_graph: n=1 g=[[] for i in range(1)] pos=[] def __init__(self,N): self.n=N self.g=[[] for i in range(N)] self.pos = [] def add_edge(self,From,To,cap): assert 0<=From and From0): v=que.popleft() for e in self.g[v]: if e["cap"]==0 or level[e["to"]]>=0:continue level[e["to"]]=level[v]+1 if e["to"]==t:return que.append(e["to"]) def dfs(func,v,up): if (v==s):return up res=0 level_v=level[v] for i in range(Iter[v],len(self.g[v])): e=self.g[v][i] if (level_v<=level[e["to"]] or self.g[e["to"]][e["rev"]]["cap"]==0):continue d=func(func,e["to"],min(up-res,self.g[e["to"]][e["rev"]]["cap"])) if d<=0:continue self.g[v][i]["cap"]+=d self.g[e["to"]][e["rev"]]["cap"]-=d res+=d if res==up:return res level[v]=self.n return res flow=0 while(flow0): p=que.popleft() visited[p]=True for e in self.g[p]: if e["cap"] and not(visited[e["to"]]): visited[e["to"]]=True que.append(e["to"]) return visited n, m, k, l = map(int, input().split()) x = [0] * l; y = [0] * l; z = [0] * l for i in range(l) : x[i], y[i], z[i] = map(int, input().split()) edges = [[] for i in range(k + 1)] for i in range(l) : edges[z[i]].append(i) alivex = [True] * (n + 1) alivey = [True] * (m + 1) wentx = [False] * (n + 1) wenty = [False] * (m + 1) val = [0] * (k + 1) matchx = [0] * (n + 1) matchy = [0] * (m + 1) S = 0; T = n + m + 1 G = mf_graph(n + m + 2) for j in range(1, n + 1) : G.add_edge(S, j, 1) for j in range(1, m + 1) : G.add_edge(j + n, T, 1) previous_edges = [] for i in range(k, -1, -1) : for j in edges[i] : if alivex[x[j]] and alivey[y[j]] : previous_edges.append(j) G.add_edge(x[j], y[j] + n, 1) gx = [[] for j in range(n + 1)] gy = [[] for j in range(m + 1)] for j in previous_edges : gx[x[j]].append(y[j]) gy[y[j]].append(x[j]) val[i] = G.flow(S, T) for j in range(1, n + 1) : matchx[j] = -1 for j in range(1, m + 1) : matchy[j] = -1 for j in range(1, n + 1) : wentx[j] = False for j in range(1, m + 1) : wenty[j] = False for j in range(1, n + 1) : for e in G.g[j] : if e["to"] != S and e["cap"] == 0 and G.g[e["to"]][e["rev"]]["cap"] == 1 : matchx[j] = e["to"] - n matchy[e["to"] - n] = j qu = [] for j in range(1, n + 1) : if matchx[j] < 0 : wentx[j] = True qu.append(j) while len(qu) : idx = qu.pop() for j in gx[idx] : if not wentx[matchy[j]] : wentx[matchy[j]] = True qu.append(matchy[j]) for j in range(1, n + 1) : if not wentx[j] : alivex[j] = False for j in range(1, m + 1) : if matchy[j] < 0 : wenty[j] = True qu.append(j) while len(qu) : idx = qu.pop() for j in gy[idx] : if not wenty[matchx[j]] : wenty[matchx[j]] = True qu.append(matchx[j]) for j in range(1, m + 1) : if not wenty[j] : alivey[j] = False gsz = [len(G.g[j]) for j in range(0, n + 1)] for j in range(1, n + 1) : if not wentx[j] : for idx in range(gsz[j]) : e = G.g[j][idx] if e["cap"] >= 0 and e["to"] != S and not wenty[e["to"] - n] and matchx[j] != e["to"] - n : G.g[j][idx]["cap"] = 0 G.g[e["to"]][e["rev"]]["cap"] = 0 psz = len(previous_edges) j = 0 while j < psz : idx = previous_edges[j] if not wentx[x[idx]] and not wenty[y[idx]] and matchx[x[idx]] != y[idx] : psz -= 1 previous_edges[j], previous_edges[psz] = previous_edges[psz], previous_edges[j] previous_edges.pop() j -= 1 j += 1 ans = 0 for i in range(0, k + 1) : ans += val[i] << i print(ans)