#include <bits/stdc++.h>
using namespace std;
const int MAX = 1005, MAXM = 13;
int main() {
  int N, M, a[MAXM], b[MAXM], c[MAXM], d[MAXM], e[MAXM];
  a[0] = 1, b[0] = 0;
  cin >> N >> M >> c[0] >> d[0];
  e[0] = MAX;
  for (int i = 1; i <= M; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    e[i] = (int)(a[i] / b[i]);
  }
  vector<int> dp(MAX, 0);
  for (int i = 0; i <= M; i++) {
    for (int j = N; j >= 0; j--) {
      for (int k = 1; k <= e[i]; k++) {
        if (j - k * c[i] < 0) break;
        dp[j] = max(dp[j], d[i] * k + dp[j - k * c[i]]);
      }
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
}
