#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;
int n, m, c[N], d[N], a[N], b[N], dp[N][N], ans;
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 = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int mx = -inf;
      for (int k = 0; k <= a[j] / b[j]; k++) {
        if (i - c[j] * k < 0) continue;
        mx = max(mx, dp[i - c[j] * k][j - 1] + d[j] * k);
      }
      dp[i][j] = mx;
    }
  }
  for (int i = 0; i <= n; i++) ans = max(ans, dp[i][m] + (n - i) / c[0] * d[0]);
  cout << ans << endl;
  return 0;
}
