#include <bits/stdc++.h>
using namespace std;
long long dp[50][1010], w[100], v[100], n, m, ans, g[100], a, b, c, d;
int main() {
  cin >> n >> m >> c >> d;
  w[1] = c;
  v[1] = d;
  g[1] = n / c;
  for (int i = 2; i <= m + 1; i++) {
    cin >> a >> b >> c >> d;
    w[i] = c;
    v[i] = d;
    g[i] = a / b;
  }
  for (int i = 1; i <= m + 1; i++)
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= g[i] && k * w[i] <= j; k++) {
        dp[i][j] = max(dp[i - 1][j - k * w[i]] + k * v[i], dp[i][j]);
      }
    }
  for (int i = 0; i <= n; i++) ans = max(ans, dp[m + 1][i]);
  cout << ans << endl;
}
