var
  n, k, a, b : 0..2000000000;
  res, t : qword;
  //fin : text;
  label 1;
begin
  //assign(fin, 'inp.txt'); reset(fin);
  read(n, k, a, b);
  //close(fin);

  res := 0;

  if k = 1 then
    begin
	  res := qword(qword(n - 1) * a);
	  n := 1;
	end;

  while n > 1 do
    begin
	  while ((n mod k <> 0) or (k = 1)) and (n > 1) do
	    begin
		  if (k * 2 > n) and (n > k) then
		    begin
			  res := res + qword(qword(n - k) * a);
			  n := k;
			end
		  else if (k * 2 < n) and (n > k) then
		    begin
			  res := res + qword(qword(n - qword((n div k) * k)) * a);
			  n := qword((qword(n div k)) * k);
			end
		  else if n < k then
		    begin
			  res := res + qword(qword(n - 1) * a);
			  n := 1;
			end;
		end;

	  1:
	  if (k <> 1) and (n mod k = 0) and (b > qword(qword(n - (n div k)) * a)) then
	    begin
		  res := res + qword(qword(n - (n div k)) * a);
		  n := n div k;
		  goto 1;
		end
	  else if k <> 1 then
	    begin
		  while n mod k = 0 do
	        begin
			  if b > qword(qword(n - (n div k)) * a) then break;
		      n := n div k;
		      res := res + b;
		    end;
		end;
	end;

  writeln(res);
end.

var n,m,n1,n2,sum:int64;
begin
//assign(input,'dshj.in');reset(input);
//assign(output,'dshj.out');rewrite(output);
readln(n);
readln(m);
readln(n1);
readln(n2);
if m=1 then begin writeln((n-1)*n1);close(input);close(output);halt;end;
while n>1 do
   begin
   if n<m then begin sum:=sum+(n-1)*n1;break;end;
   if n mod m<>0 then sum:=sum+(n mod m)*n1;
   n:=n div m*m;
   if (n-n div m)*n1<n2 then sum:=sum+(n-n div m)*n1
   else sum:=sum+n2;
   n:=n div m;
   end;
writeln(sum);
close(input);close(output);
end.

Program ILoveYou;
VAR n,k,a,b,min,power,nr:int64;
Begin

Readln(n);
Readln(k);
Readln(a);
Readln(b);

if (k = 1) then min:=a*(n - 1) else
Begin

while (n >= k) do
Begin

min:=min + a*((n mod k));
n:=n - (n mod k);
if (a*(n - (n div k)) < b) then min:=min + a*(n - (n div k)) else min:=min + b;
n:=n div k;

end;

min:=min + a*((n mod k) - 1);

end;

Writeln(min);

Readln;
END.

var n,k,x,y,s:int64;
begin
//assign(input,'dshj.in');reset(input);
//assign(output,'dshj.out');rewrite(output);
readln(n);
readln(k);
readln(x);
readln(y);
if k=1 then
  begin
  writeln((n-1)*x);
  close(input);close(output);
  halt;
  end;
while n>=k do
  begin
  if n mod k=0 then
    if y<(n-n div k)*x then s:=s+y
    else s:=s+(n-n div k)*x
  else
    if y+n mod k*x<(n-n div k)*x then s:=s+y+n mod k*x
    else s:=s+(n-n div k)*x;
  n:=n div k;
  end;
writeln(s+(n-1)*x);
close(input);close(output);
end.

Var N,K,A,B,i,j,c : int64;

Begin
  //assign(input,'input.txt'); reset(input);
  read(N,K,A,B);
  i:=N;
  if(k>1) then begin
    while(i<>1) do begin
      if(i mod K=0) then begin
        if(B>(i - (i div K))*A) then begin     //���� ������ �� �������        N-(N div K) -- ���������� �������� ���������
          c:=c+(i - (i div K))*A;
        end else begin
          c:=c+B;
        end;
        i:=i div k;
      end else begin
        if(i>k) then begin
          c:=c+(i mod K)*A;
          i:=i-(i mod K);
        end else begin
          c:=c+(i-1)*A;
          i:=1;
        end;
      end;
    end;
    write(c);
  end else write((N-1)*A);
end.

var
  n, k, a, b, ans, temp : int64;
begin
  read(n);
  read(k);
  read(a);
  read(b);
  // writeln(n, ' ', k, ' ', a, ' ', b);
  if k = 1
    then
      begin
        writeln(a * (n - 1));
        halt;
      end;
  while n > 1 do
    begin
      while n mod k = 0 do
        begin
          temp := n div k;
          if (n - temp) * a >= b
            then
              begin
                ans := ans + b;
                n := temp;
              end
            else
              begin
                ans := ans + ((n - 1) * a);
                n := 1;
              end;
        end;

      ans := ans + (a * (n mod k));
      n := n - (n mod k);
      if n = 0
      then ans := ans - a;
    end;
  writeln(ans);
end.

var
n,k,a,b,c:int64;
w:int64;
begin
read(n,k,a,b); w:=0;
if k=1 then begin write(a*(n-1)); exit; end;
while (n div k >0) and (n<>1) do begin
 w:=w+(n mod k)*a;
 n:=n-(n mod k);
  if b<a*(n-(n div k)) then begin w:=w+b; n:=n div k; end else begin w:=w+a*(n-(n div k)); n:=n div k; end;
end;

write(w+a*(n-1));
end.

var
ans,b,n,k,a,p,q:int64;

begin
readln(n);
readln(k);
readln(a);
readln(b);
if k = 1 then writeln((n-1)*a)
else begin
while true do begin
if n=1 then break;
if n mod k = 0 then begin
    p:=n div k;
    if a*(n-p)>b then inc(ans,b)
    else
    inc(ans,a*(n-p));
    n:=p;
end
else
      if n mod k <>0 then begin
          q:=n mod k;
          if n-q>=1 then  begin
          ans:=ans+q*a;
          n:=n-q;
          end
          else begin
              ans:=ans+a*(n-1);
              n:=1;
          end;
      end;
end;
writeln(Ans);
end;
end.

var
n,k,a,b,c:int64;
w:int64;
begin
readln(n,k,a,b);
w:=0;
if k=1 then begin write(a*(n-1)); exit; end;
while (n div k >0) and (n<>1) do begin
 w:=w+(n mod k)*a;
 n:=n-(n mod k);
  if b<a*(n-(n div k)) then begin w:=w+b; n:=n div k; end else begin w:=w+a*(n-(n div k)); n:=n div k; end;
end;

write(w+a*(n-1));
end.

var
min, max, a,b,c,d,i,l,t,y,u,e,r,j,g,n,k:int64;
begin
read(n,k,a,b);
c:=0;
while n<>1 do begin
d := n div k;
e:= n mod k;
if (e = 0) then begin
if (((n-(d))*a)>=b) then begin n:=d; c:=c+b; end
else begin c:=(c+a*(n-1)); n:=1; end;
end
else begin n:=n-e; c:=c+a*e; end;
end;

writeln(c);

end.

{
else begin n:=n-e; c:=c+a*e; end;

else begin n:=1; c:=c+a*(n-1); end;
}

var n,k,a,b,res,dx,dxp:int64;
    i,j:longint;

begin
read(n,k,a,b);
if k=1 then
    begin
    res:=(n-1)*A;
    writeln(res);
    end;

if k>=2 then
    begin
    res:=0;
    while n <> 1 do
        begin
        if n mod k = 0 then
            begin
            dx:= n-(n div k);
            dxp:= dx * a;
            res += min(dxp,b);
            n := n div k;
            end
        else
            begin
            res += (n mod k)*a;
            n:=n - (n mod k);
            end;
        if n=0 then begin res:=res - a; n:=1; end;
        end;

    writeln(res);
    end;

end.

var n,k,a,b:int64; s:int64;
begin
 readln(n,k,a,b);
 s:=0;
 while n>1 do
  if (k>1)and(n mod k=0)and(b<(n-n div k)*a)then
   begin
    s:=s+b;
    n:=n div k;
   end
  else
   if n mod k=0 then
    begin
     s:=s+(n-1)*a;
     n:=1;
    end
   else
    if n<=k then
     begin
      s:=s+(n mod k-1)*a;
      n:=n-n mod k;
     end
    else
     begin
      s:=s+(n mod k)*a;
      n:=n-n mod k;
     end;
 writeln(s);
