#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; 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; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; const ll inf = 1e18; using T = tuple; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n, m, k, t; cin >> n >> m >> k >> t; vector a(k), b(k), c(k), D(k); auto idx = vec2d(n, m, -1); for(int i = 0; i < k; i++){ cin >> a[i] >> b[i] >> c[i] >> D[i]; a[i]--; b[i]--; idx[a[i]][b[i]] = i; } auto dp = vec3d(n, m, n+m+1, inf); priority_queue, greater> que; auto set_dist = [&](int i, int j, int cnt, ll d){ if(chmin(dp[i][j][cnt], d)){ que.push({d, i, j, cnt}); } }; auto in_field =[&](int i, int j){ return 0 <= i && i < n && 0 <= j && j < m; }; set_dist(0, 0, 0, 0); while(!que.empty()){ auto [d, i, j, cnt] = que.top(); que.pop(); if(dp[i][j][cnt] != d) continue; for(int k = 0; k < 4; k++){ int ii = i+dx[k]; int jj = j+dy[k]; if(!in_field(ii, jj)) continue; set_dist(ii, jj, cnt, d); } if(idx[i][j] != -1){ int cc = cnt+c[idx[i][j]]-1; chmin(cc, n+m); set_dist(i, j, cc, d+D[idx[i][j]]); } } ll ans = inf; for(int cnt = 0; cnt <= n+m; cnt++){ if(n+m-2-cnt <= t){ chmin(ans, dp[n-1][m-1][cnt]); } } if(ans > inf/2){ cout << -1 << endl; }else{ cout << ans << endl; } }