#include <bits/stdc++.h>
using namespace std;
const long long M = 1000001;
long long m, n, be[1001][11], a[11], b[11], c[11], d[11], c0, d0, mx;
long long fu(long long rest, long long pointer) {
  if (pointer == m) {
    return d[pointer] * min(rest / c[pointer], a[pointer] / b[pointer]);
  }
  if (be[rest][pointer]) return be[rest][pointer];
  long long ma = 0;
  for (long long i = 0; i <= min(rest / c[pointer], a[pointer] / b[pointer]);
       i++) {
    ma = max(ma, i * d[pointer] + fu(rest - i * c[pointer], pointer + 1));
  }
  if (ma == 0) {
    return 0;
  }
  be[rest][pointer] = ma;
  return ma;
}
int main() {
  cin >> n >> m >> c0 >> d0;
  for (long long i = 1; i <= m; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  for (long long i = 0; i <= n / c0; i++) {
    mx = max(mx, i * d0 + fu((n - i * c0), 1));
  }
  cout << mx;
  return 0;
}
