#include <bits/stdc++.h>
using namespace std;
int n, m, c0, d0, a[11][1009], b[11], c[11], d[11], maxx;
int r[1009];
int main() {
  cin >> n >> m >> c0 >> d0;
  for (int i = 0; i < m; ++i) cin >> a[i][0] >> b[i] >> c[i] >> d[i];
  memset(r, 0, sizeof r);
  for (int i = 0; i < n + 1; ++i) {
    int x = -1;
    maxx = 0;
    for (int j = 0; j < m; ++j) {
      if (a[j][i - c[j]] - b[j] >= 0 && i - c[j] >= 0 &&
          r[i - c[j]] + d[j] > maxx) {
        maxx = r[i - c[j]] + d[j];
        x = j;
      }
    }
    if (i - c0 >= 0 && r[i - c0] + d0 > maxx) {
      maxx = r[i - c0] + d0;
      x = -1;
    }
    if (x == -1)
      r[i] = maxx;
    else {
      r[i] = maxx;
      for (int j = 0; j < m; ++j) a[j][i] = a[j][i - c[x]];
      a[x][i] = a[x][i - c[x]] - b[x];
    }
  }
  int maxx = 0;
  for (int i = 0; i < n + 1; ++i) maxx = max(maxx, r[i]);
  cout << maxx;
  return 0;
}
