#include <bits/stdc++.h>
using namespace std;
int dp[12000];
int num[200];
int gram[200];
int value[200];
int n, m;
inline int max(int a, int b) { return a > b ? a : b; }
int main() {
  int a, b;
  int x, y, z, w;
  scanf("%d%d", &n, &m);
  scanf("%d%d", &a, &b);
  for (register int i = 1; i <= m; i++) {
    scanf("%d%d%d%d", &x, &y, &z, &w);
    num[i] = x / y;
    gram[i] = z;
    value[i] = w;
  }
  gram[0] = a;
  value[0] = b;
  num[0] = n / a;
  memset(dp, 0, sizeof(dp));
  for (register int i = 0; i <= m; i++)
    for (register int j = 1; j <= num[i]; j++)
      for (register int k = n; k >= gram[i]; k--)
        dp[k] = max(dp[k], dp[k - gram[i]] + value[i]);
  printf("%d\n", dp[n]);
  return 0;
}
