#include using namespace std; using ll = long long; ll H, W, T; vector>> field(80, vector>(80, {0, 0})); template using pq = priority_queue, greater>; void solve(){ ll h, w, nh, nw, alt; ll x, t, c, d; //x...疲労度、t...時間 pq> que; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; vector>> dist(H, vector(W, vector(321, 9e18))); //-160~160 vector>> visit(H, vector(W, vector(321))); dist[0][0][160] = 0; que.push({0, 0, 0, 160}); while(!que.empty()){ tie(x, h, w, t) = que.top(); que.pop(); if (visit[h][w][t]) continue; if (t == 320) continue; visit[h][w][t] = 1; for (int dir=0; dir < 4; dir++){ nh = h + dx[dir]; nw = w + dy[dir]; if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (x < dist[nh][nw][t+1]){ dist[nh][nw][t+1] = x; que.push({dist[nh][nw][t+1], nh, nw, t+1}); } } if (field[h][w].first > 0){ tie(c, d) = field[h][w]; if (t-c >= 0 && x+d < dist[h][w][t-c]){ dist[h][w][t-c] = x+d; que.push({dist[h][w][t-c], h, w, t-c}); } } } ll mi=9e18; for (int i=0; i<=min(T+160, 320LL); i++){ mi = min(mi, dist[H-1][W-1][i]); } cout << (mi == 9e18 ? -1 : mi) << endl; } int main(){ ll K, a, b, c, d; cin >> H >> W >> K >> T; for (int i=0; i> a >> b >> c >> d; a--; b--; c--; field[a][b] = {c, d}; } solve(); return 0; }