program goblin;
var
fin,fout:text;
n,m,n0:longint;
f,p,pai:array[0..500000]of longint;
a,b:array[1..100]of longint;
hash:array[1..100]of longint;
i,j,pp:longint;
procedure dp(x:longint);
begin
for i:=0 to n do begin
f[i]:=0;
p[i]:=0;
pai[i]:=0;
end;
if x=1 then
for i:=1 to m do begin
if a[i]>=b[i] then continue;
for j:=a[i] to n do
if f[j-a[i]]+b[i]>f[j] then begin
f[j]:=f[j-a[i]]+b[i];
p[j]:=i;
pai[j]:=j-a[i];
end;
end;
if x=2 then
for i:=1 to m do begin
if b[i]>=a[i] then continue;
for j:=b[i] to n do
if f[j-b[i]]+a[i]>f[j] then begin
f[j]:=f[j-b[i]]+a[i];
p[j]:=i;
pai[j]:=j-b[i];
end;
end;
pp:=n;
while p[pp]<>0 do begin
inc(hash[p[pp]]);
pp:=pai[pp];
end;
end;
begin
assign(fin,'goblin.in'); reset(fin);
assign(fout,'goblin.out'); rewrite(fout);
read(fin,n,m);
n0:=n;
for i:=1 to m do read(fin,a[i],b[i]);
for i:=1 to m do hash[i]:=0;
dp(1);
i:=n;
while f[i]=f[n] do begin
dec(i);
end;
inc(i);
n:=f[n]+(n-i);
dp(2);
writeln(fout,f[n]-n0);
for i:=1 to m do begin
write(fout,'Buy ');
write(fout,hash[i]);
if hash[i]<>0 then begin
if a[i]<b[i] then writeln(fout,' from Alliance');
if b[i]<a[i] then writeln(fout,' from Hord');
end else writeln(fout);
end;
close(fin);
close(fout);
end.