#include <bits/stdc++.h>
using namespace std;
bool comp(pair<double, long long> a, pair<double, long long> b) {
  return a.first > b.first;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long i, j;
  long long ans = 0;
  long long n, m;
  long long inf = 10e9;
  cin >> n >> m;
  long long arr[m + 2][4];
  long long q[m + 2];
  q[1] = inf;
  arr[1][0] = 1000;
  arr[1][1] = 0;
  cin >> arr[1][2] >> arr[1][3];
  long long dp[m + 2][n + 1];
  memset(dp, 0, sizeof(dp));
  for (i = 2; i <= m + 1; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2] >> arr[i][3];
    q[i] = arr[i][0] / arr[i][1];
  }
  for (i = 1; i <= m + 1; i++) {
    for (int s = 0; s <= n; s++) {
      dp[i][s] = dp[i - 1][s];
      for (int j = 1; j <= q[i] && j * arr[i][2] <= s; j++)
        dp[i][s] =
            max(dp[i][s], dp[i - 1][s - (j * arr[i][2])] + (j * arr[i][3]));
      ans = max(ans, dp[i][s]);
    }
  }
  cout << ans;
  return 0;
}
