#include using namespace std; #define rep(i,a,b) for(int i=a;i> que; que.push({0, 0}); dst[0][0] = 0; while (!que.empty()) { auto q = que.top(); que.pop(); int x = q.second % 1010; int y = q.second / 1010; int c = -q.first; if (y == GY && x == GX) return c; if (vis[y][x]) continue; vis[y][x] = true; rep(i, 0, N) { int yy = y + Y[i]; int xx = x + X[i]; if (GY < yy) continue; if (GX < xx) continue; if (vis[yy][xx]) continue; int nc = c + C[i]; if (nc < dst[yy][xx]) { dst[yy][xx] = nc; que.push({ -nc, yy * 1010 + xx }); } } } return -1; } //----------------------------------------------------------------------------------- int main() { cin >> GX >> GY >> N >> F; rep(i, 0, N) cin >> X[i] >> Y[i] >> C[i]; X[N] = 0; Y[N] = 1; C[N] = F; N++; X[N] = 1; Y[N] = 0; C[N] = F; N++; cout << sol() << endl; }