#include <bits/stdc++.h>
using namespace std;
int dp[1005][15];
int main() {
  int m, n;
  scanf("%d", &n);
  scanf("%d", &m);
  int d[m + 1], c[m + 1], a[m + 1], b[m + 1];
  int i, j, pos;
  scanf("%d", &c[1]);
  scanf("%d", &d[1]);
  for (i = 2; i <= m + 1; i++) {
    scanf("%d", &a[i]);
    scanf("%d", &b[i]);
    scanf("%d", &c[i]);
    scanf("%d", &d[i]);
  }
  for (i = 0; i <= n; i++) {
    for (j = 1; j <= m + 1; j++) {
      if (j == 1 && i >= c[j]) {
        dp[i][j] = dp[i - c[j]][j] + d[j];
      } else {
        dp[i][j] = dp[i][j - 1];
        int k = 1;
        while (i - c[j] * k >= 0 && k <= (a[j] / b[j])) {
          dp[i][j] = max(d[j] * k + dp[i - c[j] * k][j - 1], dp[i][j]);
          k++;
        }
      }
    }
  }
  cout << dp[n][m + 1] << endl;
}
