#include using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") typedef int ll; const ll INF = 1e9; const ll MOD = 1e9 + 7; const ll MAXN = 7e3 + 5; const ll LOG = 31; #define vll vector #define pll pair #define fi first #define se second #define endl '\n' ll n, m, k, t; pll a [MAXN], b [MAXN]; ll dp [MAXN][350]; void solve(){ cin >> n >> m >> k >> t; for(ll i = 1; i <= k; i++){ cin >> a[i].fi >> a[i].se >> b[i].fi >> b[i].se; } ll tt = n+m-2; t = min(t, tt); a[0] = {1, 1}; a[k+1] = {n, m}; for(ll i = 0; i <= k+1; i++){ for(ll j = 0; j <= 2*tt; j++){ if(i == 0){ if(j < tt) dp[i][j] = INF; continue; } dp[i][j] = INF; for(ll u = 0; u < i; u++){ ll dis = abs(a[u].fi - a[i].fi) + abs(a[u].se - a[i].se); // cout << i << " " << u << " << " << dis << endl; if(dis <= j) dp[i][j] = min(dp[i][j], dp[u][j-dis]); } } for(ll j = 2*tt; j >= 0; j--){ if(i == 0 || i == k+1) break; ll cnt = j+b[i].fi-1; dp[i][j] = min(dp[i][j], dp[i][min(2*tt, cnt)] + b[i].se); } // for(ll j = 0; j <= 2*tt; j++){ // cout << dp[i][j] << " "; // }cout << endl; } ll ans = dp[k+1][tt+t]; if(ans == INF) cout << -1 << endl; else cout << ans << endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // ll t; cin >> t; // while(t--){ solve(); // } }