#include using namespace std; #define rep(i,a,b) for(int i=a;i> n; int c; cin >> c; int v; cin >> v; vector< vector> > edges(n); int data[4][v]; rep(i,0,4){ rep(j,0,v){ cin >> data[i][j]; } } rep(i,0,v){ vector tmp = {data[1][i]-1, data[2][i], data[3][i]}; edges[data[0][i]-1].push_back(tmp); } int dp[c+1][n]; rep(i,0,c+1) rep(j,0,n) dp[i][j] = inf; dp[0][0] = 0; rep(i,0,n){ rep(j,0,c+1){ if(dp[j][i] > -1){ rep(k,0,edges[i].size()){ if(j + edges[i][k][1] < c+1){ dp[j + edges[i][k][1]][edges[i][k][0]] = min(dp[j+edges[i][k][1]][edges[i][k][0]], dp[j][i] + edges[i][k][2]); } } } } } int ans = inf; rep(i,0,c+1){ if(dp[i][n-1] > -1){ ans = min(ans, dp[i][n-1]); } } cout << ((ans == inf) ? -1 : ans) << endl; }