program watera(input,output);
var
i,j,n,nn,k,ans:longint;
k2:array[0..30]of longint;
bo:array[0..30]of byte;
begin
assign(input,'watera.in');
reset(input);
assign(output,'watera.out');
rewrite(output);
readln(n,k);
k2[0]:=1;
for i:=1 to 30 do
k2[i]:=k2[i-1] shl 1;
i:=30;
while i>=0 do
if k2[i]>n then
dec(i)
else
begin
inc(nn);
n:=n-k2[i];
bo[i]:=1;
dec(i);
end;
for i:=0 to 30 do
begin
if nn<=k then
break;
if bo[i]=1 then
begin
ans:=ans+k2[i];
inc(bo[i+1]);
end
else if bo[i]=2 then
begin
inc(bo[i+1]);
dec(nn);
end;
end;
writeln(ans);
close(input);
close(output);
end.