#include #include #include using namespace std; using namespace atcoder; using mint = modint1000000007; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 long long dis[325][80][80]; int cs[80][80],ds[80][80]; int main(){ int N,M,K,T; cin>>N>>M>>K>>T; T = min(T,160); rep(i,325){ rep(j,N){ rep(k,M)dis[i][j][k] = Inf64; } } rep(i,K){ int a,b,c,d; cin>>a>>b>>c>>d; a--,b--; cs[a][b] = c; ds[a][b] = d; } int cen = 160; dis[cen][0][0] = 0; priority_queue>,vector>>,greater>>> Q; Q.emplace(0LL,vector({cen,0,0})); vector dx = {1,-1,0,0},dy = {0,0,1,-1}; while(Q.size()>0){ long long D = Q.top().first; int a = Q.top().second[0]; int x = Q.top().second[1]; int y = Q.top().second[2]; Q.pop(); if(dis[a][x][y]!=D)continue; rep(i,4){ int aa = a+1; int xx = x+dx[i]; int yy = y+dy[i]; if(xx>=N||yy>=M||xx<0||yy<0||aa>=325||aa<0)continue; if(dis[aa][xx][yy]>D){ dis[aa][xx][yy] = D; Q.emplace(D,vector({aa,xx,yy})); } } if(cs[x][y]!=0){ long long DD = D + ds[x][y]; //cout<DD){ dis[aa][xx][yy] = DD; Q.emplace(DD,vector({aa,xx,yy})); } } } long long ans = Inf64; rep(i,cen+T+1)ans = min(ans,dis[i][N-1][M-1]); if(ans==Inf64)ans = -1; cout<