#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  while (t--) {
    long long int n, m, c0, d0, ans = 0;
    cin >> n >> m >> c0 >> d0;
    long long int a[m + 1], b[m + 1], c[m + 1], d[m + 1];
    for (long long int i = 1; i <= m; i++) {
      cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    long long int dp[m + 1][n + 1];
    memset(dp, 0, sizeof(dp));
    for (long long int i = 1; i <= m; i++) {
      for (long long int j = 1; j <= n; j++) {
        dp[i][j] = dp[i - 1][j];
        for (long long int k = 1; k <= j / c[i] && k <= a[i] / b[i]; k++) {
          dp[i][j] = max(dp[i - 1][j - k * c[i]] + k * d[i], dp[i][j]);
        }
      }
    }
    for (long long int i = 0; i < n / c0 + 1; i++) {
      if (i * c0 <= n) {
        ans = max(d0 * i + dp[m][n - i * c0], ans);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
