#include <bits/stdc++.h>
inline int getx() {
  char c;
  int x;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    ;
  for (x = 0; c >= '0' && c <= '9'; c = getchar())
    x = (x << 3) + (x << 1) + c - '0';
  return x;
}
bool upmax(int &a, const int &b) { return a < b ? a = b, 1 : 0; }
const int MAX_N = 10050;
int n, m;
int v[MAX_N], c[MAX_N];
int f[MAX_N];
int main() {
  n = getx(), m = getx();
  int c0 = getx(), d0 = getx();
  for (int i = c0; i <= n; ++i) upmax(f[i], f[i - c0] + d0);
  for (int i = 1; i <= m; ++i) {
    int a = getx(), b = getx(), c = getx(), d = getx();
    for (int p = b; p <= a; p += b) {
      for (int j = n; j >= c; --j) upmax(f[j], f[j - c] + d);
    }
  }
  printf("%d\n", f[n]);
}
