program watera;
var
n,nt,k,i,j,k2,a,t,ans:longint;
sz:array[0..1000]of longint;
begin
assign(input,'watera.in');
assign(output,'watera.out');
reset(input);
rewrite(output);
readln(n,k);
nt:=n;
fillchar(sz,sizeof(sz),0);
a:=0;
while n<>0 do
begin
sz[0]:=sz[0]+1;
sz[sz[0]]:=n mod 2;
n:=n div 2;
if sz[sz[0]]=1 then a:=a+1;
end;
if a>k then begin
t:=0;
for i:=sz[0] downto 1 do
if sz[i]=1 then begin
t:=t+1;
if t=k then begin
for j:=i+1 to sz[0]+1 do
if sz[j]=0 then begin
sz[j]:=1;
for k2:=j-1 downto 1 do
sz[k2]:=0;
break;
end;
break;
end;
end;
end;
ans:=0;
if sz[sz[0]+1]=1 then sz[0]:=sz[0]+1;
for i:=1 to sz[0] do
if sz[i]=1 then ans:=ans+(1 shl (i-1));
writeln(ans-nt);
close(input);
close(output);
end.