var
a,m,n,t,w,v1,w1,v2,w2,ans:longint;
f:array[0..20000]of longint;
begin
assign(input,'besta.in');
assign(output,'besta.out');
reset(input);
rewrite(output);
read(m,n,t);
for m:=0 to m do
f[m]:=t;
for n:=1 to n do
begin
read(v1,w1,v2,w2);
if w1<w2 then w:=w1 else w:=w2;
for a:=m-w downto 0 do
begin
if f[a]+v2>f[a+w2] then f[a+w2]:=f[a]+v2;
if f[a]+v1>f[a+w1] then f[a+w1]:=f[a]+v1;
end;
end;
writeln(f[m]);
close(input);close(output);
end.