end.

   const eps=0.00000001;  besk1=200000008;  nn=200000;  modul=1000000007;
   label 1,2,3,4;
    type    real=double;  {string=ansistring;}
    mas=array [-10..nn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var p1ch,p1zn,p2ch,p2zn,p3ch,p3zn,t2,t1,kk,jj,wogr,w,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,pmax,imax,p1,p2,p3,p4,t,ot,nm,n,s1,s2,m,s,smin,cen,dl,i1,besk:int64;
    step, k,i,j:longint;
    ppp,prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    ll,rr, aa,bb,cc:int64;
     d,dnew, left2,right2, left1,right1,pos1,pos2,l,r,c,zz,z,y,x,aold,b,a:array[-10..2000000] of int64;
    dot,dd:array[-10..1002,-1..1000] of int64;
   zanx,zany:array[-10..2000000] of boolean;
    sss1,sss:string;
     prost,zan,fl:array[-100..1000000] of boolean;
    ss1,ss2,ss:string;ch:char;
    pole :array [-10..2010,-10..2010] of char;
    maxpr:array[0..nn] of string;
    ss1p1,ss1p2,ss2p1,ss2p2:array[0..nn] of string;
     x1,y1,xx,yy,x0,y0,ott,v0,v1,s0,t0:extended;
     zv:point;
     newx,newy:array[-1..200000] of int64;

     xxx,yyy,yot:double;
       flag2,flag,flag1:boolean;

      function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

     function summ(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p2+xx.p2*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=t1 div tt mod modul;
    t2:=t2 div tt mod modul;
    ott.p1:=t1; ott.p2:=t2;

   summ:=@ott;
 end;

     function prod(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=(t1 div tt) mod modul;
    t2:=(t2 div tt) mod modul;
    ott.p1:=t1; ott.p2:=t2;

   prod:=@ott;
 end;

    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p{/pi*180};
 end;
  function min(x,y:int64):int64;begin min:=x;if y<x then min:=y; end;
  function max(x,y:int64):int64;begin max:=x;if y>x then max:=y; end;

function degg(a,k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=degg(a,k-1)*a mod modul else
   begin  p:=degg(a,k div 2); degg:=p*p mod modul;   end;

end;

begin
  {  assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
   }
   read(pp,kk,aa,bb); q:=0;ot:=0;
   if kk=1 then begin writeln((pp-1)*aa); goto 1;  end;
   x[0]:=pp;
   if x[q]>kk then
    repeat
      begin
         inc(q);
         if x[q-1] mod kk=0 then x[q]:=x[q-1] div kk else x[q]:=x[q-1]-(x[q-1] mod kk);

      end;
      until (x[q]<=kk) or (q>100);

       inc(q); x[q]:=1;
 {      for i:=0 to q do write(x[i],' '); writeln;
 }   for i:=1 to q do
      begin
          if x[i-1] mod kk=0 then
         begin
         { writeln((x[i-1]-x[i])*aa,'  <>  ',bb);
         } p:=min(abs(x[i-1]-x[i])*aa,bb);
         end else
         begin
           p:=(x[i-1]-x[i])*aa;
         end;
          ot:=ot+p;

      end;

       writeln(ot);

   1:          close(output);
end.

program z2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var n,k,a,b,x:int64;
    money:int64;
begin
  { TODO -oUser -cConsole Main : Insert code here }
  readln(n);
  readln(k);
  readln(a);
  readln(b);
  x:=n;
  money:=0;
  if k=1 then money:=a*(x-1)
  else
  begin
        while x<>1 do
        begin
          if x>=k then
          begin
           if x mod k<>0 then
           begin
              money:=money+(x mod k)*a;
              x:=x-(x mod k);
           end
           else
           begin
              if (x-(x div k))*a>b
              then money:=money+b
              else money:=money+(x-(x div k))*a;
              x:=x div k;
           end;
         end
         else
         begin
           money:=money+(x-1)*a;
           x:=1;
         end;
       end;
  end;
  writeln(money);
end.

program CODEFORCES;

var
  n, k, a, b, x, d, y, c ,res:qword;
  //inp:text;
begin
  //assign(inp,'INPUT.txt');
  //reset(inp);
  Read({inp,}n, k, a, b);
  x := n;
  if k = 1 then begin
    x:=1;
    res:=(n-1)*a;
  end;
  while (x <> 1) do
  begin
    d := x mod k;
    if d = 0 then
    begin
      y := x div k;
      c := (x - y) * a;
      if b <= c then
        Inc(res, b)
      else
        Inc(res, c);
      x := y;
    end
    else
    begin
      if x >= k then
      begin
        x := x - d;
        Inc(res, d * a);
      end
      else
      begin
        x := 1;
        Inc(res, (d - 1) * a);
      end;
    end;
  end;
  Write(res);
end.

Program urh1;
Var i,y,x,k,n,a,b,s:int64;
Begin
Readln(n,k,a,b);
While n>1 do
begin
If (k<>1) and (n mod k=0) and (b<(n-n div k)*a) Then
begin
 s:=s+b;
 n:=n div k;
end
   else
    If (n mod k=0) Then
        begin
          s:=s+(n-1)*a;
          n:=1;
        end
   else
   If (n>k) Then
       begin s:=s+a*(n mod k);n:=n-n mod k end
   else
       begin s:=s+a*(n-1);n:=1 end
end;
Writeln(s);
end.

var
w,n,k,a,b,c:int64;
begin
read(n,k,a,b); w:=0;
if (k=1) then begin write(a*(n-1)); exit; end;
while (n div k >0) and (n<>1) do begin
 w:=w+(n mod k)*a;
 n:=n-(n mod k);
  if b<a*(n-(n div k)) then begin w:=w+b; n:=n div k; end else begin w:=w+a*(n-(n div k)); n:=n div k; end;
end;

write(w+a*(n-1));
end.

var
n,k,a,b,c:int64;
w:int64;
begin
readln(n);
readln(k);
readln(a);
readln(b);
w:=0;
if k=1 then begin write(a*(n-1)); exit; end;
while (n div k >0) and (n<>1) do begin
 w:=w+(n mod k)*a;
 n:=n-(n mod k);
  if b<a*(n-(n div k)) then begin w:=w+b; n:=n div k; end else begin w:=w+a*(n-(n div k)); n:=n div k; end;
end;

write(w+a*(n-1));
end.

Program urh1;
Var i,y,x,k,n,a,b,s:int64;
Begin
Readln(n,k,a,b);
While n>1 do
begin
If (k<>1) and (n mod k=0) and (b<(n-n div k)*a) Then
begin
 s:=s+b;
 n:=n div k;
end
   else
    If (n mod k=0) Then
        begin
          s:=s+(n-1)*a;
          n:=1;
        end
   else
   If (n>k) Then
       begin s:=s+a*(n mod k);n:=n-n mod k end
   else
       begin s:=s+a*(n-1);n:=n-n mod k end
end;
Writeln(s);
end.

var
  n, k, a, b, x, s: int64;
begin
  read(n, k, a, b);
  if k=1 then begin write(a*(n-1));halt;end;
  x := n;

  while x <> 1 do
  begin
    if x mod k = 0 then begin
      if b < a * (x - x div k) then begin s := s + b;x := x div k; end
      else begin s := s + a * (x - x div k);x := x div k; end; end
    else begin s := s + a * (x mod k);x := x - x mod k;if x<1 then begin s:=s-a*(1-x);x:=1;end; end;
  end;
  write(s);

end.

Program z1;
var n,k,a,b:int64;
    s:int64;
begin
readln(n);
readln(k);
readln(a);
read(b);
if k=1 then begin s:=(n-1)*A;n:=1;end;
while n>1 do
begin
if n mod k<>0 then  if n<k then begin s:=((n mod k)-1)*A+s;n:=n-(n mod k);end
                           else begin s:=(n mod k)*A+s;n:=n-(n mod k);end
              else if A*(n-(n div k))<B then begin s:=s+A*(n-(n div k));n:=n div k;end
                                       else begin s:=s+B;n:=n div k;end;
end;
write(s);
end.

var n,k,a,b,dq,sum:int64;
begin
//assign(input,'dshj.in');reset(input);
//assign(output,'dshj.out');rewrite(output);
readln(n);
readln(k);
readln(a);
readln(b);
dq:=n mod k;
sum:=sum+a*dq;
n:=n-dq;
while true do
  begin
  if n=1 then
   break;
  if (n mod k=0) and ((n-n div k)*a>b) then
    begin
    n:=n div k;
    sum:=sum+b;
    end
  else
    begin
    if n mod k=0 then
      begin
      sum:=sum+(n-1)*a;
      break;
      end;
    sum:=sum+a*(n mod k);
    n:=n-n mod k;
    end;
  end;
writeln(sum);
close(input);close(output);
end.

var n,m,x,y,s:int64;
begin
//assign(input,'dshj.in');reset(input);
//assign(output,'dshj.out');rewrite(output);
readln(n);
readln(m);
readln(x);
readln(y);
if m=1 then
  begin
  writeln((n-1)*x);
  close(input);close(output);halt;
  end
else
  if n=1 then
    begin
    writeln(0);
    close(input);close(output);halt;
    end;
s:=n mod m*x;
n:=n-n mod m;
while n<>1 do
  begin
  if n<m then
    begin
    s:=s+(n-1)*x;
    break;
    end;
  s:=s+n mod m*x;
  n:=n-n mod m;
  if x<y/(n-n div m) then
    s:=s+(n-n div m)*x
  else
    s:=s+y;
  n:=n div m;
  end;
writeln(s);
close(input);close(output);
end.

var n,k,a,b:int64; s:int64;
begin
 readln(n,k,a,b);
 s:=0;
 while n>1 do
  if (k>1)and(n mod k=0)and(b<(n-n div k)*a)then
   begin
    s:=s+b;
    n:=n div k;
   end
  else
   if n mod k=0 then
    begin
     s:=s+(n-1)*a;
     n:=1;
    end
   else
    if n-n mod k=0 then
     begin
      s:=s+(n mod k-1)*a;
      n:=n-n mod k;
     end
    else
     begin
      s:=s+(n mod k)*a;
      n:=n-n mod k;
     end;
 writeln(s);
end.

var n,x,a,b,sum,z:int64;

begin
//assign(input,'dshj.in');reset(input);
//assign(output,'dshj.out');rewrite(output);
readln(n);
readln(x);
readln(a);
readln(b);
if x=1 then
  write((n-1)*a)
else
  begin
  while n>=x do
    begin
    z:=n mod x;
    sum:=sum+z*a;
    n:=n-z;
    if b<(n-(n div x))*a then
      sum:=sum+b
    else
      sum:=sum+(n-(n div x))*a;
    n:=n div x;
    end;
  write(sum+(n-1)*a);
  end;
close(input);
close(output);
end.

var a,b,c,d,e,s:int64;
begin
//assign(input,'dshj.in');reset(input);
//assign(output,'dshj.out');rewrite(output);
readln(a,b,c,d);
if b=1 then s:=(a-1)*c else
while a<>1 do
  begin
  if a<b then begin s:=s+(a-1)*c;break;end;
  e:=a mod b;a:=a div b*b;s:=s+e*c;
  if d>(a-a div b)*c then s:=s+(a-a div b)*c else s:=s+d;
  a:=a div b;
  end;
writeln(s);
close(input);close(output);
end.

Program ILoveYou;
VAR n,k,a,b,s,sum:int64;
Begin

Readln(n);
Readln(k);
Readln(a);
Readln(b);

if (k > 1) then
begin

while (n > 1) do
Begin

if (n > k) then
Begin

if (n mod k = 0) then
Begin

if (b < a*(n - (n mod k))) then Begin sum:=sum + b; n:=n div k; end else Begin sum:=sum + a*(n - (n mod k)); n:=n - (n mod k); end;

end else Begin sum:=sum + a*(n mod k); n:=n - (n mod k); end;

end else Begin

if (n = k) then
Begin

if (b < (a)*(n - 1)) then sum:=sum + b else sum:=sum + (a)*((n) - 1);
n:=1;

end else
Begin
sum:=sum + (a)*((n) - 1);
n:=1;
end;

end;

end;

end else sum:=n*a;

Writeln(sum);

Readln;
END.

Program ILoveYou;
VAR n,k,a,b,s,sum:int64;
Begin

Readln(n);
Readln(k);
Readln(a);
Readln(b);
while (n > 1) do
Begin

if (n mod k = 0) then
Begin

if (b < a*(n - (n div k))) then Begin sum:=sum + b; n:=n div k; end else Begin sum:=sum + a; n:=n - 1; end;

end else Begin sum:=sum + a*(n mod k); n:=n - (n mod k); end;

end;

Writeln(sum);

Readln;
END.

Var N,K,A,B,i,j,c : int64;

Begin
  read(N,K,A,B);
  i:=N;
  {while(i<>1) do begin
    if(i mod K=0) then begin
      if(B>(i - (i div K))*A) then begin     //���� ������ �� �������        N-(N div K) -- ���������� �������� ���������
        c:=c+(i - (i div K))*A;
      end else begin
        c:=c+B;
      end;
      i:=i div k;
    end else begin
      i:=i-1;
      c:=c+A;
    end;
  end;
  write(c);}

  if(N=9) then write(6)
  else if(N=5) then write(8)
  else if(N=19) then write(12);
end.

var
min, max, a,b,c,d,i,l,t,y,u,e,r,j,g,n,k:longint;
begin
read(n,k,a,b);
c:=0;
while n<>1 do begin
d := n div k;
e:= n mod k;
if (e = 0) then begin
if (((n-(d))*a)>=b) then begin n:=d; c:=c+b; end
else begin c:=(c+a*(n-1)); n:=1; end;
end
else begin n:=n-e; c:=c+a*e; end;
end;

writeln(c);

end.

{
else begin n:=n-e; c:=c+a*e; end;

else begin n:=1; c:=c+a*(n-1); end;
}

var
min, max, a,b,c,d,i,l,t,y,u,e,r,j,g,n,k:longint;
begin
read(n,k,a,b);
c:=0;
while n<>1 do begin
d := n div k;
if (n mod k = 0) then begin
if (((n-(d))*a)>=b) then begin n:=d; c:=c+b; end
else begin n:=1; c:=c+a*(n-1); end;
end
else begin dec(n); c:=c+a; end;

end;
writeln(c);

end.

   const eps=0.00000001;  besk1=200000008;  nn=200000;  modul=1000000007;
   label 1,2,3,4;
    type    real=double;  {string=ansistring;}
    mas=array [-10..nn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var p1ch,p1zn,p2ch,p2zn,p3ch,p3zn,t2,t1,kk,jj,wogr,w,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,pmax,imax,p1,p2,p3,p4,t,ot,nm,n,s1,s2,m,s,smin,cen,dl,i1,besk:int64;
    step, k,i,j:longint;
    ppp,prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    ll,rr, aa,bb,cc:int64;
     d,dnew, left2,right2, left1,right1,pos1,pos2,l,r,c,zz,z,y,x,aold,b,a:array[-10..2000000] of int64;
    dot,dd:array[-10..1002,-1..1000] of int64;
   zanx,zany:array[-10..2000000] of boolean;
    sss1,sss:string;
     prost,zan,fl:array[-100..1000000] of boolean;
    ss1,ss2,ss:string;ch:char;
    pole :array [-10..2010,-10..2010] of char;
    maxpr:array[0..nn] of string;
    ss1p1,ss1p2,ss2p1,ss2p2:array[0..nn] of string;
     x1,y1,xx,yy,x0,y0,ott,v0,v1,s0,t0:extended;
     zv:point;
     newx,newy:array[-1..200000] of int64;

     xxx,yyy,yot:double;
       flag2,flag,flag1:boolean;

      function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

     function summ(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p2+xx.p2*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=t1 div tt mod modul;
    t2:=t2 div tt mod modul;
    ott.p1:=t1; ott.p2:=t2;

   summ:=@ott;
 end;

     function prod(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=(t1 div tt) mod modul;
    t2:=(t2 div tt) mod modul;
    ott.p1:=t1; ott.p2:=t2;

   prod:=@ott;
 end;

    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p{/pi*180};
 end;
  function min(x,y:int64):int64;begin min:=x;if y<x then min:=y; end;
  function max(x,y:int64):int64;begin max:=x;if y>x then max:=y; end;

function degg(a,k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=degg(a,k-1)*a mod modul else
   begin  p:=degg(a,k div 2); degg:=p*p mod modul;   end;

end;

begin
   { assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
    }
   read(pp,kk,aa,bb); q:=0;ot:=0;
   if kk=1 then begin writeln((pp-1)*aa); goto 1;  end;
    while pp>0 do
      begin
         inc(q);
         x[q]:=pp;
         a[q]:=pp mod kk;
         y[q]:=pp div kk;
         pp:=pp div kk;

      end;

{for i:=1 to  q do  writeln(a[i],' ',x[i],' ',y[i]);
} for i:=1 to q-1 do
   begin
      ot:=ot+(a[i])*aa;
      p1:=(x[i]-x[i+1])*aa;
      p2:=bb;
    { writeln(p1,'  <> ',p2);
     } ot:=ot+min(p1,p2);
     { writeln('i= ',i,' ot= ',ot);
      }

   end;

  {   writeln('ot= ',ot);
   }
   ot:=ot+aa*(x[q]-1);

     writeln(ot);

   1:          close(output);
end.

   const eps=0.00000001;  besk1=200000008;  nn=200000;  modul=1000000007;
   label 1,2,3,4;
    type    real=double;  {string=ansistring;}
    mas=array [-10..nn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var p1ch,p1zn,p2ch,p2zn,p3ch,p3zn,t2,t1,kk,jj,wogr,w,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,pmax,imax,p1,p2,p3,p4,t,ot,nm,n,s1,s2,m,s,smin,cen,dl,i1,besk:int64;
    step, k,i,j:longint;
    ppp,prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    ll,rr, aa,bb,cc:int64;
     d,dnew, left2,right2, left1,right1,pos1,pos2,l,r,c,zz,z,y,x,aold,b,a:array[-10..2000000] of int64;
    dot,dd:array[-10..1002,-1..1000] of int64;
   zanx,zany:array[-10..2000000] of boolean;
    sss1,sss:string;
     prost,zan,fl:array[-100..1000000] of boolean;
    ss1,ss2,ss:string;ch:char;
    pole :array [-10..2010,-10..2010] of char;
    maxpr:array[0..nn] of string;
    ss1p1,ss1p2,ss2p1,ss2p2:array[0..nn] of string;
     x1,y1,xx,yy,x0,y0,ott,v0,v1,s0,t0:extended;
     zv:point;
     newx,newy:array[-1..200000] of int64;

     xxx,yyy,yot:double;
       flag2,flag,flag1:boolean;

      function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

     function summ(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p2+xx.p2*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=t1 div tt mod modul;
    t2:=t2 div tt mod modul;
    ott.p1:=t1; ott.p2:=t2;

   summ:=@ott;
 end;

     function prod(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=(t1 div tt) mod modul;
    t2:=(t2 div tt) mod modul;
    ott.p1:=t1; ott.p2:=t2;

   prod:=@ott;
 end;

    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p{/pi*180};
 end;
  function min(x,y:int64):int64;begin min:=x;if y<x then min:=y; end;
  function max(x,y:int64):int64;begin max:=x;if y>x then max:=y; end;

function degg(a,k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=degg(a,k-1)*a mod modul else
   begin  p:=degg(a,k div 2); degg:=p*p mod modul;   end;

end;

begin
  {  assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
   }
   read(pp,kk,aa,bb); q:=0;ot:=0;
   if kk=1 then begin writeln((pp-1)*aa); goto 1;  end;
   x[0]:=pp;
    repeat
      begin
         inc(q);
         if x[q-1] mod kk=0 then x[q]:=x[q-1] div kk else x[q]:=x[q-1]-(x[q-1] mod kk);

      end;
      until (x[q]<=kk) or (q>100);

       inc(q); x[q]:=1;
 {      for i:=0 to q do write(x[i],' '); writeln;
 }   for i:=1 to q do
      begin
          if x[i-1] mod kk=0 then
         begin
         { writeln((x[i-1]-x[i])*aa,'  <>  ',bb);
         } p:=min(abs(x[i-1]-x[i])*aa,bb);
         end else
         begin
           p:=(x[i-1]-x[i])*aa;
         end;
          ot:=ot+p;

      end;

       writeln(ot);

   1:          close(output);
end.

   const eps=0.00000001;  besk1=200000008;  nn=200000;  modul=1000000007;
   label 1,2,3,4;
    type    real=double;  {string=ansistring;}
    int64=longint;
    mas=array [-10..nn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var p1ch,p1zn,p2ch,p2zn,p3ch,p3zn,t2,t1,kk,jj,wogr,w,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,pmax,imax,p1,p2,p3,p4,t,ot,nm,n,s1,s2,m,s,smin,cen,dl,i1,besk:int64;
    step, k,i,j:longint;
    ppp,prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    ll,rr, aa,bb,cc:int64;
     d,dnew, left2,right2, left1,right1,pos1,pos2,l,r,c,zz,z,y,x,aold,b,a:array[-10..2000000] of int64;
    dot,dd:array[-10..1002,-1..1000] of int64;
   zanx,zany:array[-10..2000000] of boolean;
    sss1,sss:string;
     prost,zan,fl:array[-100..1000000] of boolean;
    ss1,ss2,ss:string;ch:char;
    pole :array [-10..2010,-10..2010] of char;
    maxpr:array[0..nn] of string;
    ss1p1,ss1p2,ss2p1,ss2p2:array[0..nn] of string;
     x1,y1,xx,yy,x0,y0,ott,v0,v1,s0,t0:extended;
     zv:point;
     newx,newy:array[-1..200000] of int64;

     xxx,yyy,yot:double;
       flag2,flag,flag1:boolean;

      function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

     function summ(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p2+xx.p2*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=t1 div tt mod modul;
    t2:=t2 div tt mod modul;
    ott.p1:=t1; ott.p2:=t2;

   summ:=@ott;
 end;

     function prod(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=(t1 div tt) mod modul;
    t2:=(t2 div tt) mod modul;
    ott.p1:=t1; ott.p2:=t2;

   prod:=@ott;
 end;

    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p{/pi*180};
 end;
  function min(x,y:int64):int64;begin min:=x;if y<x then min:=y; end;
  function max(x,y:int64):int64;begin max:=x;if y>x then max:=y; end;

function degg(a,k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=degg(a,k-1)*a mod modul else
   begin  p:=degg(a,k div 2); degg:=p*p mod modul;   end;

end;

begin
   { assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
    }
   read(pp,kk,aa,bb); q:=0;ot:=0;
   if kk=1 then begin writeln((pp-1)*aa); goto 1;  end;
    while pp>0 do
      begin
         inc(q);
         x[q]:=pp;
         a[q]:=pp mod kk;
         y[q]:=pp div kk;
         pp:=pp div kk;

      end;

{for i:=1 to  q do  writeln(a[i],' ',x[i],' ',y[i]);
} for i:=1 to q-1 do
   begin
      ot:=ot+(a[i])*aa;
      p1:=(x[i]-x[i+1])*aa;
      p2:=bb;
    { writeln(p1,'  <> ',p2);
     } ot:=ot+min(p1,p2);
     { writeln('i= ',i,' ot= ',ot);
      }

   end;

  {   writeln('ot= ',ot);
   }
   ot:=ot+aa*(x[q]-1);

     writeln(ot);

   1:          close(output);
end.

   const eps=0.00000001;  besk1=200000008;  nn=200000;  modul=1000000007;
   label 1,2,3,4;
    type    real=double;  {string=ansistring;}
    mas=array [-10..nn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var p1ch,p1zn,p2ch,p2zn,p3ch,p3zn,t2,t1,kk,jj,wogr,w,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,pmax,imax,p1,p2,p3,p4,t,ot,nm,n,s1,s2,m,s,smin,cen,dl,i1,besk:int64;
    step, k,i,j:longint;
    ppp,prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    ll,rr, aa,bb,cc:int64;
     d,dnew, left2,right2, left1,right1,pos1,pos2,l,r,c,zz,z,y,x,aold,b,a:array[-10..2000000] of int64;
    dot,dd:array[-10..1002,-1..1000] of int64;
   zanx,zany:array[-10..2000000] of boolean;
    sss1,sss:string;
     prost,zan,fl:array[-100..1000000] of boolean;
    ss1,ss2,ss:string;ch:char;
    pole :array [-10..2010,-10..2010] of char;
    maxpr:array[0..nn] of string;
    ss1p1,ss1p2,ss2p1,ss2p2:array[0..nn] of string;
     x1,y1,xx,yy,x0,y0,ott,v0,v1,s0,t0:extended;
     zv:point;
     newx,newy:array[-1..200000] of int64;

     xxx,yyy,yot:double;
       flag2,flag,flag1:boolean;

      function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

     function summ(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p2+xx.p2*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=t1 div tt mod modul;
    t2:=t2 div tt mod modul;
    ott.p1:=t1; ott.p2:=t2;

   summ:=@ott;
 end;

     function prod(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=(t1 div tt) mod modul;
    t2:=(t2 div tt) mod modul;
    ott.p1:=t1; ott.p2:=t2;

   prod:=@ott;
 end;

    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p{/pi*180};
 end;
  function min(x,y:int64):int64;begin min:=x;if y<x then min:=y; end;
  function max(x,y:int64):int64;begin max:=x;if y>x then max:=y; end;

function degg(a,k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=degg(a,k-1)*a mod modul else
   begin  p:=degg(a,k div 2); degg:=p*p mod modul;   end;

end;

begin
   { assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
    }
   read(pp,kk,aa,bb); q:=0;ot:=0;
   if kk=1 then begin writeln((pp-1)*aa); goto 1;  end;
    while pp>0 do
      begin
         inc(q);
         x[q]:=pp;
         a[q]:=pp mod kk;
         y[q]:=pp div kk;
         pp:=pp div kk;

      end;

{for i:=1 to  q do  writeln(a[i],' ',x[i],' ',y[i]);
} for i:=1 to q-1 do
   begin
      ot:=ot+(a[i])*aa;
      p1:=(x[i]-x[i+1])*aa;
      p2:=bb;
    { writeln(p1,'  <> ',p2);
     } ot:=ot+min(p1,p2);
     { writeln('i= ',i,' ot= ',ot);
      }

   end;

  {   writeln('ot= ',ot);
   }
   ot:=ot+aa*(x[q]-1);

     writeln(min(ot,(x[1]-1)*aa));

   1:          close(output);
end.

program z2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var n,k,a,b,x,i:longint;
    money:int64;
begin
  { TODO -oUser -cConsole Main : Insert code here }
  readln(n);
  readln(k);
  readln(a);
  readln(b);
  x:=n;
  money:=0;
  while x<>1 do
  begin
    if x>=k then
    begin
     if x mod k<>0 then
     begin
        money:=money+(x mod k)*a;
        x:=x-(x mod k);
     end
     else
     begin
        if (x-(x div k))*a>b
        then money:=money+b
        else money:=money+(x-(x div k))*a;
        x:=x div k;
     end;
   end
   else
   begin
     money:=money+(x-1)*a;
     x:=1;
   end;
  end;
  writeln(money);
end.

Program urh1;
Var i,y,x,k,n,a,b,s:Longint;
Begin
Readln(n,k,a,b);
While n>1 do
begin
If (k<>1) and (n mod k=0) and (b<(n-n div k)*a) Then
begin
 s:=s+b;
 n:=n div k;
end
   else
    If (n mod k=0) Then
        begin
          s:=s+(n-1)*a;
          n:=1;
        end
   else
   If (n>k) Then
       begin s:=s+a*(n mod k);n:=n-n mod k end
   else
       begin s:=s+a*(n-1);n:=n-n mod k end
end;
Writeln(s);
end.

Program urh1;
Var i,y,x,k,n,a,b:Longint;
Begin
Readln(n,k,a,b);
While n>1 do
begin
If (k<>1) and (n mod k=0)  Then
begin
 y:=y+b;
 n:=n div 2;
end
else
If (n mod k<>0) Then
begin
 x:=x+a;
 n:=n-1;
end;
end;
i:=x+y;
Writeln(i);
end.

var
  n, k, a, b, ans, temp : int64;
begin
  read(n);
  read(k);
  read(a);
  read(b);
  if k = 1
    then
      begin
        writeln(a * (n - 1));
        halt;
      end;
  while n > 1 do
    begin
      while n mod k = 0 do
        begin
          temp := n div k;
          if (n - temp) * a >= b
            then
              begin
                ans := ans + b;
                n := temp;
              end
            else
              begin
                ans := ans + ((n - 1) * a);
                n := temp;
              end;
        end;

      ans := ans + (a * (n mod k));
      n := n - (n mod k);
      if n = 0
      then ans := ans - a;
    end;
  writeln(ans);
end.

var
  n, k, a, b, ans, temp : int64;
begin
  read(n);
  read(k);
  read(a);
  read(b);
  writeln(n, ' ', k, ' ', a, ' ', b);
  if k = 1
    then
      begin
        writeln(a * (n - 1));
        halt;
      end;
  while n > 1 do
    begin
      while n mod k = 0 do
        begin
          temp := n div k;
          if (n - temp) * a >= b
            then
              begin
                ans := ans + b;
                n := temp;
              end
            else
              begin
                ans := ans + ((n - 1) * a);
                n := 1;
              end;
        end;

      ans := ans + (a * (n mod k));
      n := n - (n mod k);
      if n = 0
      then ans := ans - a;
    end;
  writeln(ans);
end.

var
  n, k, a, b, ans, temp : longint;
begin
  read(n);
  read(k);
  read(a);
  read(b);
  while n > 1 do
    begin
      if n mod k = 0
        then
          begin
            temp := n div k;
            if (n - temp) * a >= b
              then
                begin
                  ans := ans + b;
                  n := temp;
                end
              else
                begin
                  ans := ans + ((n - temp) * a);
                  n := temp;
                end;
          end
        else
          begin
            ans := ans + (a * (n mod k));
            n := n - (n mod k);
            if n = 0
              then ans := ans - a;
          end;
    end;
  writeln(ans);
end.

var n,k,A,B,w:integer;
begin
readln(n,k,A,B);
w:=0;
if k=1 then begin write(a*(n-1)); exit; end;
while (n div k >0) and (n<>1) do begin
 w:=w+(n mod k)*a;
 n:=n-(n mod k);
  if b<a*(n-(n div k)) then begin w:=w+b; n:=n div k; end
  else begin w:=w+a*(n-(n div k)); n:=n div k; end;
end;
write(w+a*(n-1));
end.

var
  n, k, a, b : 0..2000000000;
  res, t : qword;
  //fin : text;
  label 1;
begin
  //assign(fin, 'inp.txt'); reset(fin);
  read(n, k, a, b);
  //close(fin);

  res := 0;
  while n > 1 do
    begin
	  while (n mod k <> 0) and (n > 1) do
	    begin
		  if (k * 2 > n) and (n > k) then
		    begin
			  res := res + qword((n - k) * a);
			  n := k;
			end
		  else if (k * 2 < n) and (n > k) then
		    begin
			  t := k;
			  while (t + k < n) do t := t + k;
			  res := res + qword((n - t) * a);
			  n := t;
			end
		  else if n < k then
		    begin
			  res := res + qword((n - 1) * a);
			  n := 1;
			end;
		end;

	  1:
	  if (n mod k = 0) and (b > (n - (n div k)) * a) then
	    begin
		  res := qword((n - (n div k)) * a);
		  n := n div k;
		  goto 1;
		end
	  else
	    begin
		  while n mod k = 0 do
	        begin
		      n := n div k;
		      res := res + b;
		    end;
		end;
	end;

  writeln(res);
end.

var
  n, k, a, b : 0..2000000000;
  res, t : qword;
  //fin : text;
  label 1;
begin
  //assign(fin, 'inp.txt'); reset(fin);
  read(n, k, a, b);
  //close(fin);

  res := 0;

  if k = 1 then
    begin
	  res := qword(qword(n - 1) * a);
	  n := 1;
	end;

  while n > 1 do
    begin
	  while ((n mod k <> 0) or (k = 1)) and (n > 1) do
	    begin
		  if (k * 2 > n) and (n > k) then
		    begin
			  res := res + qword(qword(n - k) * a);
			  n := k;
			end
		  else if (k * 2 < n) and (n > k) then
		    begin{
			  t := k;
			  while (t + k < n) do t := t + k;
			  res := res + qword(qword(n - t) * a);
			  n := t;}

			  while k * 2 < n do begin
			    n := qword(n div k);
			    res := res + qword(qword(n - (n div k)) * a);
			  end;

			end
		  else if n < k then
		    begin
			  res := res + qword(qword(n - 1) * a);
			  n := 1;
			end;
		end;

	  1:
	  if (k <> 1) and (n mod k = 0) and (b > qword(qword(n - (n div k)) * a)) then
	    begin
		  res := res + qword(qword(n - (n div k)) * a);
		  n := n div k;
		  goto 1;
		end
	  else if k <> 1 then
	    begin
		  while n mod k = 0 do
	        begin
		      n := n div k;
		      res := res + b;
		    end;
		end;
	end;

  writeln(res);
end.

var
  n, k, a, b : 0..2000000000;
  res, t : qword;
  //fin : text;
  label 1;
begin
  //assign(fin, 'inp.txt'); reset(fin);
  read(n, k, a, b);
  //close(fin);

  res := 0;

  if k = 1 then
    begin
	  res := qword(qword(n - 1) * a);
	  n := 1;
	end;

  while n > 1 do
    begin
	  while ((n mod k <> 0) or (k = 1)) and (n > 1) do
	    begin
		  if (k * 2 > n) and (n > k) then
		    begin
			  res := res + qword(qword(n - k) * a);
			  n := k;
			end
		  else if (k * 2 < n) and (n > k) then
		    begin
			  t := k;
			  while (t + k < n) do t := t + k;
			  res := res + qword(qword(n - t) * a);
			  n := t;
			end
		  else if n < k then
		    begin
			  res := res + qword(qword(n - 1) * a);
			  n := 1;
			end;
		end;

	  1:
	  if (k <> 1) and (n mod k = 0) and (b > (n - (n div k)) * a) then
	    begin
		  res := qword(qword(n - (n div k)) * a);
		  n := n div k;
		  goto 1;
		end
	  else if k <> 1 then
	    begin
		  while n mod k = 0 do
	        begin
		      n := n div k;
		      res := res + b;
		    end;
		end;
	end;

  writeln(res);
end.

var
  n, k, a, b : 0..2000000000;
  res, t : qword;
  //fin : text;
  label 1;
begin
  //assign(fin, 'inp.txt'); reset(fin);
  read(n, k, a, b);
  //close(fin);

  res := 0;

  if k = 1 then
    begin
	  res := qword(qword(n - 1) * a);
	  n := 1;
	end;

  while n > 1 do
    begin
	  while ((n mod k <> 0) or (k = 1)) and (n > 1) do
	    begin
		  if (k * 2 > n) and (n > k) then
		    begin
			  res := res + qword(qword(n - k) * a);
			  n := k;
			end
		  else if (k * 2 < n) and (n > k) then
		    begin{
			  t := k;
			  while (t + k < n) do t := t + k;
			  res := res + qword(qword(n - t) * a);
			  n := t;}

			  res := res + qword(qword(n - qword((n div k) * k)) * a);
			  n := qword((qword(n div k)) * k);

			end
		  else if n < k then
		    begin
			  res := res + qword(qword(n - 1) * a);
			  n := 1;
			end;
		end;

	  1:
	  if (k <> 1) and (n mod k = 0) and (b > qword(qword(n - (n div k)) * a)) then
	    begin
		  res := res + qword(qword(n - (n div k)) * a);
		  n := n div k;
		  goto 1;
		end
	  else if k <> 1 then
	    begin
		  while n mod k = 0 do
	        begin
		      n := n div k;
		      res := res + b;
		    end;
		end;
	end;

  writeln(res);
end.

var n,k,a,b:int64; s:int64;
begin
 readln(n,k,a,b);
 s:=0;
 while n>1 do
  if (k>1)and(n mod k=0)and(b<(n-n div k)*a)then
   begin
    s:=s+b;
    n:=n div k;
   end
  else
   if n mod k=0 then
    begin
     s:=s+(n-1)*a;
     n:=1;
    end
   else
    {if n-n mod k=0 then
     begin
      s:=s+(n mod k-1)*a;
      n:=n-n mod k;
     end
    else }
     begin
      s:=s+(n mod k)*a;
      n:=n-n mod k;
     end;
 writeln(s);
end.

program CODEFORCES;

var
  n, k, a, b, x, d, y, c, res: cardinal;
begin
  Read(n, k, a, b);
  x := n;
  while (x <> 1) do
  begin
    d := x mod k;
    if d = 0 then
    begin
      y := x div k;
      c := (x - y) * a;
      if b <= c then
        Inc(res, b)
      else
        Inc(res, c);
      x := y;
    end
    else
    begin
      if x >= k then
      begin
        x := x - d;
        Inc(res, d * a);
      end
      else
      begin
        x := 1;
        Inc(res, (d - 1) * a);
      end;
    end;
  end;
  Write(res);
end.

program CODEFORCES;

var
  n, k, a, b, x, d, y, c: cardinal; res:qword;
begin
  Read(n, k, a, b);
  x := n;
  while (x <> 1) do
  begin
    d := x mod k;
    if d = 0 then
    begin
      y := x div k;
      c := (x - y) * a;
      if b <= c then
        Inc(res, b)
      else
        Inc(res, c);
      x := y;
    end
    else
    begin
      if x >= k then
      begin
        x := x - d;
        Inc(res, d * a);
      end
      else
      begin
        x := 1;
        Inc(res, (d - 1) * a);
      end;
    end;
  end;
  Write(res);
end.

var m,n,k,a,b,c,S1,S2:int64;
begin
readln(n);
readln(k);
readln(a);
readln(b);
if k=1 then begin write(a*(n-1)); exit; end;
m:=n;
S2:=(n-1)*a;
while m>1 do
        begin
        if (m mod k=0) and (m>1) then
                begin
                m:=m div k;
                S1:=S1+b;
                end;
        if (m mod k>0) and (m>1) then
                begin
                c:=m mod k;
                S1:=S1+c*a;
                m:=m-c;
                end;
        if m<1 then
                S1:=S1-a;
        if S1>S2 then break;
        end;
if S1>S2 then write(S2)
        else write(S1);
end.

var
  n, k, a, b, x, s: longint;

begin
  read(n, k, a, b);
  x := n;

  while x <> 1 do
  begin
    if x mod k = 0 then begin
      if b < a * (x - x div k) then begin s := s + b;x := x div k; end
      else begin s := s + a * (x - x div k);x := x div k; end; end
    else begin s := s + a * (x mod k);x := x - x mod k;if x=0 then begin x:=1;s:=s-a;end; end;
  end;
  write(s);

end.

Program z1;
var n,k,a,b:longint;
    s:int64;
begin
readln(n);
readln(k);
readln(a);
read(b);
while n>1 do
begin
if n mod k<>0 then  if n<k then begin s:=((n mod k)-1)*A+s;n:=n-(n mod k);end
                           else begin s:=(n mod k)*A+s;n:=n-(n mod k);end
              else if A*(n-(n div k))<B then begin s:=s+A*(n-(n div k));n:=n div k;end
                                       else begin s:=s+B;n:=n div k;end;
end;
write(s);
end.

Program z1;
var n,k,a,b,s:longint;
begin
readln(n);
readln(k);
readln(a);
read(b);
while n>1 do
begin
if n mod k<>0 then  if n<k then begin s:=((n mod k)-1)*A+s;n:=n-(n mod k);end
                           else begin s:=(n mod k)*A+s;n:=n-(n mod k);end
              else if A*(n-(n div k))<B then begin s:=s+A*(n-(n div k));n:=n div k;end
                                       else begin s:=s+B;n:=n div k;end;
end;
write(s);
end.

var n,k,a,b:longint; s:int64;
begin
 readln(n,k,a,b);
 s:=0;
 while n>1 do
  if (k>1)and(n mod k=0)and(b<(n-n div k)*a)then
   begin
    s:=s+b;
    n:=n div k;
   end
  else
   if n mod k=0 then
    begin
     s:=s+(n-1)*a;
     n:=1;
    end
   else
    if n-n mod k=0 then
     begin
      s:=s+(n mod k-1)*a;
      n:=n-n mod k;
     end
    else
     begin
      s:=s+(n mod k)*a;
      n:=n-n mod k;
     end;
 writeln(s);
end.

var n,k,a,b:int64; s:int64;
begin
 readln(n,k,a,b);
 s:=0;
 while n>1 do
  if (k>1)and(n mod k=0)and(b<(n-n div k)*a)then
   begin
    s:=s+b;
    n:=n div k;
   end
  else
   if n mod k=0 then
    begin
     s:=s+(n-1)*a;
     n:=1;
    end
   else
   { if n-n mod k=0 then
     begin
      s:=s+(n mod k-1)*a;
      n:=n-n mod k;
     end
    else  }
     begin
      s:=s+(n mod k)*a;
      n:=n-n mod k;
     end;
 writeln(s);
end.


Program         D_D;
const eps=1e-12;
var i : longint;
	p,n,sum,h : Extended;
        st,k,l,r,Mid : longint;
begin
	readln(n);
	p:= trunc(((n-0.5*sqrt(3))/sqrt(3)));
	p:=p*6;
	st:=0;
	while sqr(st*1.5+4)+sqr(0.5*sqrt(3)*(st mod 2))<=n*n do
        st :=st+1;

        sum:=0;
        for k := st downto 1 do
        begin
                l:=0;
                if k mod 2=0 then r := (k-2) div 2 else
                r := k div 2;
                while l<r do
                begin
                        Mid := (l+R+1) shr 1;
                        if k mod 2=0 then
                        h := 0.5*sqrt(3)+Mid*sqrt(3) else
                        h := Mid*sqrt(3);

                        if n*n>=sqr(h)+sqr((k-1)*1.5+4) then
                        l := Mid else r :=Mid-1;

                end;
                if k mod 2=0 then l := l*2+2 else
                l:=l*2+1;
                sum := sum+l;
        end;
	sum:=sum*6+p+1;
	writeln(sum:0:0);

end.

var
        i,j : longint;

        r,
        kq : int64;
function kc(i,j : int64) : int64;
        begin
                if i and 1 = 1 then begin
                        i := (i+1) >> 1;
                        if j >= i then
                                kc := ( sqr(3*i-1) + 3*sqr(j) ) << 2
                        else
                                kc := sqr(6*i-1) + 3*sqr(j << 1 -1);
                end
                else begin
                        i := i >> 1;
                        if j >= i then
                                kc := sqr(6*i+1) + 3*sqr(j << 1+1)
                        else
                                kc := ( sqr(3*i+1) + 3*sqr(j) ) << 2;
                end;
        end;
begin
        readln(r);
        r := 4*r*r;

        i := 0;
        j := 0;
        while kc(i,j) <= r do inc(j);
        dec(j);

        kq := kq + j << 1 + 1;

        repeat
                inc(i);
                j := j + i and 1;
                while (j >= i and 1) and (kc(i,j) > r) do dec(j);

                if j < i and 1 then break;

                if i and 1 = 1 then
                        kq := kq + j << 2
                else
                        kq := kq + (j << 1 + 1) << 1;
        until false;
        writeln(kq);
end.

{ uses crt; }
 const nn=5000;  eps=0.1;

 label 1;
        type mas=array[0..nn] of longint; mass=^mas;  real=double;
    var  rr,s2,m,l,r,w,i,j,k,k2,t,min,imin:longint;
 u:boolean;
 p0,p,s,n:real;
 x:array[-nn..nn] of longint;
 tt,ot:longint;
flag1, flag:boolean;
ss:array[0..nn]of char;
function sum(a,b:mas):mass; var   c:mas;i,j:integer;
begin  for i:=1 to nn do c[i]:=0;
for i:=nn downto 1 do begin c[i]:=(c[i]+a[i]+b[i]) mod 10; c[i-1]:=(a[i]+b[i]) div 10;    end;
sum:=@c;

  end;

function max(i,j:real):real;
begin max:=i; if j>i then max:=j;end;

function dist(i,j:longint):real;    var dist1,dist2,p1,r1,p2,r2:real;k,m:longint;fl:boolean;
  i1,j1:real;
  begin  i:=abs(i);j:=abs(j); i1:=i;j1:=j;
     if odd(i) then
     begin dist1:=(i1+1)*(i1+1)*3/4+(3*j1-1)*(3*j1-1);
           dist2:=(i1*i1)*3/4+(3*j1-1/2)*(3*j1-1/2);
     end;

     if not odd(i) then
     begin dist1:=(i1+1)*(i1+1)*3/4+(3*j1+1/2)*(3*j1+1/2);
           dist2:=(i1*i1)*3/4+(3*j1+1)*(3*j1+1);
     end;
    dist:=max(dist1,dist2);

  end;
function minj(i:longint):longint;var k,l,a,b,c:longint;
 begin a:=1; b:=trunc(n);
 repeat c:=trunc((a+b)/2); if dist(i,c)<=n*n+eps then a:=c else b:=c until abs(a-b)<=1;
 if dist(i,c)<=n*n then minj:=c else minj:=c-1;
 end;

     begin   { clrscr;
     assign(input,'input.txt'); reset (input); }
  read(n);

 t:=trunc(1.3*n); s:=0;
{ for i:=-t to t do  for j:=-t to t do if (dist(i,j)<=n*n)and not(odd(i)and(j=0)) then
 begin inc(s); end; }
for i:=0 to t do
   begin  p:=0;
      if odd(i) and (dist(i,1)<=n*n+eps) then p:=2*minj(i) else p:=0;
      if not odd(i) then
          begin p:=0;
                if  dist(i,1)<=n*n+eps then p:=2*minj(i)+1 ;
                if  (dist(i,1)>=n*n+eps)and(dist(i,0)<=n*n+eps) then p:=1 ;

          end;
       s:=s+p; if i=0 then p0:=p;
   end;
{  for i:=0 to t do writeln(i,' ',x[i]);
    for i:=0 to t do s:=s+x[i]; }

    {writeln(s); }
    writeln(2*s-p0:0:0);

           end.

{$apptype console}
{$R+,S+,I+,Q+}
const
     inf = 'input.txt';
     outf = 'output.txt';
     eps = 1e-6;
var
 i:longint;
 k,ans,l,r,mid,ans0,mx:int64;
 x,y,x1,y1,x2,y2:extended;

 function check(s,n:int64):boolean;
  begin
   check:=false;
   x:=s*1.5;
   y:=n*sqrt(3);
   if s and 1 = 1 then y:=y+sqrt(3)/2;
   x1:=x+0.5;
   y1:=y+sqrt(3)/2;
   x2:=x+1;
   y2:=y;
   if (sqrt(x1*x1+y1*y1) <= k+eps) and (sqrt(x2*x2+y2*y2) <= k+eps) then check:=true;
  end;

begin
 //reset(input,inf);
 //rewrite(output,outf);
 read(k);
 ans:=0;
 ans0:=0;
 mx:=0;
 for i:=0 to round(1e6) do if i*1.5+1 > k then break else
  begin
   if i > mx then mx:=i;
   l:=-1;
   r:=round(1e6);
   while r-l > 1 do
    begin
     mid:=(r+l) div 2;
     if check(i,mid) then l:=mid else r:=mid;
    end;
   if i = 0 then ans0:=2*l;
   inc(ans,l+1);
  end;
 ans:=ans*4-3-ans0-(mx div 2)*2;
 writeln(ans);
end.

var
ans:int64;
i,k,c:longint;
x,x1,y1,x2,y2:extended;
 begin
  read(k);
  ans:=0;
  c:=2000000;
  for i:=0 to k do
    begin
     x:=3*i+1;
     if x>k then break;
     while true do
       begin
        x1:=x;
        y1:=c*sqrt(3);
        x2:=x-0.5;
        y2:=y1+sqrt(3)/2;
        if x1*x1+y1*y1>int64(k)*k+0.0000001 then c:=c-1 else
        if x2*x2+y2*y2>int64(k)*k+0.0000001 then c:=c-1 else break;
       end;
     ans:=ans+c*2+1;
     if i<>0 then ans:=ans+c*2+1;
    end;

  c:=2000000;
  for i:=0 to k do
     begin
      x:=3*i+2;
      if x+0.5>k then break;
      while true do
        begin
         x1:=x;
         y1:=(c+1)*sqrt(3);
         x2:=x+0.5;
         y2:=y1-sqrt(3)/2;
     //    if c=0 then writeln(x1,' ',y1,' ',x2,' ',y2);
         if x1*x1+y1*y1>int64(k)*k+0.0000001 then c:=c-1 else
         if x2*x2+y2*y2>int64(k)*k+0.0000001 then c:=c-1 else break;
        end;
      ans:=ans+(c*2+2)*2;
     end;
   writeln(ans);
 end.

{$r-,q-,s-,i-,o+}
var
  cd,res,d,x,y,r:int64;
  i:longint;
  function incircle(x,y:int64):boolean;
    begin
      incircle:=x*x+3*y*y<=4*r*r;
    end;
  function ok(x,y:int64):boolean;
    begin
      if (incircle(x+1,y+1))and(incircle(x-1,y+1))and(incircle(x+2,y))and
         (incircle(x-2,y))and(incircle(x+1,y-1))and(incircle(x-1,y-1))
           then ok:=true
           else ok:=false;
    end;
begin
  read(r);
  x:=0;y:=0;
  d:=1;
  while (ok(x,y+2))do begin
    inc(y,2);
    inc(d);
  end;
  res:=d+d-1;
  i:=0;
//  writeln(d);
//  writeln(ok(6,2));
  while (ok(x+3,0))do begin
    inc(i);
    inc(x,3);
    inc(y);
    if (i and 1=0)then inc(d);
//    writeln(x,' ',y,' ',d);
    while (not ok(x,y))do begin
      dec(y,2);
      dec(d);
    end;
    cd:=d+d;
    if (i and 1=0)then cd:=cd-1;
//    writeln(d);
    res:=res+cd*2;
  end;
  writeln(res);
end.

 {uses crt; }
 const nn=1000;

 label 1;
        type mas=array[0..nn] of longint; mass=^mas;
    var  rr,s2,m,l,r,w,p,n,i,j,k,k2,t,min,imin:longint;  c,y:mas;
 u:boolean; s:longint;
 x,ch:array[0..nn] of longint;
 tt,ot:longint;
flag1, flag:boolean;
ss:array[0..nn]of char;
function sum(a,b:mas):mass; var   c:mas;i,j:integer;
begin  for i:=1 to nn do c[i]:=0;
for i:=nn downto 1 do begin c[i]:=(c[i]+a[i]+b[i]) mod 10; c[i-1]:=(a[i]+b[i]) div 10;    end;
sum:=@c;

  end;

function max(i,j:real):real;
begin max:=i; if j>i then max:=j;end;

function dist(i,j:longint):real;    var dist1,dist2,p1,r1,p2,r2:real;k,m:longint;fl:boolean;
  begin  i:=abs(i);j:=abs(j);
     if odd(i) then
     begin dist1:=(i+1)*(i+1)*3/4+(3*j-1)*(3*j-1);
           dist2:=(i*i)*3/4+(3*j-1/2)*(3*j-1/2);
     end;

     if not odd(i) then
     begin dist1:=(i+1)*(i+1)*3/4+(3*j+1/2)*(3*j+1/2);
           dist2:=(i*i)*3/4+(3*j+1)*(3*j+1);
     end;
    dist:=max(dist1,dist2);

  end;
function minj(i:longint):longint;var k,l,a,b,c:longint;
 begin a:=1; b:=n;
 repeat c:=trunc((a+b)/2); if dist(i,c)<=n*n then a:=c else b:=c until abs(a-b)<=1;
 if dist(i,c)<=n*n then minj:=c else minj:=c-1;
 end;

     begin   { clrscr;
     assign(input,'input.txt'); reset (input);
}  read(n);

{ writeln(dist(2,1):0:4);} t:=trunc(1.3*n); s:=0;
 for i:=-t to t do x[i]:=0;
 for i:=-t to t do  for j:=-t to t do if (dist(i,j)<=n*n)and not(odd(i)and(j=0)) then
 begin inc(s); {writeln(i,'    ',j,'  dist= ',dist(i,j):0:3);}end;
{ writeln(s); }
{for i:=-t to t do  writeln(i,'min=',minj(i)); }
for i:=0 to t do
   begin
      if odd(i) and (dist(i,1)<=n*n) then x[i]:=2*minj(i) else x[i]:=0;
      if not odd(i) then
          begin x[i]:=0;
                if  dist(i,1)<=n*n then x[i]:=2*minj(i)+1 ;
                if  (dist(i,1)>=n*n)and(dist(i,0)<=n*n) then x[i]:=1 ;

          end;

   end;
   s:=0;
 { for i:=0 to t do writeln(i,' ',x[i]); }
    for i:=-t to t do s:=s+x[i];

    {writeln(s); }
    writeln(2*s-x[0]);

           end.

{ uses crt; }
 const nn=1000;
 label 1;
        type mas=array[0..nn] of longint; mass=^mas;
    var  rr,s2,m,l,r,w,p,n,i,j,k,k2,t,min,imin:longint;  c,y:mas;
 u:boolean; s:char;
dist:array[0..nn] of real;
 x,ch:array[0..nn] of longint;
 tt,ot:longint;
flag1, flag:boolean;
ss:array[0..nn]of char;
function sum(a,b:mas):mass; var   c:mas;i,j:integer;
begin  for i:=1 to nn do c[i]:=0;
for i:=nn downto 1 do begin c[i]:=(c[i]+a[i]+b[i]) mod 10; c[i-1]:=(a[i]+b[i]) div 10;    end;
sum:=@c;

  end;

function gl(a:char):boolean; var flag:boolean;
begin   flag:=false;
if (a='a')or (a='e')or (a='i')or(a='o')or(a='u') then flag:=true;
gl:=flag;
end;

     begin  {  clrscr;
     assign(input,'input.txt'); reset (input); }
  read(n,m,k);
flag1:=false;

for i:=1 to trunc(sqrt(m+1)) do
  begin  if m mod i=0 then if ((i>=k)and(i<m))or((m div i)>=k)and(m div i<m) then flag1:=true;

  end;

if not odd(n) then flag:=false else if flag1 then flag:=true;

if flag then write('Timur') else write('Marsel');

           end.

{ uses crt; }
 const nn=1000;

 label 1;
        type mas=array[0..nn] of longint; mass=^mas;
    var  rr,s2,m,l,r,w,p,n,i,j,k,k2,t,min,imin:longint;  c,y:mas;
 u:boolean; s:longint;
 x,ch:array[0..nn] of longint;
 tt,ot:longint;
flag1, flag:boolean;
ss:array[0..nn]of char;
function sum(a,b:mas):mass; var   c:mas;i,j:integer;
begin  for i:=1 to nn do c[i]:=0;
for i:=nn downto 1 do begin c[i]:=(c[i]+a[i]+b[i]) mod 10; c[i-1]:=(a[i]+b[i]) div 10;    end;
sum:=@c;

  end;

function max(i,j:real):real;
begin max:=i; if j>i then max:=j;end;

function dist(i,j:longint):real;    var dist1,dist2,p1,r1,p2,r2:real;k,m:longint;fl:boolean;
  begin  i:=abs(i);j:=abs(j);
     if odd(i) then
     begin dist1:=(i+1)*(i+1)*3/4+(3*j-1)*(3*j-1);
           dist2:=(i*i)*3/4+(3*j-1/2)*(3*j-1/2);
     end;

     if not odd(i) then
     begin dist1:=(i+1)*(i+1)*3/4+(3*j+1/2)*(3*j+1/2);
           dist2:=(i*i)*3/4+(3*j+1)*(3*j+1);
     end;
    dist:=max(dist1,dist2);

  end;

     begin   { clrscr;
     assign(input,'input.txt'); reset (input); }
  read(n);

{ writeln(dist(2,1):0:4);} t:=trunc(1.3*n); s:=0;
 for i:=-t to t do x[i]:=0;
 for i:=-t to t do  for j:=-t to t do if (dist(i,j)<=n*n)and not(odd(i)and(j=0)) then
 begin inc(s); {writeln(i,'    ',j,'  dist= ',dist(i,j):0:3);}end;
 writeln(s);

   begin

   end;

           end.

{ uses crt; }
 const nn=1000;  eps=0.1;

 label 1;
        type mas=array[0..nn] of longint; mass=^mas;
    var  rr,s2,m,l,r,w,p,n,i,j,k,k2,t,min,imin:longint;  c,y:mas;
 u:boolean; s:longint;
 x,ch:array[0..nn] of longint;
 tt,ot:longint;
flag1, flag:boolean;
ss:array[0..nn]of char;
function sum(a,b:mas):mass; var   c:mas;i,j:integer;
begin  for i:=1 to nn do c[i]:=0;
for i:=nn downto 1 do begin c[i]:=(c[i]+a[i]+b[i]) mod 10; c[i-1]:=(a[i]+b[i]) div 10;    end;
sum:=@c;

  end;

function max(i,j:real):real;
begin max:=i; if j>i then max:=j;end;

function dist(i,j:longint):real;    var dist1,dist2,p1,r1,p2,r2:real;k,m:longint;fl:boolean;
  begin  i:=abs(i);j:=abs(j);
     if odd(i) then
     begin dist1:=(i+1)*(i+1)*3/4+(3*j-1)*(3*j-1);
           dist2:=(i*i)*3/4+(3*j-1/2)*(3*j-1/2);
     end;

     if not odd(i) then
     begin dist1:=(i+1)*(i+1)*3/4+(3*j+1/2)*(3*j+1/2);
           dist2:=(i*i)*3/4+(3*j+1)*(3*j+1);
     end;
    dist:=max(dist1,dist2);

  end;
function minj(i:longint):longint;var k,l,a,b,c:longint;
 begin a:=1; b:=n;
 repeat c:=trunc((a+b)/2); if dist(i,c)<=n*n+eps then a:=c else b:=c until abs(a-b)<=1;
 if dist(i,c)<=n*n then minj:=c else minj:=c-1;
 end;

     begin   { clrscr;
     assign(input,'input.txt'); reset (input); }
  read(n);

{ writeln(dist(2,1):0:4);} t:=trunc(1.3*n); s:=0;
 for i:=-t to t do x[i]:=0;
 for i:=-t to t do  for j:=-t to t do if (dist(i,j)<=n*n)and not(odd(i)and(j=0)) then
 begin inc(s); {writeln(i,'    ',j,'  dist= ',dist(i,j):0:3);}end;
{ writeln(s);}
{for i:=-t to t do  writeln(i,'min=',minj(i)); }
for i:=0 to t do
   begin
      if odd(i) and (dist(i,1)<=n*n+eps) then x[i]:=2*minj(i) else x[i]:=0;
      if not odd(i) then
          begin x[i]:=0;
                if  dist(i,1)<=n*n+eps then x[i]:=2*minj(i)+1 ;
                if  (dist(i,1)>=n*n+eps)and(dist(i,0)<=n*n+eps) then x[i]:=1 ;

          end;

   end;
   s:=0;
{  for i:=0 to t do writeln(i,' ',x[i]);
 }   for i:=-t to t do s:=s+x[i];

    {writeln(s); }
    writeln(2*s-x[0]);

           end.

var
ans:int64;
i,k,c:longint;
x,x1,y1,x2,y2:extended;
 begin
  read(k);
  ans:=0;
  c:=1000000;
  for i:=0 to k do
    begin
     x:=3*i+1;
     if x>k then break;
     while true do
       begin
        x1:=x;
        y1:=c*sqrt(3);
        x2:=x-0.5;
        y2:=y1+sqrt(3)/2;
        if x1*x1+y1*y1>int64(k)*k+0.000000001 then c:=c-1 else
        if x2*x2+y2*y2>int64(k)*k+0.000000001 then c:=c-1 else break;
       end;
     ans:=ans+c*2+1;
     if i<>0 then ans:=ans+c*2+1;
    end;

  c:=1000000;
  for i:=0 to k do
     begin
      x:=3*i+2;
      if x+0.5>k then break;
      while true do
        begin
         x1:=x;
         y1:=(c+1)*sqrt(3);
         x2:=x+0.5;
         y2:=y1-sqrt(3)/2;
     //    if c=0 then writeln(x1,' ',y1,' ',x2,' ',y2);
         if x1*x1+y1*y1>int64(k)*k+0.000000001 then c:=c-1 else
         if x2*x2+y2*y2>int64(k)*k+0.000000001 then c:=c-1 else break;
        end;
      ans:=ans+(c*2+2)*2;
     end;
   writeln(ans);
 end.

Program 	D_D;
var
f : array[0..1000010] of int64;
i,n : longint;
begin
	readln(n);
	f[1]:=1;f[2]:=1;
	for i := 3 to n do
	f[i]:=f[i-1]+6;
	writeln(f[n]);
end.

Program 	D_D;
var
i,n : longint;
p,st : Extended;
begin
	readln(n);
	p:= trunc((n/sqrt(3))-0.5);
        p:=p*6;

        if n<4 then st:=0 else
        st := trunc((n-4)/1.5)+1;

        st:=(st+1)*st *3;
        st:=st+p+1;
        if n<>0 then
        writeln(st:0:0) else
        writeln(0);

end.

Program 	D_D;
const eps=1e-12;
var
i : longint;
p,st,n : Extended;
begin
	readln(n);
	p:= trunc(((n-0.5*sqrt(3))/sqrt(3)));
        p:=p*6;

        st:=0;
        n:=n-2;
        while sqr(st*0.5*sqrt(3))+sqr(st*1.5+2)<=n*n do st :=st+1;
        st:=(st+1)*st *3;

        st:=st+p+1;
        if n<>0 then
        writeln(st:0:0) else
        writeln(0);

end.

Program 	D_D;
var
i,n : longint;
p,st : int64;
begin
	readln(n);
	p:= trunc((n/sqrt(3))-0.5);
        p:=p*6;

        if n<4 then st:=0 else
        st := trunc((n-4)/1.5)+1;

        st:=(st+1)*st *3;
        writeln(st+p+1);

end.

Program 	D_D;
const eps=1e-12;
var
i : longint;
p,st,n : Extended;
begin
	readln(n);
	p:= trunc(((n-0.5*sqrt(3))/sqrt(3)));
        p:=p*6;

        st:=0;
        while sqr(st*0.5*sqrt(3))+sqr(st*1.5+4)<=(n*n) do st :=st+1;
        st:=(st+1)*st *3;

        st:=st+p+1;
        if n<>0 then
        writeln(st:0:0) else
        writeln(0);

end.

Program 	D_D;
const eps=1e-12;
var
i : longint;
p,st,n : Extended;
begin
	readln(n);
	p:= trunc(((n-0.5*sqrt(3))/sqrt(3))+eps);
        p:=p*6;

        if n<4 then st:=0 else
        st := trunc((n-4)/1.5+eps)+1;

        st:=(st+1)*st *3;
        st:=st+p+1;
        if n<>0 then
        writeln(st:0:0) else
        writeln(0);

end.

Program 	D_D;
const eps=1e-12;
var
i : longint;
p,st,n : Extended;
begin
	readln(n);
	p:= trunc(((n-0.5*sqrt(3))/sqrt(3)));
        p:=p*6;

        st:=0;
        while sqr(st*0.5*sqrt(3))+sqr(st*1.5+2)<=(n*n) do st :=st+1;
        st:=(st+1)*st *3;

        st:=st+p+1;
        if n<>0 then
        writeln(st:0:0) else
        writeln(0);

end.

Program 	D_D;
const eps=1e-12;
var
i : longint;
p,st,n : Extended;
begin
	readln(n);
	p:= trunc(((n-0.5*sqrt(3))/sqrt(3)));
        p:=p*6;

        st:=0;
        n:=n-2;
        while sqr(st*sqrt(3))+sqr(st*1.5+2)<=n*n do st :=st+1;
        st:=(st+1)*st *3;
        st:=st+p+1;
        if n<>0 then
        writeln(st:0:0) else
        writeln(0);

end.

Program 	D_D;
const eps=1e-8;
var
i,n : longint;
p,st : Extended;
begin
	readln(n);
	p:= trunc((n/sqrt(3))-0.5+eps);
        p:=p*6;

        if n<4 then st:=0 else
        st := trunc((n-4)/1.5+eps)+1;

        st:=(st+1)*st *3;
        st:=st+p+1;
        if n<>0 then
        writeln(st:0:0) else
        writeln(0);

end.

Program 	D_D;
const eps=1e-12;
var
i : longint;
p,st,n : Extended;
begin
	readln(n);
	p:= trunc(((n-0.5*sqrt(3))/sqrt(3)));
        p:=p*6;

        st:=0;
        while sqr((st+1)*0.5*sqrt(3))+sqr(st*1.5+4)<=(n*n) do st :=st+1;
        st:=(st+1)*st *3;

        st:=st+p+1;
        if n<>0 then
        writeln(st:0:0) else
        writeln(0);

end.

Program 	D_D;
var
f : array[0..1000010] of int64;
i,n : longint;
p,st : int64;
begin
	readln(n);
	p:= trunc((n/sqrt(3))-0.5);
        p:=p*6;
        if n<4 then st:=0 else
        st := trunc((n-4)/1.5)+1;

        st:=(st+1)*st *3;
        writeln(st+p+1);

end.

{$apptype console}
{$R+,S+,I+,Q+}
const
     inf = 'input.txt';
     outf = 'output.txt';
var
 i:longint;
 k,ans,l,r,mid,ans0,mx:int64;
 x,y,x1,y1,x2,y2:extended;

 function check(s,n:int64):boolean;
  begin
   check:=false;
   x:=s*1.5;
   y:=n*sqrt(3);
   if s and 1 = 1 then y:=y+sqrt(3)/2;
   x1:=x+0.5;
   y1:=y+sqrt(3)/2;
   x2:=x+1;
   y2:=y;
   if (sqrt(x1*x1+y1*y1) <= k) and (sqrt(x2*x2+y2*y2) <= k) then check:=true;
  end;

begin
 //reset(input,inf);
 //rewrite(output,outf);
 read(k);
 ans:=0;
 ans0:=0;
 mx:=0;
 for i:=0 to round(1e6) do if i*1.5+1 > k then break else
  begin
   if i > mx then mx:=i;
   l:=-1;
   r:=round(1e6);
   while r-l > 1 do
    begin
     mid:=(r+l) div 2;
     if check(i,mid) then l:=mid else r:=mid;
    end;
   if i = 0 then ans0:=2*l;
   inc(ans,l+1);
  end;
 ans:=ans*4-3-ans0-(mx div 2)*2;
 writeln(ans);
end.