#include template T in() { abort(); return T(); } template<> std::string in() { std::string str; std::cin >> str; return str; } template<> int in() { int x; scanf("%d", &x); return x; } template void out(T x) { abort(); } template<> void out(const char* x) { printf("%s\n", x); } template<> void out(std::string x) { std::cout << x << std::endl; } template<> void out(int x) { printf("%d\n", x); } template<> void out(long x) { printf("%ld\n", x); } class Edge { public: Edge() {} Edge(int to_, int time_) : to(to_), time(time_) {} int to; int time; }; std::vector< Edge > edges[64 * 512]; int buf[64 * 512]; int xs[64 * 512]; int buf2[64 * 512]; inline int nodeId(int id, int cost) { int res = (id << 9) | cost; assert( 0 <= res and res < 64 * 512 ); return res; } constexpr int inf = (1 << 28); int main() { for(int i = 0; i < 64 * 512; ++i) { xs[i] = inf; } for(int i = 0; i < 512; ++i) { xs[nodeId(1, i)] = 0; } auto n = in(); auto c = in(); auto v = in(); auto ss = std::vector(v, 0); for(auto& x : ss) x = in(); auto ts = std::vector(v, 0); for(auto& x : ts) x = in(); auto ys = std::vector(v, 0); for(auto& x : ys) x = in(); auto ms = std::vector(v, 0); for(auto& x : ms) x = in(); for(int i = 0; i < v; ++i) { int from = ss[i]; int to = ts[i]; int cost = ys[i]; int time = ms[i]; for(int j = 0; j <= c; ++j) { int nc = j + cost; if( c < nc ) break; edges[nodeId(from, j)].push_back( Edge(nodeId(to, nc), time) ); } } std::priority_queue< std::pair > q; for(int i = 0; i <= c; ++i) { q.push(std::pair(0, nodeId(1, i))); } while( not q.empty() ) { int id, time; std::tie(time, id) = q.top(); q.pop(); time = -time; for(int i = 0; i < (int)edges[id].size(); ++i) { Edge& edge = edges[id][i]; int to = edge.to; int ntime = time + edge.time; if( xs[to] <= ntime ) continue; xs[to] = ntime; q.push( std::pair(-ntime, to) ); } } int res = inf; for(int i = 0; i <= c; ++i) { res = std::min(res, xs[nodeId(n, i)]); } printf("%d\n", res == inf ? -1 : res); return 0; }