class Heap_Point: def __init__(self,x,d): self.d=d self.x=x def __str__(self): return "(point:{}, dist:{})".format(self.x,self.d) def __repr__(self): return str(self) def __lt__(self,other): return self.d<other.d def __iter__(self): yield from (self.x,self.d) #================================================ from heapq import heappop,heappush Gx,Gy,N,F=map(int,input().split()) P=["*"] for _ in range(N): x,y,c=map(int,input().split()) P.append((x,y,c)) inf=float("inf") T=[[[inf]*(N+1) for _ in range(Gy+1)] for __ in range(Gx+1)] T[0][0][0]=0 Q=[Heap_Point((0,0,0),0)] while Q: p,d=heappop(Q) x,y,k=p if x==Gx and y==Gy: break if T[x][y][k]<d: continue if x<Gx and T[x+1][y][k]>d+F: T[x+1][y][k]=d+F heappush(Q,Heap_Point((x+1,y,k),d+F)) if y<Gy and T[x][y+1][k]>d+F: T[x][y+1][k]=d+F heappush(Q,Heap_Point((x,y+1,k),d+F)) for i,q in enumerate(P[k+1:],k+1): a,b,c=q if 0<=x+a<=Gx and 0<=y+b<=Gy and T[x+a][y+b][i]>d+c: T[x+a][y+b][i]=d+c heappush(Q,Heap_Point((x+a,y+b,i),d+c)) print(min(T[Gx][Gy]))