#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
int n, m, x, y;
int a[maxn], b[maxn], c[maxn], d[maxn];
int dp[maxn];
int main() {
  cin >> n >> m >> x >> y;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    b[i] = a[i] / b[i];
  }
  c[m + 1] = x, d[m + 1] = y;
  b[m + 1] = 1000;
  for (int i = 1; i <= m + 1; i++) {
    for (int j = 1; j <= b[i]; j++) {
      for (int k = n; k >= c[i]; k--) {
        if (k >= c[i]) dp[k] = max(dp[k], dp[k - c[i]] + d[i]);
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}
