#include using namespace std; struct Edge { int to, cost, time; }; int main() { int N, C, V; cin >> N >> C >> V; vector> graph(N); for (int i = 0; i < V; i++) { int s, t, y, m; cin >> s >> t >> y >> m; s--, t--; graph[s].push_back({t, y, m}); } vector dp(C+1, INT_MAX); dp[0] = 0; priority_queue, vector>, greater<>> pq; pq.push({0, 0}); while (!pq.empty()) { auto [curr_time, curr_cost] = pq.top(); pq.pop(); if (curr_time > dp[curr_cost]) continue; for (const auto& [to, cost, time] : graph[0]) { int new_cost = curr_cost + cost; int new_time = curr_time + time; if (new_cost <= C && new_time < dp[new_cost]) { dp[new_cost] = new_time; pq.push({new_time, new_cost}); } } } int ans = *min_element(dp.begin(), dp.end()); cout << (ans == INT_MAX ? -1 : ans) << endl; return 0; }