#include using namespace std; #define int long long #define ll long long #define pii pair #define pb push_back #define mp make_pair #define dbg puts("dbg"); #define outp(x) cout << #x << " = " << (x) << endl #define all(x) (x).begin(), (x).end() #define test int t; cin >> t; while(t--){solve();} #define MOD 998244353 int fpb(int a,int b){if(b == 0){return a;}return fpb(b, a%b);} int kpk(int a,int b){return (a*b)/fpb(a, b);} int n, m, k, t; map dp[82][82]; vector >> v; int f(int baris, int kolom, int curWaktu){ if(curWaktu > t) return 2e18; if(baris == n && kolom == m) return 0; if(dp[baris][kolom].count(curWaktu)) return dp[baris][kolom][curWaktu]; int pilihan1 = f(n, m, curWaktu+n+m-baris-kolom); int mini = pilihan1; if(curWaktu+n+m-baris-kolom <= t){ mini = pilihan1; } else { int pilihan2; for(auto i : v){ int barisNext = i.first, kolomNext = i.second.first; int waktuMundur = i.second.second.first, lelah = i.second.second.second; pilihan2 = f(barisNext, kolomNext, curWaktu+barisNext+kolomNext-baris-kolom+1-waktuMundur)+lelah; mini = min(mini, pilihan2); } } dp[baris][kolom][curWaktu] = mini; return mini; } void solve(){ cin >> n >> m >> k >> t; while(k--){ int a, b, c, d; cin >> a >> b >> c >> d; v.pb({a, {b, {c, d}}}); } int res = f(1, 1, 0); cout << ((res != 2e18)?res:-1) << endl; } int32_t main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); solve(); return 0; }