#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #define rep(i,n) for(int i=0;i<(n);i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) ((int)(x).size()) #define pb push_back using ll = long long; using namespace std; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b dy = {-1,0,1,0}; vector dx = {0,-1,0,1}; int main(){ int N,M,K,T; cin >> N >> M >> K >> T; vector>> A(N,vector>(M,{0,0})); rep(i,K){ int a,b,c,d; cin >> a >> b >> c >> d; a--; b--; if(c!=1) A[a][b] = {c,d}; } if(N+M-2<=T){ cout << "0\n"; return 0; } vector D(N,vector(M,vector(200,1LL<<60))); vector used(N,vector(M,vector(200,false))); ll ans = 1LL<<60; rep(i,200) chmin(ans,D[N-1][M-1][i]); priority_queue>,vector>>,greater>>> q; q.push({0LL,{0,0,T}}); while(!q.empty()){ ll d = q.top().first; vector tv = q.top().second; q.pop(); int y = tv[0], x = tv[1], l = tv[2]; if(used[y][x][l]) continue; used[y][x][l] = true; D[y][x][l] = d; if(l!=0){ rep(i,4){ int ny = y + dy[i]; int nx = x + dx[i]; if(!(0<=ny&&ny({0,0})){ if(D[y][x][min(199,l-1+A[y][x].first)] <= d+A[y][x].second) continue; q.push({d+A[y][x].second,{y,x,min(199,l-1+A[y][x].first)}}); } } rep(i,200) chmin(ans,D[N-1][M-1][i]); if(ans>=(1LL<<60)) ans = -1; cout << ans << '\n'; return 0; };