#include <bits/stdc++.h>
using namespace std;
int n, m, c0, d0;
int dp[2000];
int a[2000], b[2000], c[2000], d[2000];
int main() {
  int i, j, k;
  cin >> n >> m >> c0 >> d0;
  for (i = 0; i < m; i++) cin >> a[i] >> b[i] >> c[i] >> d[i];
  for (i = 0; i < n / c0; i++)
    for (j = n; j >= c0; j--) {
      dp[j] = max(dp[j], dp[j - c0] + d0);
    }
  for (i = 0; i < m; i++)
    for (j = 0; j < a[i] / b[i]; j++) {
      for (k = n; k >= c[i]; k--) {
        dp[k] = max(dp[k], dp[k - c[i]] + d[i]);
      }
    }
  cout << dp[n] << endl;
  return 0;
}
