package yukicoder; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Shortcut { private static BufferedReader br; public static void main(String[] args) throws IOException{ br = new BufferedReader(new InputStreamReader(System.in)); int town = Integer.parseInt(br.readLine()); int money = Integer.parseInt(br.readLine()); int road = Integer.parseInt(br.readLine()); road++; int[] s = new int[road]; int[] t = new int[road]; int[] y = new int[road]; int[] m = new int[road]; arrayInit(s); arrayInit(t); arrayInit(y); arrayInit(m); //Library.disp(Library.cast(m)); int dp[][] = new int[3][road]; // cost time while(true){ for(int i = 1 ; i < road; i++){ dp[0][i] = 0; dp[1][i] = 0; dp[2][i] = -1; } for(int i = 1 ; i < road ; i++){ int from = s[i]; if(from == -1){ continue; } int to = t[i]; int fCost = dp[0][from]; int fTime = dp[1][from]; int tTime = dp[1][to]; int totalCost = y[i] + fCost; int totalTime = m[i] + fTime; if((tTime == 0 || totalTime < tTime) && totalCost <= money ){ dp[0][to] = totalCost; dp[1][to] = totalTime; dp[2][to] = i; } } int time = dp[1][road-1]; if(time == 0){ int max = -1; for(int i = 1 ; i < road ; i++){ int from = dp[2][i]; if(max < from){ max = from; } } if(max == -1){ break; } s[max] = -1; continue; } break; } int time = dp[1][road-1]; int disp = time == 0 ? -1 : time; System.out.println(disp); } private static void arrayInit(int[] array) throws IOException{ String[] spl = br.readLine().split(" "); for(int i = 1 ; i < array.length ; i++){ array[i] = Integer.parseInt(spl[i-1]); } } }