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