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