program watera;
var
cn,count,ans,i,j,d,g,n,k:longint;
t:array [1..100] of integer;
begin
assign (input,'watera.in');
reset (input);
readln (n,k);
close (input);
assign (output,'watera.out');
rewrite (output);
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;
count:=i-1;d:=0;
for i:=1 to count do if t[i]=1 then inc(d);
if d<k then ans:=0 else begin
g:=0;
for i:=count downto 1 do begin
if t[i]=1 then inc(g);
if g=k then break;
end;
for j:=i to count+1 do if t[j]=0 then break;
t[j]:=1;
for i:=j-1 downto 1 do t[i]:=0;
if t[count+1]=1 then begin
ans:=1;
for i:=1 to count do ans:=ans*2;
end
else begin
ans:=0;
for i:=count downto 1 do if t[i]=1 then ans:=ans+trunc(exp(ln(2)*(i-1)));
end;
ans:=ans-cn;
end;
writeln (ans);
close (output);
end.