#include using namespace std; const long long INF = 1000000000000000; vector dx = {1, 0, -1, 0}; vector dy = {0, 1, 0, -1}; int main(){ int N, M, K, T; cin >> N >> M >> K >> T; vector A(K), B(K), C(K), D(K); for (int i = 0; i < K; i++){ cin >> A[i] >> B[i] >> C[i] >> D[i]; A[i]--; B[i]--; } if (T >= N + M - 2){ cout << 0 << endl; } else { vector> id(N, vector(M, -1)); for (int i = 0; i < K; i++){ id[A[i]][B[i]] = i; } int T2 = (N + M) * 2 + 1; vector>> d(T2, vector>(N, vector(M, INF))); priority_queue, vector>, greater>> pq; pq.push(make_tuple(0, N + M, 0, 0)); while (!pq.empty()){ long long c = get<0>(pq.top()); int t = get<1>(pq.top()); int x = get<2>(pq.top()); int y = get<3>(pq.top()); pq.pop(); if (d[t][x][y] == INF){ d[t][x][y] = c; if (id[x][y] != -1){ int t2 = max(t - C[id[x][y]] + 1, 0); if (d[t2][x][y] == INF){ pq.push(make_tuple(c + D[id[x][y]], t2, x, y)); } } if (t < T2 - 1){ for (int i = 0; i < 4; i++){ int x2 = x + dx[i]; int y2 = y + dy[i]; if (0 <= x2 && x2 < N && 0 <= y2 && y2 < M){ if (d[t + 1][x2][y2] == INF){ pq.push(make_tuple(c, t + 1, x2, y2)); } } } } } } long long ans = INF; for (int i = -(N + M); i <= T; i++){ ans = min(ans, d[N + M + i][N - 1][M - 1]); } if (ans == INF){ cout << -1 << endl; } else { cout << ans << endl; } } }