#include <bits/stdc++.h>
using namespace std;
template <typename T, size_t N>
int SIZE(const T (&t)[N]) {
  return N;
}
template <typename T>
int SIZE(const T &t) {
  return t.size();
}
string to_string(const string s, int x1 = 0, int x2 = 1e9) {
  return x1 < int(s.size()) ? s.substr(x1, x2 - x1 + 1) : "";
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(const bool b) { return b ? "true" : "false"; }
string to_string(const char c) { return string({c}); }
template <size_t N>
string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) {
  string t = "";
  for (int s = min(x1, SIZE(b)), e = min(x2, SIZE(b) - 1); s <= e; ++s) {
    t += b[s] + '0';
  }
  return t;
}
template <typename T, typename... E>
string to_string(const T(&v), int x1 = 0, int x2 = 1e9, E... rem);
template <typename T, typename E>
string to_string(const pair<T, E> &p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename T, typename... E>
string to_string(const T(&v), int x1, int x2, E... rem) {
  string res = "[";
  bool first = true;
  x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
  auto l = begin(v);
  advance(l, x1);
  auto r = l;
  advance(r, (x2 - x1) + (x2 < SIZE(v)));
  for (auto e = l; e != r; e = next(e), first = false) {
    if (!first) {
      res += ", ";
    }
    res += to_string(*e, rem...);
  }
  res += "]";
  return res;
}
void dbgm() { ; }
template <typename T, typename... E>
void dbgm(T t, E... e) {
  cout << to_string(t) << ", ";
  dbgm(e...);
}
using state = pair<int, int>;
struct cmp_state {
  bool operator()(const state &lhs, const state &rhs) const {
    if (lhs.first != rhs.first) {
      return lhs.first < rhs.first;
    }
    return rhs.second < lhs.second;
  }
};
int m, c0, d0;
vector<int> a(10), b(10), c(10), d(10);
map<state, int, cmp_state> mp;
int dfs(int n, int i) {
  if (n < 0) return -1000000000 / 3;
  for (int x : a)
    if (x < 0) return -1000000000 / 3;
  if (i >= m) return 0;
  if (mp.find({n, i}) != mp.end()) {
    return mp[{n, i}];
  }
  int rs = 0;
  for (int j = 0; j * c[i] <= n && j * b[i] <= a[i]; j++) {
    a[i] -= j * b[i];
    rs = max(rs, dfs(n - j * c[i], i + 1) + d[i] * j);
    a[i] += j * b[i];
  }
  mp[{n, i}] = rs;
  return rs;
}
int main() {
  int n;
  scanf("%d %d %d %d", &n, &m, &c0, &d0);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
  }
  int rs = 0;
  for (int i = 0; i * c0 <= n; i++) {
    rs = max(rs, dfs(n - i * c0, 0) + i * d0);
  }
  printf("%d\n", rs);
}
