#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7, M = 5e1 + 1, OO = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, m, c0, d0, dp[N][11];
int a[N], b[N], c[N], d[N];
int main() {
  cin >> n >> m >> c[0] >> d[0];
  for (int i = 1; i <= m; ++i) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  for (int i = 0; i <= n; ++i) dp[i][0] = (i / c[0]) * d[0];
  for (int i = 0; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      for (int k = 0; k <= a[j] / b[j]; ++k) {
        ;
        if (i - c[j] * k >= 0)
          dp[i][j] = max(dp[i][j], dp[i - c[j] * k][j - 1] + d[j] * k);
      }
    }
  }
  cout << dp[n][m];
  return 0;
}
