program watera;
uses math;
var cn,u,ans,i,j,d,g,n,k:longint;
t:array[1..100]of integer;
begin
assign(input,'watera.in'); reset(input);
assign(output,'watera.out'); rewrite(output);
readln (n,k);
fillchar(t,sizeof(t),0);
i:=1; cn:=n;
while n<>0 do
begin
t[i]:=n mod 2;
n:=n div 2;
inc(i);
end;
u:=i-1; d:=0;
for i:=1 to u do if t[i]=1 then inc(d);
if d<k then ans:=0 else
begin
g:=0;
for i:=u downto 1 do
begin
if t[i]=1 then inc(g);
if g=k then break;
end;
for j:=i to u+1 do if t[j]=0 then break;
t[j]:=1;
for i:=j-1 downto 1 do t[i]:=0;
if t[u+1]=1 then
begin
ans:=1;
for i:=1 to u do ans*=2;
end
else begin
ans:=0;
for i:=u downto 1 do
if t[i]=1 then ans+=2**(i-1);
end;
ans-=cn;
end;
writeln(ans);
close(input); close(output);
end.