#include <bits/stdc++.h>
using namespace std;
int dp[11][1001], a[11], b[11], c[11], d[11], n, m;
int main() {
  a[0] = 100000, b[0] = 0;
  scanf("%d %d %d %d", &n, &m, &c[0], &d[0]);
  for (int i = 1; i <= m; i++) scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
  for (int i = 0; i <= m; i++) dp[i][0] = 0;
  for (int j = 0; j <= n; j++) dp[0][j] = (j / c[0]) * d[0];
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = dp[i - 1][j];
      for (int k = 1; k * c[i] <= j && k * b[i] <= a[i]; k++) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - k * c[i]] + k * d[i]);
      }
    }
  }
  printf("%d", dp[m][n]);
  return 0;
}
