#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n, m;
int a, b, w[maxn], v[maxn];
int num[maxn];
int dp[20005];
int main(void) {
  cin >> n >> m >> w[0] >> v[0];
  num[0] = n / w[0];
  for (int i = 1; i <= m; ++i) {
    cin >> a >> b >> w[i] >> v[i];
    num[i] = min(a / b, n / w[i]);
  }
  for (int i = 0; i <= m; ++i) {
    for (int j = n; j > 0; --j) {
      for (int k = 0; k <= num[i]; ++k) {
        if (j - k * w[i] >= 0) {
          dp[j] = max(dp[j], dp[j - k * w[i]] + k * v[i]);
        }
      }
    }
  }
  int maxv = 0;
  for (auto i : dp) {
    maxv = max(i, maxv);
  }
  cout << maxv << endl;
  return 0;
}
