#include #include using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i; using vvi = vector>; template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline void drop(T x) { cout << x << endl; exit(0); } template void vout(vector v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; int main() { lint N, M, K, T; cin >> N >> M >> K >> T; vi A(K), B(K), C(K), D(K); rep(i, K) cin >> A[i] >> B[i] >> C[i] >> D[i]; lint a=0, b=0, c=0, x, y, z; std::vector dp(N+1, vvi(M+1, vi(400, LINF))); dp[1][1][200] = 0; repx(i, 1, N+1) { repx(j, 1, M+1) { repx(k, 1, 400) { chmin(dp[i][j][k], dp[i][j-1][k-1]); chmin(dp[i][j][k], dp[i-1][j][k-1]); } rep(k, K) { if(A[k] == i && B[k] == j && C[k] != 1) { rep(l, 400) { if (dp[i][j][l] == LINF) continue; x = l, y = D[k]; while (x-C[k] > 0) { x -= C[k]-1; chmin(dp[i][j][x], dp[i][j][l]+y); y += D[k]; } chmin(dp[i][j][0], dp[i][j][l]+y); } } } } } a = LINF; rep(i, min((lint)400, 200+T+1)) { chmin(a, dp[N][M][i]); } if (a == LINF) std::cout << "-1" << '\n'; else std::cout << a << '\n'; }