// Source: https://usaco.guide/general/io #include using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int h, w, k, t;cin >> h >> w >> k >> t; if(t >= h + w - 2){ cout << 0 << endl; return 0; } vector> C(h, vector(w)), D(h, vector(w)); for(int i = 0; i < k; i++){ int y, x, v1, v2; cin >> y >> x >> v1 >> v2; y--, x--; C[y][x] = v1; D[y][x] = v2; } int temp = h + w - 2 - t; const ll INF = 1ll << 20; vector>> v(temp, vector>(h, vector(w, INF))); priority_queue> pq; v[0][0][0] = 0; pq.emplace(0, 0, 0, 0); ll ans = INF; while(!pq.empty()){ ll tim, d; int y, x; tie(d, tim, y, x) = pq.top(); pq.pop(); if(d > v[tim][y][x]) continue; if(C[y][x] >= 2){ ll nt = tim + C[y][x] - 1; if(nt < temp){ if(d + D[y][x] < v[nt][y][x]){ v[nt][y][x] = d + D[y][x]; pq.emplace(v[nt][y][x], nt, y, x); } } else{ ans = min(ans, d + D[y][x]); } } for(int i = 0 ; i < 4 ; i++){ int yy = y + (i == 0) - (i == 1); int xx = x + (i == 2) - (i == 3); if(yy < 0 || xx < 0 || yy >= h || xx >= w) continue; if(yy < y || xx < x){ ll nt = tim - 2; if(nt < 0) continue; if(d >= v[nt][yy][xx]) continue; v[nt][yy][xx] = d; pq.emplace(d, nt, yy, xx); }else{ if(d >= v[tim][yy][xx]) continue; v[tim][yy][xx] = d; pq.emplace(d, tim, yy, xx); } } } if(ans == INF) ans = -1; cout << ans << endl; }