#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, c0, d0;
  cin >> n >> m >> c0 >> d0;
  map<int, int> mm;
  for (int i = 0; i * c0 <= n; i++) {
    mm[n - i * c0] = d0 * i;
  }
  for (int i = 0; i < m; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    map<int, int> mm2;
    map<int, int>::iterator it = mm.begin();
    for (; it != mm.end(); ++it) {
      int rest = it->first, earn = it->second;
      for (int j = 0; j * c <= rest && j * b <= a; j++) {
        if (mm2.count(rest - j * c)) {
          mm2[rest - j * c] = max(d * j + earn, mm2[rest - j * c]);
        } else {
          mm2[rest - j * c] = d * j + earn;
        }
      }
    }
    mm.swap(mm2);
  }
  int ans = 0;
  map<int, int>::iterator it = mm.begin();
  for (; it != mm.end(); ++it) {
    if (ans < it->second) ans = it->second;
  }
  cout << ans << endl;
  return 0;
}
