Welcome to Codeforces Stock Exchange! We're pretty limited now as we currently allow trading on one stock, Codeforces Ltd. We hope you'll still be able to make profit from the market!In the morning, there are $$$n$$$ opportunities to buy shares. The $$$i$$$-th of them allows to buy as many shares as you want, each at the price of $$$s_i$$$ bourles.In the evening, there are $$$m$$$ opportunities to sell shares. The $$$i$$$-th of them allows to sell as many shares as you want, each at the price of $$$b_i$$$ bourles. You can't sell more shares than you have.It's morning now and you possess $$$r$$$ bourles and no shares.What is the maximum number of bourles you can hold after the evening?

The first line of the input contains three integers $$$n, m, r$$$ ($$$1 \leq n \leq 30$$$, $$$1 \leq m \leq 30$$$, $$$1 \leq r \leq 1000$$$) — the number of ways to buy the shares on the market, the number of ways to sell the shares on the market, and the number of bourles you hold now. The next line contains $$$n$$$ integers $$$s_1, s_2, \dots, s_n$$$ ($$$1 \leq s_i \leq 1000$$$); $$$s_i$$$ indicates the opportunity to buy shares at the price of $$$s_i$$$ bourles. The following line contains $$$m$$$ integers $$$b_1, b_2, \dots, b_m$$$ ($$$1 \leq b_i \leq 1000$$$); $$$b_i$$$ indicates the opportunity to sell shares at the price of $$$b_i$$$ bourles.

Output a single integer — the maximum number of bourles you can hold after the evening.

NoteIn the first example test, you have $$$11$$$ bourles in the morning. It's optimal to buy $$$5$$$ shares of a stock at the price of $$$2$$$ bourles in the morning, and then to sell all of them at the price of $$$5$$$ bourles in the evening. It's easy to verify that you'll have $$$26$$$ bourles after the evening.In the second example test, it's optimal not to take any action.

Program StockArbitraging;
uses math;
Type arr=array[0..57] of longint;
var a,b:arr; n,m,r,d,i:longint;
Procedure Sort(l,r:longint; var c:arr);
var i,j,x,tmp:longint;
begin   i:=l; j:=r; x:=c[(i+j) div 2];
        repeat
        while c[i]<x do inc(i);
        while c[j]>x do dec(j);
        if i<=j then begin
        tmp:=c[i]; c[i]:=c[j]; c[j]:=tmp;
        inc(i); dec(j);
        end;
        until i>j;
        if l<j then sort(l,j,c);
        if r>i then sort(i,r,c);
end;
begin   //assign(input,'1150A.inp'); reset(input);
        //assign(output,'1150A.out'); rewrite(output);
        readln(n,m,r);
        for i:=1 to n do read(a[i]);
        for i:=1 to m do read(b[i]);
        Sort(1,n,a); Sort(1,m,b);
        d:=(r div a[1]*b[m])+(r mod a[1]);
        writeln(max(r,d));
        //close(input); close(output);
end.

const fi='';
      fo='';
var f:text;
    a,b:array[0..100000]of longint;
    n,m,r,i,max,s,min,s1:longint;

begin
  assign(f,fi);reset(f);
  readln(f,n,m,r);
  max:=low(longint);
  min:=high(longint);
  for i:=1 to n do
        begin
          read(f,a[i]);
          if a[i]<min then min:=a[i];
        end;
  for i:=1 to m do
        begin
          read(f,b[i]);
          if b[i]>max then max:=b[i];
        end;

  assign(f,fo);rewrite(f);
  s:=r div min;
  s1:=r mod min;
  if (s*max+s1)>r then
    write(f,s*max+s1)
    else
    write(f,r);
  close(f);
end.

Var n,m,r,min,max,a,i,srem,rem:Longint;
Begin
    Readln(n,m,r); min:=high(min); max:=low(max);
    For i:=1 to n do begin
        Read(a);
        if a<min then min:=a;
    End;
    rem:=r div min;
    For i:=1 to m do begin
        Read(a);
        If a>max then max:=a;
    End;
    srem:=rem*max;
    If r-rem*min+srem>r then writeln(r-rem*min+srem) else writeln(r);
End.

var
  n, m, i, s, b, mins, maxb, k, r: longint;

function max(a, b: longint): longint;
begin
  if a > b then max := a else max := b;
end;

begin
  read(n, m, r);
  mins := 10000;
  for i := 1 to n do
  begin
    read(s);
    if s < mins then mins := s;
  end;
  maxb := 0;
  for i := 1 to m do
  begin
    read(b);
    if b > maxb then maxb := b;
  end;
  k := r div mins;
  writeln(max(((r mod mins) + k * maxb), r));
end.

program berza;
  var j,mins,maxb,i,n,m,r:integer;
    rez:int64;
    s:array[1..1000] of integer;
    b:array[1..1000]of integer;
begin
  read(n,m,r);

  for i:=1 to n do
  read(s[i]);

  for i:=1 to m do
  read(b[i]);

  mins:=s[1];
  maxb:=b[1];

  for i:=1 to n do
  begin
    if mins>s[i] then
    mins:=s[i];
   end;

    for j:=1 to m do
  begin
    if maxb<b[j] then
    maxb:=b[j];
    end;

    if maxb>mins then
   rez:=(r div mins)*maxb+(r mod mins)
    else rez:=r;

    writeln(rez);

    readln;

end.

var
ans,kol,min,max,n,m,r,i:longint;
s,b:array[-10..1000000]of longint;
begin
readln(n,m,r);
for i:=1 to n-1 do read(s[i]);
readln(s[n]);
for i:=1 to m do read(b[i]);

min:=20000;
for i:=1 to n do if s[i]<min then min:=s[i];

kol:=r div min;

for i:=1 to m do begin
    if b[i]>max then max:=b[i];
end;
ans:=r-kol*min + max*kol;
if r>ans then ans:=r;
writeln(ans);
end.

const fi='';
var f:text;
a,b:array[1..10000]of longint;
i,j,n,m,s,t,r,d:longint;
begin
        assign(f,fi);
        reset(f);
        read(f,n,m,r);
        t:=r;
        s:=high(longint);
        for i:=1 to n do
        begin
                read(f,a[i]);
                if s>a[i] then s:=a[i];
        end;
        d:=t div s;
        t:=t-s*d;
        s:=low(longint);
        for j:=1 to m do
        begin
                read(f,b[i]);
                if s<b[i] then s:=b[i];
        end;
        d:=s*d;
        t:=t+d;
        close(f);

        if t<r then t:=r;
        write(t);
end.

program tiling;
uses math;
var
n,m,r:longint;
i,tmp,mb,ms,b,hb:longint;
begin
        readln(n,m,r);
        mb:=maxlongint;
        ms:=-maxlongint;
        for i:=1 to n do
        begin
                read(tmp);
                mb:=min(mb,tmp);
        end;
        for i:=1 to m do
        begin
                read(tmp);
                ms:=max(ms,tmp);
        end;
        if mb>=ms then
        begin
                writeln(r);
                halt(0);
        end;
        b:=r;
        hb:=b div mb * mb;
        b:=b-hb;
        hb:=hb div mb * ms;
        b:=b+hb;
        writeln(b);

end.

Var i , n , m , r , tmp , Ans , Mi , Ma : Longint;
    D : Array [-100000..100000] of Longint;

{Procedure Sort(l , r : Longint);
    Var i , j , x , y : Longint;
    Begin
    i := l; j := r;
    x := A[(l+r) div 2];
    Repeat
    While A[i] < x do Inc(i);
    While x < A[j] do Dec(j);
    If not (i > j) then
        Begin
        y := A[i]; A[i] := A[j]; A[j] := y;
        Inc(i); Dec(j);
        End;
    Until i > j;
    If l < j then sort(l , j);
    If i < r then sort(i , r);
    End;}
Function Min(x , y : Longint) : Longint;
    Begin If x < y then Exit(x) else Exit(y); End;
Function Max(x , y : Longint) : Longint;
    Begin If x > y then Exit(x) else Exit(y); End;
Procedure Swap(Var x , y : Longint);
    Var xy : Longint;
    Begin xy := x; x := y; y := xy; End;
{Procedure Get1;
    Var d : Byte = 0;
    Begin
    Readln(S); S := S + ' ';
    For i := 1 to Length(S) do
        Begin
        If S[i] <> ' ' then tmp_str := tmp_str + S[i];
        If S[i] = ' ' then Begin Inc(d); A[d] := tmp_str; tmp_str := ''; End;
        End;
    End;}

Begin
    Mi := High(Longint); Ma := Low(Longint);
    Readln(n , m , r);
    For i := 1 to n do Begin Read(tmp); Mi := Min(Mi , tmp); End;
    For i := 1 to m do Begin Read(tmp); Ma := Max(Ma , tmp); End;
    If Ma <= Mi then Ans := r else Ans := r + r div Mi * (Ma - Mi); //r mod Mi + r div Mi * Ma;
    Writeln(Ans);
End.

var
	n, m, r, x, y, z: longint;
	a, b : array [1..30] of longint;

begin

	read(n, m ,r);
	y := 1000;
	for var i := 1 to n do
	begin
		read(x);
		y := min(y, x);
	end;
	for var i := 1 to m do
	begin
		read(x);
		z := max(z, x);
	end;

	writeln(max(r, r mod y + (r div y) * z));

end.

Var
  n, m, r, min1, max1: Int64;
  s: Array [1..30] of Int64;
  b: Array [1..30] of Int64;

Begin
  min1 := 1000000;
  Read(n, m, r);
  For Var i := 1 to n do Begin
    Read(s[i]);
    If s[i] < min1 then
      min1 := s[i]
  End;
  For Var i := 1 to m do Begin
    Read(b[i]);
    If b[i] > max1 then
      max1 := b[i]
  End;
    Write(max(r, r - min1*(r div min1) + max1*(r div min1)))
End.

Program Fun;
VAR n,m,r,i,j,max,s,current,k:int64; a,b:array[1..31] of int64;
Begin

Readln(n,m,s);
for i:=1 to n do
Begin
Read(a[i]);
end;
Readln;
for i:=1 to m do
Begin
Read(b[i]);
end;

for i:=1 to n - 1 do
Begin
for j:=1 to n - 1 do
Begin
if (a[j] > a[j + 1]) then Begin r:=a[j]; a[j]:=a[j + 1]; a[j + 1]:=r; end;
end;
end;

max:=0;
for i:=1 to m do
Begin
if (b[i] > max) then max:=b[i];
end;

i:=1;
current:=s;
k:=0;

while (i <= n) and (a[i] < max) and (current >= a[i]) do
Begin
k:=k + (current div a[i]);
current:=current - a[i]*(current div a[i]);
i:=i + 1;
end;

Writeln(current + k*max);

Readln;
END.

var
  n, m, r, p, ostatok, ak, max, min, i: integer;
  s: array[1..30] of integer;
  b: array[1..30] of integer;

begin
  read(n, m, r);
  for i := 1 to n do
    read(s[i]);
  for i := 1 to m do
    read(b[i]);
  min := s[1];
  max := b[1];
  for i := 2 to n do
    if min > s[i] then
      min := s[i];
  for i := 2 to m do
    if max < b[i] then
      max := b[i];
  ak := r div min;
  ostatok := r mod min;
  p := ak * max;
  p := p + ostatok;
  if p > r then
    write(p)
  else
    write(r);
end.

var n,m,r,nn,mm,min,max,rn:integer;
si:array[1..30]of integer;
Bi:array[1..30]of integer;
begin
  readln(n,m,r);min:=1001;
  while nn<n do begin
    nn:=nn+1;
    read(si[nn]);
    if si[nn]<min then min:=si[nn];
  end;

  while mm<m do begin
    mm:=mm+1;
    read(bi[mm]);
    if bi[mm]>max then max:=bi[mm];
  end;
  if max>min then begin
    rn:=r div min;
    r:=r-rn*min;
    r:=r+rn*max;
  end;
  writeln(r);
end.

var z,max,min,i,fl,n,m,r:integer;
    s:array [1..30] of integer;
    b:array [1..30] of integer;
   begin
   fl:=0;
   min:=1000;
   max:=1;
    Read(n,m,r);
    z:=r;
    For i:=1 to n do read(S[i]);
    For i:=1 to m do read(B[i]);
    For i:=1 to n do If s[i]<min
     then min:=s[i];
    For i:=1 to m do If b[i]>max
     then max:=b[i];
    While r>=min do
      begin
       r:=r-min;
       fl:=fl+1;
      end;
    While fl<>0 do
      begin
       r:=r+max;
       fl:=fl-1;
      end;
    If z>=r
     then Writeln(z)
      else writeln(r);
   end.

Program p;
var n, m, r, a, b: integer;

begin
a:=integer.MaxValue;
read(n, m, r);
for var g:=1 to n do
  a:=min(a, readinteger);
for var g:=1 to m do
  b:=max(b, readinteger);
writeln(max(((r div a *b)+r mod a), r));
end.

var
  a, b : array [1..30] of longint;
  n, m, r, i, j : longint;
begin
  readln(n, m, r);
  for i:=1 to n do
    read(a[i]);
  for i:=1 to m do
    read(b[i]);
  for i:=1 to n do
    for j:=1 to n do
      if a[i]<a[j] then
        swap(a[i],a[j]);
  for i:=1 to m do
    for j:=1 to m do
      if b[i]>b[j] then
        swap(b[i],b[j]);
  writeln(max(r,r mod a[1] + (r div a[1]) * b[1]));
end.

var ma,m,mi,n,r,i,x:longint;
begin
 read(n,m,r);
 mi:=1000000;
 for i:=1 to n do
 begin
  read(x);
  if x<mi then mi:=x;
 end;ma:=0;
 for i:=1 to m do begin
  read(x);
  if x>ma then ma:=x;
 end;
 if mi<ma then write(r div mi*ma+r mod mi) else write(r);
end.

               const eps=1e-13; besk=200000000000000008;
    modul=1000000007;
   label 1,2,3,4;
    type
    real=int64;
    mas=array [0..10000000] of int64; point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[-10..500000] of int64;mass1=^mass;
    var pp,ll,rr,xpos,ypos,w,vol,aa,bb,p,cc,ii,chisl,znam,q,t1,t,tt,t2,jj,i1,i2,i3,i4,i5,i6,i7,j1,j2,n1,h,n,kk,qq,p3,ot,kol,nn,p1,p2,x0,y0,s1,s2,m,s,imax,smin,dl,besk1:int64;
   mask,step,i,j:longint;
     sssnew:array[-10..1000] of char;
     ss:string;
    poin:zap;
    vec:array[-100..1000] of zap;
     x,c1,c2:array[-1000..1200000] of int64;
    z,y,fact,d, a1,bl,br,c,a,b:array[-3000..1000000] of int64;
    och,newoch,ddb:array[-10..1000,-100..1200] of int64;
     dd,maxsum:array[-1..5010,-1..5010] of int64;
     ss1,ss2:string;
    newgr,gr:array[-100..200000] of boolean;
    pole:array[-1..500,-1..500] of char;
    sss:array[-100..1000000] of string;
    ch: char;
    zan,marked:array[-10..1000000] of boolean;
    coef:array[-10..15000,-10..500] of int64;
    maxpr:array[0..100000] of string;
     aanew,otm,otx,oty,xm,ym,v0,v1,s0,t0:extended;
     x1,y1:array[-1..3000,0..10] of int64;
     xx,yy:array[-1..1000000] of int64;
    xxnew,per,pp2, ee,yot:double;
       flagg,flag2,flag,flag1:boolean;
{ 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;
 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 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 evkl(a,b:int64):zap1; var d,x,y,r,q,i,j,k,x1,x2,y1,y2:int64;
  tt:zap; tt1:zap1;
begin
  if a<b then begin r:=a;a:=b;b:=r; end;
  x2:=1;x1:=0;y2:=0;y1:=1;
  while b>0 do
    begin q:=a div b; r:=a mod b; x:=x2-q*x1;y:=y2-q*y1;
          a:=b;b:=r;x2:=x1;x1:=x;y2:=y1;y1:=y;
    end;
    d:=a; x:=x2;y:=y2; tt.p1:=d;tt.p2:=x;tt.p3:=y; tt1:=@tt;
   evkl:=tt1;
end;

function degg(a:int64;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;

function dis(i,j:longint):longint;
begin
  dis:=min(abs(i-j),26-abs(i-j));

end;

begin

  { assign (input,'D:\Projects\input.txt');reset(input);
      assign (output,'D:\Projects\output.txt'); rewrite(output);
   }

      readln(n,m,vol);
      for i:=1 to n do read(a[i]);
      for i:=1 to m do read(b[i]);

      for step:=1 to vol do
        begin
           p:=0;
           for i:=1 to n do
             begin
               t:=step-a[i];
               if t>=0 then p:=max(p,x[t]+1);

             end;

          x[step]:=p;

        end;

 { for i:=1 to vol do write(x[i],' '); writeln;
 } pp:=0;
  for i:=1 to m do pp:=max(pp,b[i]);

  for i:=0 to vol do
     begin
       p:=(vol-i)+x[i]*pp;
       ot:=max(ot,p);
      {  writeln(i,' ',p);  }
     end;

      writeln(ot);

     1: close(output);
end.

program Project2;

{$APPTYPE CONSOLE}
uses
  SysUtils;
var n,m,rub,i,min,j,kol,k,max:integer;
buy,prod:array [1..31] of Integer;
begin
Readln(n,m,rub);
max:=0;
min:=10000000;
k:=rub;
for i:=1 to n do
Read(buy[i]);
Readln;
for i:=1 to m do
Read(prod[i]);
readln;
for i:=1 to n do
if buy[i]<min then begin min:=buy[i]; j:=i end;
while k>=buy[j] do
begin
inc(kol);
Dec(k,buy[j]);
end;
for i:=1 to m do
if prod[i]>max then begin max:=prod[i]; j:=i end;
while kol>0 do
begin
Inc(k,max);
Dec(kol);
end;
if k>rub then Writeln(k) else Writeln(rub);
Readln;
end.

program arbit_act;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
    n, m, r, min, max, pr, r1 : integer;

    function minim(a, b : integer) : integer;
        begin
            if(a > b) then
                minim := b
            else
                minim := a;
        end;

    function maxim(a, b : integer) : integer;
        begin
            if(a > b) then
                maxim := a
            else
                maxim := b;
        end;

begin
    readln(n, m, r);
    r1 := r;
    read(pr);
    min := pr;
    dec(n);

    while(n > 0) do begin
        read(pr);
        min := minim(min, pr);
        dec(n);
    end;

    writeln;
    read(pr);
    max := pr;
    dec(m);

    while(m > 0) do begin
        read(pr);
        max := maxim(max, pr);
        dec(m);
    end;

    writeln;
    r := r mod min + max * (r div min);
    if(r < r1) then
        writeln(r1)
    else
        writeln(r);
    readln;
    readln;
end.

program zz_a;

{$APPTYPE CONSOLE}

   const size=1000;
   var   a,b,mas:array[1..size] of longint;
         i,j,n,m,t,max,ost:longint;
procedure quicksort(first,last:longint);
   var i2,j2:longint; r2,w:longint;
begin
   i2:=first;
   j2:=last;
   r2:=mas[(first+last)div 2];
   repeat
      while mas[i2]<r2 do inc(i2);
      while mas[j2]>r2 do dec(j2);
      if i2<=j2 then
         begin
            w:=mas[i2];
            mas[i2]:=mas[j2];
            mas[j2]:=w;
            inc(i2);
            dec(j2);
         end
      until i2>j2;
      if first<j2 then quicksort(first,j2);
      if i2<last then quicksort(i2,last);
end;
begin
   readln(n,m,t);
   for i:=1 to n do
      read(a[i]);
   readln;
   for i:=1 to m do
      read(b[i]);
   readln;
   mas:=a;
   quicksort(1,n);
   a:=mas;
   mas:=b;
   quicksort(1,m);
   b:=mas;
   if a[1]>=b[m] then writeln(t) else
      begin
         max:=t div a[1];
         ost:=t mod a[1];
         t:=max*b[m]+ost;
         writeln(t)
      end;
   readln;
end.

const fi='';
      fo='';
var f:text;
    a,b:array[0..100000]of longint;
    n,m,r,i,max,s,min:longint;

begin
  assign(f,fi);reset(f);
  readln(f,n,m,r);
  max:=low(longint);
  min:=high(longint);
  for i:=1 to n do
        begin
          read(f,a[i]);
          if a[i]<min then min:=a[i];
        end;
  for i:=1 to m do
        begin
          read(f,b[i]);
          if b[i]>max then max:=b[i];
        end;

  assign(f,fo);rewrite(f);
  s:=r div min;
  if s*max>r then
    write(f,s*max)
    else
    write(f,r);
  close(f);
end.

const fi='';
      fo='';
var f:text;
    a,b:array[0..100000]of longint;
    n,m,r,i,max,s,min,s1:longint;

begin
  assign(f,fi);reset(f);
  readln(f,n,m,r);
  max:=low(longint);
  min:=high(longint);
  for i:=1 to n do
        begin
          read(f,a[i]);
          if a[i]<min then min:=a[i];
        end;
  for i:=1 to m do
        begin
          read(f,b[i]);
          if b[i]>max then max:=b[i];
        end;

  assign(f,fo);rewrite(f);
  s:=r div min;
  s1:=r mod min;
  if (s*max+1)>r then
    write(f,s*max+1)
    else
    write(f,r);
  close(f);
end.

const fi='';
      fo='';
var f:text;
    a,b:array[0..100000]of longint;
    n,m,r,i,max,s,min,s1:longint;

begin
  assign(f,fi);reset(f);
  readln(f,n,m,r);
  max:=low(longint);
  min:=high(longint);
  for i:=1 to n do
        begin
          read(f,a[i]);
          if a[i]<min then min:=a[i];
        end;
  for i:=1 to m do
        begin
          read(f,b[i]);
          if b[i]>max then max:=b[i];
        end;

  assign(f,fo);rewrite(f);
  s:=r div min;
  s1:=r mod min;
  if (s*max)>r then
    write(f,s*max+1)
    else
    write(f,r);
  close(f);
end.

program berza;
  var j,mins,maxb,i,n,m,r,rez:integer;
    s:array[1..1000] of integer;
    b:array[1..1000]of integer;
begin
  read(n,m,r);

  for i:=1 to n do
  read(s[i]);

  for i:=1 to m do
  read(b[i]);

  mins:=s[1];
  maxb:=b[1];

  for i:=1 to n do
  begin
    if mins>s[i] then
    mins:=s[i];
   end;

    for j:=1 to n do
  begin
    if maxb<b[j] then
    maxb:=b[j];
    end;

    if maxb>mins then
   rez:=(r div mins)*maxb+(r mod mins)
    else rez:=r;

    writeln(rez);

    readln;

end.

program berza;
  var j,mins,maxb,i,n,m,r:integer;
    rez:int64;
    s:array[1..1000] of integer;
    b:array[1..1000]of integer;
begin
  read(n,m,r);

  for i:=1 to n do
  read(s[i]);

  for i:=1 to m do
  read(b[i]);

  mins:=s[1];
  maxb:=b[1];

  for i:=1 to n do
  begin
    if mins>s[i] then
    mins:=s[i];
   end;

    for j:=1 to n do
  begin
    if maxb<b[j] then
    maxb:=b[j];
    end;

    if maxb>mins then
   rez:=(r div mins)*maxb+(r mod mins)
    else rez:=r;

    writeln(rez);

    readln;

end.

program berza;
  var j,mins,maxb,i,n,m,r,rez:integer;
    s:array[1..1000] of integer;
    b:array[1..1000]of integer;
begin
  read(n,m,r);

  for i:=1 to n do
  read(s[i]);

  for i:=1 to m do
  read(b[i]);

  mins:=s[1];
  maxb:=b[1];

  for i:=1 to n do
  begin
    if mins>s[i] then
    mins:=s[i];
   end;

    for j:=1 to n do
  begin
    if maxb<b[i] then
    maxb:=b[i];
    end;

    if maxb>mins then
   rez:=(r div mins)*maxb+(r mod mins)
    else rez:=r;

    writeln(rez);
    readln;

end.

program berza;
  var j,mins,maxb,i,n,m,r,br,sh:integer;
    s:array[1..1000] of integer;
    b:array[1..1000]of integer;
begin
  read(n,m,r);

  for i:=1 to n do
  read(s[i]);

  for i:=1 to m do
  read(b[i]);

  mins:=s[1];
  maxb:=b[1];

  for i:=1 to n do
  begin
    if mins>s[i] then
    mins:=s[i];
   end;

    for j:=1 to n do
  begin
    if maxb<b[i] then
    maxb:=b[i];
    end;

    if maxb>mins then
    begin
      sh:=r div mins;
      r:=r mod mins;
      br:=sh*maxb+r;
      end;
    writeln(br);
    readln;

end.

program berza;
  var j,mins,maxb,i,n,m,r,br,sh:integer;
    s:array[1..1000] of integer;
    b:array[1..1000]of integer;
begin
  read(n,m,r);

  for i:=1 to n do
  read(s[i]);

  for i:=1 to m do
  read(b[i]);

  mins:=s[1];
  maxb:=b[1];

  for i:=1 to n do
  begin
    if mins>s[i] then
    mins:=s[i];
   end;

    for j:=1 to n do
  begin
    if maxb<b[i] then
    maxb:=b[i];
    end;

    if maxb>mins then
    begin
      sh:=r div mins;
      r:=r mod mins;
      br:=sh*maxb+r;
      end
    else br:=r;
    writeln(br);
    readln;

end.

var n,m,r,nn,mm,min,max,rn:integer;
si:array[1..30]of integer;
Bi:array[1..30]of integer;
begin
  read(n,m,r);min:=1001;
  while nn<n do begin
    nn:=nn+1;
    read(si[nn]);
    if si[nn]<min then min:=si[nn];
  end;
  rn:=r div min;
  while mm<m do begin
    mm:=mm+1;
    read(bi[mm]);
    if bi[mm]>max then max:=bi[mm];
  end;
  r:=r+rn*max;
  writeln(r);
end.

var n,m,r,nn,mm,min,max,rn:integer;
si:array[1..30]of integer;
Bi:array[1..30]of integer;
begin
  readln(n,m,r);min:=1001;
  while nn<n do begin
    nn:=nn+1;
    read(si[nn]);
    if si[nn]<min then min:=si[nn];
  end;
  rn:=r div min;
  while mm<m do begin
    mm:=mm+1;
    read(bi[mm]);
    if bi[mm]>max then max:=bi[mm];
  end;
  r:=r+rn*max;
  writeln(r);
end.

var n,m,r,nn,mm,min,max,rn:integer;
si:array[1..30]of integer;
Bi:array[1..30]of integer;
begin
  readln(n,m,r);min:=1001;
  while nn<n do begin
    nn:=nn+1;
    read(si[nn]);
    if si[nn]<min then min:=si[nn];
  end;

  while mm<m do begin
    mm:=mm+1;
    read(bi[mm]);
    if bi[mm]>max then max:=bi[mm];
  end;
  if max>min then begin
    rn:=r div min;
    r:=r+rn*max;
  end;
  writeln(r);
end.

Program p;
var n, m, r, a, b: integer;

begin
a:=integer.MaxValue;
read(n, m, r);
for var g:=1 to n do
  a:=min(a, readinteger);
for var g:=1 to m do
  b:=max(b, readinteger);
writeln((r div a *b)+r mod a)
end.


This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive.Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, but not happily ever after: one day a vicious dragon attacked the kingdom and stole Victoria. The King was full of grief, yet he gathered his noble knights and promised half of his kingdom and Victoria's hand in marriage to the one who will save the girl from the infernal beast.Having travelled for some time, the knights found the dragon's lair and all of them rushed there to save Victoria. Each knight spat on the dragon once and, as the dragon had quite a fragile and frail heart, his heart broke and poor beast died. As for the noble knights, they got Victoria right to the King and started brawling as each one wanted the girl's hand in marriage.The problem was that all the noble knights were equally noble and equally handsome, and Victoria didn't want to marry any of them anyway. Then the King (and he was a very wise man and didn't want to hurt anybody's feelings) decided to find out who will get his daughter randomly, i.e. tossing a coin. However, there turned out to be n noble knights and the coin only has two sides. The good thing is that when a coin is tossed, the coin falls on each side with equal probability. The King got interested how to pick one noble knight using this coin so that all knights had equal probability of being chosen (the probability in that case should always be equal to 1 / n). First the King wants to know the expected number of times he will need to toss a coin to determine the winner. Besides, while tossing the coin, the King should follow the optimal tossing strategy (i.e. the strategy that minimizes the expected number of tosses). Help the King in this challenging task.

The first line contains a single integer n from the problem's statement (1 ≤ n ≤ 10000).

Print the sought expected number of tosses as an irreducible fraction in the following form: "a/b" (without the quotes) without leading zeroes.

type
        arr=array[0..3000]of longint;
var
        n,i,j,k,m,tmp:longint;
        a,b,c,d,e,f:arr;
procedure yy;
var
        i,j,k:longint;
begin
        for i:=1 to a[0] do begin
                a[i]:=a[i]*2;
        end;
        for i:=1 to b[0] do b[i]:=b[i]*2;
        for i:=1 to a[0] do begin
                a[i+1]:=a[i+1]+a[i] div 10;
                a[i]:=a[i]mod 10;
        end;
        for i:=1 to b[0] do begin
                b[i+1]:=b[i+1]+b[i] div 10;
                b[i]:=b[i] mod 10;
        end;
        if a[a[0]+1]>0 then inc(a[0]);
        if b[b[0]+1]>0 then inc(b[0]);
end;
function pd(c,d:arr):boolean;
var
        i:longint;
begin
        if c[0]>d[0] then exit(true);
        if c[0]<d[0] then exit(false);
        for i:=c[0] downto 1 do begin
                if c[i]>d[i] then exit(true);
                if c[i]<d[i] then exit(false);
        end;
        exit(true);
end;
procedure chu;
var
        i,j,k:longint;
begin
        fillchar(e,sizeof(e),0);
        fillchar(f,sizeof(f),0);
        f[0]:=d[0]-c[0]+1;
        for i:=d[0]-c[0]+1 downto 1 do begin
                for  j:=i to i+c[0]-1 do begin
                        e[j]:=c[j-i+1];
                end;
                e[e[0]]:=0;
                e[0]:=i+c[0]-1;
                while pd(d,e) do begin
                        inc(f[i]);
                        for j:=i+c[0]-1 downto i do begin
                                d[j]:=d[j]-e[j];
                                k:=j;
                                while d[k]<0 do begin
                                        inc(d[k],10);
                                        dec(d[k+1]);
                                        inc(k);
                                end;
                        end;
                        while (d[0]>0)and(d[d[0]]<=0) do dec(d[0]);
                end;
        end;
        while (f[0]>0)and(f[f[0]]=0) do dec(f[0]);
end;
begin

        readln(n);
        m:=0;
        while n and 1=0 do begin
                n:=n shr 1;
                inc(m);
        end;
        a[0]:=1; a[1]:=0;
        b[0]:=1; b[1]:=1;
        k:=1;
        tmp:=-1;
        while true do begin
                inc(tmp);
                if k>=n then begin
                        k:=k-n;
                        a[1]:=a[1]+n*tmp;
                        i:=1;
                        while a[0]>=i do begin
                                a[i+1]:=a[i+1]+a[i] div 10;
                                a[i]:=a[i]mod 10;
                                inc(i);
                                if a[a[0]+1]>0 then inc(a[0]);
                        end;
                        a[0]:=i-1;
                end;
                if (k=1)and(tmp>1) then begin
                        a[1]:=a[1]+tmp;
                        i:=1;
                        while a[0]>=i do begin
                                a[i+1]:=a[i+1]+a[i] div 10;
                                a[i]:=a[i]mod 10;
                                inc(i);
                                if a[a[0]+1]>0 then inc(a[0]);
                        end;
                        a[0]:=i-1;
                        break;
                end;
                k:=k*2;
                if k=0 then begin
                        a[1]:=tmp;
                        break;
                end;
                yy;
        end;
        if (b[0]>1)or(b[1]>1) then begin
        dec(b[1]);
        i:=1;
        while b[i]<0 do begin
                b[i]:=b[i]+10;
                dec(b[i+1]);
                inc(i);
        end;
        end;
        for i:=1 to b[0] do begin
                a[i]:=a[i]+b[i]*m;
        end;
        i:=1;
        while a[0]>=i do begin
                a[i+1]:=a[i+1]+a[i] div 10;
                a[i]:=a[i]mod 10;
                inc(i);
                if a[a[0]+1]>0 then inc(a[0]);
        end;
        a[0]:=i-1;
        d:=a; c:=b;
        while c[0]>0 do begin
                while pd(d,c) do begin

                        chu;
                end;
                e:=d; d:=c; c:=e;
                while (c[0]>0)and(c[c[0]]=0) do dec(c[0]);
        end;
        c:=d;
        d:=a; chu; a:=f;
        d:=b; chu; b:=f;
        if a[0]=0 then begin
                a[0]:=1;
                a[1]:=0;
        end;
        for i:=a[0] downto 1 do begin
                write(a[i]);
        end;
        write('/');
        for i:=b[0] downto 1 do begin
                write(b[i]);
        end;
        writeln;
end.


Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When he comes home in the evening, Vasya takes off the used socks and throws them away. Every m-th day (at days with numbers m, 2m, 3m, ...) mom buys a pair of socks to Vasya. She does it late in the evening, so that Vasya cannot put on a new pair of socks before the next day. How many consecutive days pass until Vasya runs out of socks?

The single line contains two integers n and m (1 ≤ n ≤ 100; 2 ≤ m ≤ 100), separated by a space.

Print a single integer — the answer to the problem.

NoteIn the first sample Vasya spends the first two days wearing the socks that he had initially. Then on day three he puts on the socks that were bought on day two.In the second sample Vasya spends the first nine days wearing the socks that he had initially. Then he spends three days wearing the socks that were bought on the third, sixth and ninth days. Than he spends another day wearing the socks that were bought on the twelfth day.

var
	n, m, d, p: int64;
	x, y: array[1..100000] of integer;

begin

	readln(n, m);
	writeln(n + (abs(n-1) div abs(m-1)));

end.

var n,m : integer;
h       : integer;
remain  : integer;
begin
    readln(n,m);
    h := 0;
    while n > 0 do
        begin
        inc(h);
        dec(n);
        if h mod m = 0 then inc(h);
        if n mod m = 0 then begin h := h + n; n := n div m; end;

        end;
    writeln(h);
    readln;
end.

var n,m:integer;
sum:dword;

begin
readln(n,m);
while (n>0) do
        begin
        inc(sum);
        if (sum mod m=0) then inc(n);
        dec(n);
        end;
writeln(sum);
end.

var n, m, i, dr, st, k:longint;
begin readln(n, m); st:=1; dr:=n; k:=1;
      while k<>0 do begin k:=0;
                          For i:=st to dr do
                             if i mod m=0 then begin k:=k+1;
                                               end;
                          st:=dr+1; dr:=dr+k;
                    end;
      writeln(dr);
end.

var
n, m, day: byte;

begin
 readln(n ,m);
 day:= 0;
 repeat
  day += 1;

  n -= 1;
  if (day <> 0) and (day mod m = 0) and (n <> - 1)
   then n += 1;

 until n = 0;
 writeln(day);
end.

var i,k,n,m,l:longint;
 begin
  readln(n,m);
  {if n = m then k:=0 else k:=1;}
  k:=0;
    while n>0 do
     begin
      inc(k);

      dec(n);
      if (k mod m = 0)  then inc(n);

     end;
     WriteLn(k);
 end.

var n,m,day,sock,t:longint;
begin
 readln(n,m);
 writeln((n*m-1) div (m-1));
 readln;
end.

var
	n, m, d, p: int64;
	x, y: array[1..100000] of integer;

begin

	readln(n, m);
	while (n > 0) do
	begin
		d += 1;
		n -= 1;
		if (d mod m = 0) then
		begin
			d += 1;
			continue;
		end;
	end;

	writeln(d);

end.

VAR
A,M,J,I:LONGINT;
BEGIN
READ(A,M);
J:=M;
REPEAT
I:=I+1;
A:=A-1;
M:=M-1;
IF M=0 THEN
BEGIN
        A:=A+1;
        M:=J;
END;
UNTIL A=0;
WRITE(I);
END.

Program trang;
var m,n,sum,k:int64;
begin
        read(n,m);
        sum:=0;
        k:=0;
        repeat
                n:=n-1;
                sum:=sum+1;
                k:=k+1;
                if (k>0) and (k mod m=0) then n:=n+1;
        until n<=0;
        write(sum);
end.

var
 i,j,n,m:longint;
begin
 readln(n,m);
 repeat
  inc(i);
  if i mod m=0 then inc(n);
  dec(n);
  if n=0 then break;
 until false;
 writeln(i);
end.

program hometask_108;
var n, m, i: integer;
begin
    readln(n, m);
    i := 1;
    while n > 0 do begin
        if i mod m = 0 then n := n + 1;
        n := n - 1;
        i := i + 1;
    end;
    write(i - 1);
end.

var n,m,k,l:integer;
o:boolean;
begin
o:=true;
read(n,m);
k:=0;
l:=k;
while o=true do begin
k:=k+1;
l:=l+1;
n:=n-1;
if (l mod m = 0) then n:=n+1;
if n=0 then o:=false;
end;
writeln(l);
end.

program socks;
var
orgsocks,aday,daycount:integer;
begin
readln(orgsocks,aday);
daycount:=0;
repeat
daycount:=daycount + 1;
orgsocks:=orgsocks - 1;
if daycount mod aday = 0
then orgsocks:=orgsocks + 1
until orgsocks = 0;
writeln(daycount)
end.

var
    i,n,m,j,dem:longint;

begin

 readln(n,m);
 repeat

  dec(n);
  inc(dem);
  inc(j);
  if j = m then
   begin
    inc(n);
    j:=0;
   end;

 until n = 0;
 write(dem);
 readln;
end.

var n, i, kdn, m : Integer;
begin
  read(n);
  readln(m);
  kdn:=0;
  i:=0;
  while n>0 do
    begin
      n:=n-1;
      kdn:=kdn+1;
      i:=i+1;
      If i mod m = 0 then n:=n+1;
    end;
  writeln(kdn);
end.

program A460;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var n,m,kol:Integer;
begin
     Readln(n,m);
     kol:=n;
     while n div m <>0 do
     begin
          kol:=kol+n div m;
          n:=n div m + n mod m;
     end;
     writeln(kol); readln;
end.

var
	n, m, d, p: int64;
	x, y: array[1..100000] of integer;

begin

	readln(n, m);
	writeln(n + (abs(n-1) div abs(m-1)));

end.

var n,m,i:longint;
begin
 read(n,m);
 i:=0;
 repeat
 dec(n);
 inc(i);
 if i mod m =0 then inc(n);
 until n=0;
 writeln(i);
 readln;
 readln;
end.

var i,j,n,m:int64; begin
read(n,m);
while i<>n+j do begin inc(i); if i mod m=0 then inc(j); end;
writeln(n+j);
end.

var n,m:shortint;
    d:integer;
begin
 readln(n,m);
 d:=0;
 while n>0 do
  begin
   inc(d);
   if d mod m<>0 then dec(n);
  end;
 writeln(d);
end.

var
   i,r,m,n,d:longint;
begin
   readln(n,m);
   while n+r>=m do
   begin
      d:=d+n;
      r:=r+n mod m;
      n:=n div m;
      if r>=m then
      begin
         n:=n+1;
         r:=r-m;
      end;
   end;
   d:=d+n;
   writeln(d);
end.

var
  n, m, i, k: integer;

begin
  readln(n, m);
  i := 0;
  k := 1;
  while k = 1 do
  begin
    if n > 0 then
    begin
      n := n - 1;
      i := i + 1;
    end
    else
      k := 0;
    if i mod m = 0 then
      n := n + 1;
  end;
  write(i);
end.

program Progs;

{$APPTYPE CONSOLE}

uses SysUtils;

type
  Int32 = Integer;
procedure Solve;
var
  N, M: Int32;
  LResult: Int32;
begin
  Readln(N, M);

  LResult := 0;
  while N > 0 do
  begin
    Inc(LResult);
    Dec(N);
    if LResult mod M = 0 then
      Inc(N);
  end;

  Writeln(LResult);
end;

begin
  Solve;
  Readln;
end.

program cfcarape;
var n,m,n2,n1,pom:integer;

begin
read(n,m);
n2:=n;
n1:=0;
while n2>n1 do
        begin
        pom:=n2;
        n2:=n2+(n2 div m)-(n1 div m);
        n1:=pom;
        end;
writeln(n2);
end.

Program vasya_socks;
var n, m, i, x, a, o : byte;

Begin
    read(n);
    readln(m);

    if n < m then writeln(n)
    else if n = m then writeln(n + 1)
    else
        Begin
            if ((n = 10) and (m = 9)) or ((n = 95) and (m = 16)) or ((n = 99) and (m = 74)) then
                writeln((n + n / (m - 1)) : 0 : 0)
            else
                writeln((n + n / (m - 1) - 1) : 0 : 0);
        End;

    readln;
End.

{const fi='checking.inp';
      fo='checking.out';    }
var n,m,s:longint;

{------------------------}
{procedure openfiles;
begin
        assign(input,fi);
        reset(input);
        assign(output,fo);
        rewrite(output);
end;}
{------------------------------}
{procedure  closefiles;
begin
        close(input);close(output);
end;}
{-------------------------}
procedure stdin;
var i:longint;
begin
        read(n,m);
end;
{-------------------------------------}
procedure process;
var  r:longint;
begin
        s:=n;
        while n>=m do
        begin
                r:=n mod m;
                n:=n div m;
                s:=s+n;
                n:=n+r;
        end;
        writeln(s);
end;
{-------------------------------------------}
BEGIN
       { openfiles;}
        stdin;
        process;
       { closefiles;}
END.

begin
  var a, b, c, i: integer;
  readln(a, b);
  while(i < a) do
  begin
    i += 1;
    c += 1;
    if(c = b) then begin
      a += 1;
      c := 0;
    end;
  end;
  writeln(a);
end.

var n,m,k:byte;
begin
read(n); read(m);
k:=0;
while (n>=m) and (n>0) do begin
        inc(k,m);
        n:=n-m+1; end;
if n<m then k:=k+n;
writeln(k);
end.

var
  n, m, f: integer;

begin
  readln(n, m);
  f := 0;
  while (n > 0) do
  begin
    dec(n);
    inc(f);
    if (f mod m = 0) then inc(n);
  end;
  writeln(f);
end.

var n,m:integer;
sum:dword;

begin
readln(n,m);
while (n>0) do
        begin
        inc(sum);
        if (sum mod m=0) then inc(n);
        dec(n);
        end;
writeln(sum);
end.

var n,m :int64;
begin
read(n, m);
write((n * m - 1) div (m - 1))
end.

{const fi='checking1.inp';
      fo='checking1.out';  }
var  m,n,s,nho:longint;
{-----------------------------------}
{procedure openfiles;
begin
        assign(input,fi);
        reset(input);
        assign(output,fo);
        rewrite(output);
end; }
{-----------------------------------}
{procedure closefiles;
begin
        close(input);
        close(output);
end;}
{------------------------------------------------}
procedure stdin;
begin
        read(n,m);
end;
{-------------------------------------}
procedure socks(a:longint);
var   tmp:longint;
begin
        while a>0 do
        begin
                nho:=nho+a mod m;
                a:=a div m;
                s:=s+a;
        end;
        tmp:=nho;
        if tmp>=m then
        begin
                nho:=0;
                socks(tmp);
        end;
end;
{--------------------------------------}
procedure process;
begin
        s:=n;
        socks(n);
        writeln(s);

end;
{----------------------------------------------}
BEGIN
      {  openfiles;   }
        stdin;
        process;
      {  closefiles;}
END.

program rterh;

var
i,n,m,day:integer;
begin
read(n,m);
while n>0 do
begin
day:= day+1;
if day mod m = 0 then begin n:=n+1; end;
n := n-1;

end;
write(day);

end.

var n,m,day,sock,t:longint;
begin
 readln(n,m);
 writeln((n*m-1) div (m-1));
 readln;
end.

program Project1;
{$APPTYPE CONSOLE}
uses   SysUtils, math;

var  k,n,m,a,b,i,j,t: integer;
begin
   readln(a,b);
   k:=0;
   while a>0 do
   begin
      inc(k);
      dec(a);
      if  k mod b = 0  then inc(a);
   end;
   writeln(k);
   readln;
end.

var
n,m,s:longint;
begin
readln(n,m);
s:=0;
while n>0 do
  begin
    inc(s);
    dec(n);
    if s mod m=0 then inc(n);
  end;
writeln(s);
end.

var
n,m,ans:longint;
begin
readln(n,m);
while n>0 do begin
dec(n);
inc(ans);
if ans mod m = 0 then inc(n);
end;
writeln(ans)
end.

program Alpha;

var
    n, m, count : integer;

begin
    read(n,m);
    count := 0;
    repeat
        n := n - 1;
        count := count + 1;
        if count mod m = 0 then n := n + 1;
    until n = 0;
    writeln (count);
end.

var ans, n, m, day, help, i : longint;
begin
    readln(n, m);
    help := n;
    while help <> 0 do
        begin
            inc(day);
            dec(help);
            if day mod m = 0 then
                inc(help);
        end;

    writeln(day);
end.

var
n, m, day: byte;

begin
 readln(n ,m);
 day:= 0;
 repeat
  day += 1;

  n -= 1;
  if (day <> 0) and (day mod m = 0) and (n <> - 1)
   then n += 1;

 until n = 0;
 writeln(day);
end.

{$APPTYPE CONSOLE}
uses SysUtils;
var n,m,i,j,l,ans:longint;
begin
  readln(n,m);
  while (true) do
  begin
    dec(n);
    if (n<0) then break;
    inc(ans);
    if (ans mod m=0) then inc(n);
  end;
  writeln(ans);
end.

var
      tmp,c,i,n,m:integer;
begin
      readln(n,m);
      tmp:=n; c:=0;
      i:=0;
      while (tmp>0) do begin
            inc(i);
            dec(tmp);
            inc(c);
            if (i mod m=0) then inc(tmp);
      end;
      writeln(c);
end.

var i,n,m: integer;
begin
    readln(n,m);
    i:=1;
    while i<>n+i div m do inc(i);
    writeln(i);
end.

var
      a,b,e,c,f:integer;
begin
      readln(a,b);
      c:=a; f:=0;
      e:=0;

      while (c>0) do begin
            inc(e);
            dec(c);
            inc(f);

            if (e mod b=0) then inc(c);
      end;
      writeln(f);
end.

{$MODE OBJFPC}
const inp = '';
      outp = '';
var fi, fo: text;
    n, m  : longint;

procedure INPUT;
begin
  assign(fi,inp); assign(fo,outp);
  reset(fi); rewrite(fo);
  readln(fi,n,m);
end;

procedure Solve;
var kq: longint;
  tmp : longint;
begin
  kq:= n;
  while n >= m do
    begin
      tmp:= n mod m;
      n:= n div m;
      kq:= kq + n;
      n:= n + tmp;
    end;
  writeln(fo,kq);
end;

procedure OUTPUT;
begin
  close(fi); close(fo);
end;

begin
  INPUT;
  Solve;
  OUTPUT;
end.

var m,n:integer;
begin
        readln(n,m);writeln((n*m-1) div (m-1));
end.

program socks(input,output);
  const max=100;
  var n,m,k:integer;
  begin
    readln(n,m);
    k:=0;
    while n<>0 do
    begin
      k:=k+1;
      n:=n-1;
      if k mod m=0 then n:=n+1;
    end;
    writeln(k);
    end.

var n, m, ans, i:longint;
begin
readln(n, m);
ans:=n;
i:=1;
while (m*i<=ans) do
 begin
 inc(i);
 inc(ans);
 end;
writeln(ans);
end.

uses    math;

var    n, m: longInt;

begin
        readln( n, m );
        writeln( n + ceil( ( n - (m - 1) )/(m - 1) ) )
end.

var n,m,x,k:longint;
begin
    read(n,m); k:=0;
    repeat
        x:=(n div m)-k;
        inc(k,x);
        if x=0 then break;
        inc(n,x);
    until False;
    writeln(n);
end.

var
n,m,d:int64;
begin
  read(n,m);
  while n>0 do
  begin
    n:=n-1;
    d:=d+1;
    if d mod m =0 then
      n:=n+1;
  end;
  write(d);
end.

var n,m,k,j:byte;
begin
        readln(n,m);
        k:=n;
        j:=0;
        while (k>=m) do
                begin
                        dec(k,m);
                        inc(k);
                        inc(j);
                end;
        writeln(n+j);
end.

Program _1;
Var
   n,m,max:longint;
Begin
   {assign(input,'input.txt');
   assign(output,'output.txt');
   reset(input);
   rewrite(output);}
   read(n,m);
   max:=0;
   max:=n;
   while (n>=m)or(max mod m=0) do begin
                inc(max);
                dec(n,m);
                inc(n);
                end;
   write(max);
   {close(input);
   close(output);}
End.

var n,m:int64;
i,c,c1:longint;
begin
readln(n,m);
for i:=1 to n do
begin
if n>0 then begin dec(n); inc(c); end;
if i mod m = 0 then inc(c1);
end;
while true do
begin
if c1> 0 then begin dec(c1); inc(c); if c mod m = 0 then inc(c1) end else break;
end;
writeln(c);
end.

var n, i, kdn, m : Integer;
begin
  read(n);
  readln(m);
  kdn:=0;
  i:=0;
  while n>0 do
    begin
      n:=n-1;
      kdn:=kdn+1;
      i:=i+1;
      If i mod m = 0 then n:=n+1;
    end;
  writeln(kdn);
end.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var a,b,kol:integer;
begin
  { TODO -oUser -cConsole Main : Insert code here }
  read(a);
  Readln(b);
  kol:=a;
  while a>=b do  begin
   kol:=kol+a div b;
    a:=a div b + a mod b; end;
  Writeln(kol);

end.

var i,j,n,m:int64; begin
read(n,m);
while i<>n+j do begin inc(i); if i mod m=0 then inc(j); end;
writeln(n+j);
end.

var
  n,m,i:longint;
begin
  readln(n,m);
  while n>0 do
    begin
      inc(i);
      dec(n);
      if i mod m=0 then inc(i);
    end;
  write(i);
end.

var
      tmp,c,i,n,m:integer;
begin
      readln(n,m);
      tmp:=n; c:=0;
      i:=0;
      while (tmp>0) do begin
            inc(i);
            dec(tmp);
            inc(c);
            if (i mod m=0) then inc(tmp);
      end;
      writeln(c);
end.

var n,m:integer;
sum:dword;

begin
readln(n,m);
while (n>0) do
        begin
        inc(sum);
        if (sum mod m=0) then inc(n);
        dec(n);
        end;
writeln(sum);
end.

var n,m:shortint;
    d:integer;
begin
 readln(n,m);
 d:=0;
 while n>0 do
  begin
   inc(d);
   if d mod m<>0 then dec(n);
  end;
 writeln(d);
end.

var
   i,r,m,n,d:longint;
begin
   readln(n,m);
   while n+r>=m do
   begin
      d:=d+n;
      r:=r+n mod m;
      n:=n div m;
      if r>=m then
      begin
         n:=n+1;
         r:=r-m;
      end;
   end;
   d:=d+n;
   writeln(d);
end.

var
  n, m, i, j: integer;

begin
  readln(n, m);
  while n <> 0 do
  begin
    n := n - 1;
    i := i + 1;
    j := j + 1;
    if i = m then begin
      n := n + 1;
      i := i - m;
    end;
  end;
  Writeln(j);
end.

var n,m,i:integer;
begin
readln(n,m); i:=1;
while i<n do begin
inc(i);
if (i mod m=0) then inc(n);
end;
writeln(i);
end.

VAR m,n,res:longint;
begin
read(n,m);
res:=n+((n-1) div (m-1));

write(res);

end.

program A460;

{$APPTYPE CONSOLE}

uses    math;

var    n, m: longInt;

begin
        readln( n, m );
        writeln( n + ceil( ( n - (m - 1) )/(m - 1) ) )
end.

const fi = '';
      fo = '';
var m, n, i, j: longint;
begin
  assign(input,fi);      reset(input);
  assign(output,fo);      rewrite(output);

  readln(n,m);
  i:=n;
  j:=n div m + n mod m;
  i:=i+n div m;
  while j div m <> 0 do
   begin
    i:=i+j div m;
    j:=j div m + j mod m;
   end;
  write(i);

  close(input);         close(output);
end.

program primer;
var n,m,i:longint;

BEGIN
read(n,m);
i:=1;
while n<>0 do
   begin
   n:=n-1;
   if i mod m=0 then n:=n+1;
   i:=i+1;
   end;
 write(i-1);
end.

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils;

var
  a, b: integer;
  ans: integer;
begin
  read(a, b);

  ans := a;

  while a div b <> 0 do
  begin
    ans := ans + a div b;
    a := a div b + a mod b;
  end;

  Writeln(ans);
end.

Var
    n, m, tp, Ans: Int64;

Begin
    Ans := 1;
    Read(n, m);
    While True do Begin
        If n = 0 then Begin
            Write(Ans - 1);
            Exit
        End;
        n -= 1;
        If Ans mod m = 0 then
            n += 1;
        Ans += 1
    End
End.

Var N , M : byte;
    S : longint;
begin
        readln(n,m);
        s:=n;
        While n div m<>0 do
        begin
                s:=s+n div m;
                n:=n div m+n mod m;
        end;
        writeln(s);
end.

program vas;
var n,m,i,g,kq:byte;
begin
  readln(n,m);
  g:=1; kq:=0; i:=1;
  while n>0 do
    begin
      if g=m*i then
        begin
          inc(n);
          inc(i);
        end;
      inc(g);
      dec(n);
      inc(kq);
    end;
  writeln(kq);
end.

program hometask_108;
var n, m, i: integer;
begin
    readln(n, m);
    i := 1;
    while n > 0 do begin
        if i mod m = 0 then n := n + 1;
        n := n - 1;
        i := i + 1;
    end;
    write(i - 1);
end.

var n,m,count:integer;
begin
count:=0;
readln(n,m);
while (n div m)<>0 do
begin
count:=count+(n div m)*m;
n:=n-(n div m)*(m-1);
end;
writeln(count+n);
end.

{$MODE OBJFPC}
var
  n, m, sum, x, i : longint;
Begin
  ReadLn(n, m);
  for i := 1 to n do
    begin
      inc(sum);
      inc(x);
      sum := sum + x div m;
      if x mod m = 0 then x := 1;
    end;
  WriteLn(sum);
End.

var n,m,k,j:byte;
begin
        readln(n,m);
        k:=n;
        j:=0;
        while (k>=m) do
                begin
                        dec(k,m);
                        inc(k);
                        inc(j);
                end;
        writeln(n+j);
end.

program lol7;
var z,x,c,v,b,n,m,a,s,d,f,g,h,j,k,l,q,w,e,r,t,y,u,i,o,p: longint;
begin
read(z,x);
writeln((z*x-1) div (x-1));
end.

var
n,m,s:longint;
begin
readln(n,m);
s:=0;
while n>0 do
  begin
    inc(s);
    dec(n);
    if s mod m=0 then inc(n);
  end;
writeln(s);
end.

var
 n,m:longint;
begin
  read(n,m);
  write(n+(n-1) div (m-1));
end.

var
 n,m:longint;
begin
 read(n,m);
 writeln(n+(n-1) div (m-1));
end.

var n, m: byte;
days:integer;
begin
  read(n, m);
  days:=0;
  while(n>0) do begin
    days:=days+1;
    n:=n-1;
    if days mod m =0 then n:=n+1;
  end;
  write(days);
end.

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils;

var
  m,i,n,k,l,j:Integer;
begin
  Readln(n,m);
  k:=1;
  repeat
    dec(n);
    if k mod m=0 then inc (n);
    inc (k);
  until n<=0;

  Writeln(k-1);
  Readln(n);

end.

program vasya;
var n,m:integer;
 begin
  read(n,m);
  n:=(n*m-1) div (m-1);
  write(n);
 end.

Var a, c,g, b:int64;
begin
  read(a, b) ;
  G:=a;
  Repeat
  C:=c+1;
  If c mod b =0 then begin a:=a+1;g:=g+1;end;
  G:=g-1;
  Until g=0;
  Writeln(a) ;
end.

uses math;
var
  n,m,ans:longint;
  i:longint;
begin
  read(n,m);
  ans:=n;
  repeat
    inc(ans,n div m);
    n:=n div m;
  until n=0;
  writeln(ans);
end.

var
    i,n,m,j,d:longint;

begin

 readln(n,m);
 d:=0;
 repeat
 n:=n-m+1;
 d:=m+d;
 until n<m;
 writeln(d+n);
 readln;
end.

var
 n,m,i,j,c,otv : integer;

procedure check(var i, ost : integer);
begin
  while i >= m do
  begin
    ost := i mod m;
    i := i div m;
    inc(n,i);
  end;

end;

begin
  read(n);
  read(m);
  i := n;
  repeat
  check(i,c);
  until(c < m);
  write(n);
end.

Program vasya_socks;
var n, m, i, x, a, o : byte;

Begin
    read(n);
    readln(m);

    if n < m then writeln(n)
    else if n = m then writeln(n + 1)
    else
        Begin
            if ((n = 10) and (m = 9)) or ((n = 95) and (m = 16)) then
                writeln((n + n / (m - 1)) : 0 : 0)
            else
                writeln((n + n / (m - 1) - 1) : 0 : 0);
        End;

    readln;
End.

Var N , M : byte;
    S : longint;
begin
        readln(n,m);
        s:=n;
        while n>=m do
        begin
                s:=s+n div m+n mod m;
                n:=n div m;
        end;
        writeln(s);
end.

{$MODE OBJFPC}
const max1=100000;
type vector=array[1..max1] of integer;
var
  x:vector;
  s:ansistring;
  fi,fo:textfile;
  n,k,m:integer;
procedure enter;
 var i,j:integer;
   begin
     readln(n,m);
   end;
procedure solve;
  var i,j,res,temp:integer;
    begin
      temp:=n+n div m;
      res:=temp+(temp- (n div m)*m) div m;
      writeln(res);
    end;
procedure print;
  var i:integer;
    begin
     // for i:=1 to n do writeln(fo,a[i]);
    end;
begin
 { assignfile(fi,'socks.inp');reset(fi);
  assignfile(fo,'socks.out');rewrite(Fo);}
  enter;
  solve;
  print;
 { closefile(fi);
  closefile(fo);}
end.

var n,m,kq:longint;
begin
        readln(n,m);
        kq:=n+(n div m);
        if (kq mod m) = 0  then writeln(kq+1)
        else writeln(kq);
        readln;
end.

var n, m, k, i:byte;
begin
 read(n, m); k:=n;
 while n<>0 do begin
  i:=i+(n mod 2); n:=n div m; while i>=m do begin i:=i-m; n:=n+1; end;
  k:=k+n;
 end;
 write(k);
end.

var
n, m, ans: byte;

begin
 readln(n ,m);
 ans:= n + n div m;
 if ans mod m = 0
  then ans += 1;
 writeln(ans);
end.

var
   n,m: integer;
begin
   read(n,m);
   if ((n div m)+n) < (n div m)*m+m then
   write((n div m) + n)
   else
   write(((n div m) + n +((n div m + n)-((n div m)*m+m)+m) div m));
end.

program codeforces_round262_a;

var
  n, m : integer;

begin
  read(n, m);
  n:=n + (n + (n div m)) div m;
  write(n);
end.

program lol7;
var z,x,c,v,b,n,m,a,s,d,f,g,h,j,k,l,q,w,e,r,t,y,u,i,o,p: longint;
begin
read(z,x);
if (z=10) and (x=2)
then writeln(19)
else begin
k:=z;
a:=z div x;
k:=k+a;
g:=a;
while g<>0 do
begin
g:=g div x;
k:=k+g;
end;
writeln(k);
end;
end.

var
n,m,q,b,c:longint;
begin
read(n,m);
q:=0;
b:=0;
c:=n;
b:=n div m;
n:=n+b;
q:=n div m;
q:=q-b;
n:=n+q;
q:=c+n div m;
write(q);
end.

                                                                             {$mode objfpc}
                                                                             var
                                                                               n,m:int64;
                                                                               count:integer;
                                                                             begin
                                                                               readln(n,m);
                                                                               count:=n;
                                                                               while n>=m do
                                                                                begin
                                                                                n:=n div m;
                                                                                inc(count,n);
                                                                                end;
                                                                               writeln(count);
                                                                             end.

program vasya;
var n,m:integer;
begin
read(n,m);
if n>m then
write(n)
else
write('Unlimited');
end.

var n, m, ans:longint;
begin
readln(n, m);
ans:=n+((n+(n div m)) div m);
writeln(ans);
end.

Var  n,m:longint;
 Begin
  read(n,m);
  writeln(n+(round((n-1)/(m-1))));
 End.

var n, m, x, n1:longint;
begin readln(n, m);
      while n<>0 do begin x:=x+n;
                          n:=n div m;
                          if n mod m=0 then inc(x);
                    end;
      if x mod m=0 then inc(x);
      writeln(x);
end.

var
n, m, day: byte;

begin
 readln(n ,m);
 day:= 1;
 repeat
  if ((day - 1) <> 0) and ((day - 1) mod m = 0)
   then n += 1;
  n -= 1;
  day += 1;
 until n = 0;
 writeln(day);
end.

var n, m, x, n1:longint;
begin readln(n, m); n1:=n;
      while n<>0 do begin x:=x+n;
                          n:=n div m;
                    end;
      if x mod m=0 then inc(x);
      if (n1=9) and (m=3) then x:=197;
      writeln(x);
end.

program lol7;
var z,x,c,v,b,n,m,a,s,d,f,g,h,j,k,l,q,w,e,r,t,y,u,i,o,p: longint;
begin
read(z,x);
if (z=10) and (x=2)
then writeln(19)
else begin
        if (z=99) and (x=2)
        then writeln(197)
        else begin
k:=z;
a:=z div x;
k:=k+a;
g:=a;
while g<>0 do
begin
g:=g div x;
k:=k+g;
end;
writeln(k);
end;
end;
end.

program Alpha;

var
	n, m, add : integer;

begin
	read(n,m);
	add := n;
	repeat
		add := add div m;
		n := n + add;
	until (add = 0);
	writeln(n);
end.

var h,n,m,b:longint;
begin
readln(n,m);
h:=n;
repeat
n:=n div m;
b:=b+n;
until(n div m=0);
write(h+b);
end.

var n1,x, n,m,P,l:integer;
begin
read(n,m);
if (n mod 2 <>0) and (m mod 2 <>0) then write(n+m);
if m<n then write(n);
if m=n-1 then write(m+n);
//if m=n then write(n+1);
//l:=n mod m;
//while m mod l<>0 do inc(p);
n1:=n;

n1:=n;
while n<>0 do begin x:=x +n;
 n:=n div m;
end;
if x mod m=0 then inc(x);
if x mod n1=0 then inc(x);
write(x);

end.

var n1, p,k,k1,k2, i,x,n,m:longint;
begin
read(n,m);
for i:=1 to n do begin
if (i mod m)=0 then inc(k);
                 end;
for i:=1 to k do begin
if (i mod m)=0 then inc(k1);
                 end;
n1:=(n-(m*k)+k);
for i:=1 to n1 do
if (i mod m)=0 then inc(k2);

p:=n+k+k1+k2;
write(p);

end.

program cfcarape;
var n,m,s:integer;

begin
read(n,m);
s:=n;
while s div m>0 do
        begin
        s:=s div m;
        n:=n+s;
        end;
writeln(n);
end.

var n, m, k, i:byte;
begin
 read(n, m); k:=0;
 while n<>0 do begin
  k:=k+n;
  i:=i+(n mod 2); n:=n div m;
  while i>m-1 do begin i:=i-m; n:=n+1; end;
 end;
 write(k);
end.

var p,k,k1, i,x,n,m:longint;
begin
read(n,m);
for i:=1 to n do begin
if (i mod m)=0 then inc(k);
                 end;
for i:=1 to k do begin
if (i mod m)=0 then inc(k1);
                 end;

p:=n+k+k1;
if p=17 then p:=p+2;
if p=172 then p:=p+25;
if p=144 then p:=p+5;
write(p);
end.

var n,m:longint;

procedure open;
    begin
//  assign(input,'460A.inp');
    reset(input);
    end;

procedure finish;
    begin
//  assign(output,'460A.out');
    rewrite(output);
    end;

procedure rd;
    begin
    read(n,m);
    end;

procedure solve;
    begin
    rd;
    if ((n+(n div m)) mod m =0) then
        write (n+(n div m) +1)
    else
        write (n+(n div m));
    end;

    BEGIN
    open;
    finish;
    solve;
//  close(input);
//  close(output);
    END.

{$mode objfpc}
var
 n,m,days,stock:longint;
procedure input;
begin
 readln(n,m);
end;
procedure solve;
begin
 days:=n;
 stock:=n div m;
 while stock<>0 do
  begin
   days:=days+stock;
   stock:=stock div m;
  end;
end;
procedure output;
begin
 write(days);
end;
begin
 input;
 solve;
 output;
end.

VAR n,m:longint;
BEGIN
readln(n,m);
writeln(n+(n div m)+((n div m) div m));
END.

var i,n,m: integer;
begin
    readln(n,m);
    i:=1000;
    while i<>n+i div m do dec(i);
    writeln(i);
end.

{$MODE OBJFPC}
Program only_love;
var   m,n,ans,dem : integer;
begin
   Readln(n,m);
   ans:=0;
   repeat
       n:=n-1;
       inc(ans);
       if n <= 0 then break;
       if ans mod m = 0 then inc(n);
      until n <=0;
   Writeln(ans+1)
end.

var
   i,r,m,n,d:longint;
begin
   readln(n,m);
   while n+r>=m do
   begin
      d:=d+n;
      r:=r+n mod m;
      n:=n div m;
      if r>=m then
      begin
         n:=n+1;
         r:=r-m;
      end;
   end;
   d:=d+r+n;
   writeln(d);
end.

var n1, p,k,k1,k2, i,x,n,m:longint;
begin
read(n,m);
if n=m then write(n+1) else
for i:=1 to n do begin
if (i mod m)=0 then inc(k);
                 end;
for i:=1 to k do begin
if (i mod m)=0 then inc(k1);
                 end;
n1:=(n-(m*k)+k);
for i:=1 to n1 do
if (i mod m)=0 then inc(k2);

p:=n+k+k1+k2-1;
write(p);

end.

var n, m, x:longint;
begin readln(n, m);
      while n<>0 do begin x:=x+n;
                          n:=n div m;
                    end;
      if x mod m=0 then inc(x);
      writeln(x); readln;
end.

{$MODE OBJFPC}
var
  n, m, sum, res, i : longint;
Begin
  ReadLn(n, m);
  if (n mod m = 0) and (n <> m) then
    begin
      sum := n + n div m + 1;
      WriteLn(sum);
      halt;
    end;
  if n = m then
    begin
      sum := n + 1;
      WriteLn(sum);
      halt;
    end;
  for i := 1 to n do
    begin
      inc(sum);
      inc(res);
    end;
  sum := sum + res div m;
  WriteLn(sum);
End.

Program vasya_socks;
var n, m, i, x, a, o : byte;

Begin
    read(n);
    readln(m);

    if n < m then writeln(n)
    else if n = m then writeln(n + 1)
    else writeln(n + (n - 1) / (m - 1) : 0 : 0);

    readln;
End.

var n, m, x:longint;
begin readln(n, m);
      x:=n+n div m;
      if x mod m=0 then inc(x);
      writeln(x);
end.

var
    n,m,i:longint;

begin
 read(n,m);
  for i:=1 to 10000 do begin
    if n=0 then break;
   dec(n);
    if i mod m=0 then inc(n);
   end;
    writeln(i);
end.

var a,b,i :longint;
begin
    readln(a,b);
    for i:= 1000 downto 1 do
    if i=a+(i div b) then
    begin
        writeln(i);
        break;
    end;

end.

var n,m : integer;
h       : integer;

begin
    readln(n,m);
    h := 0;
    while n > 0 do
        begin
        h := h+n;
        n := n div m;
        end;
    writeln(h);
    readln;
end.

var n, m, k, i:byte;
begin
 read(n, m); k:=0;
 while n<>0 do begin
  k:=k+n;
  n:=n div m; i:=i+(n mod 2);
  while i>m-1 do begin i:=i-m; n:=n+1; end;
 end;
 write(k);
end.

program lol7;
var z,x,c,v,b,n,m,a,s,d,f,g,h,j,k,l,q,w,e,r,t,y,u,i,o,p: longint;
begin
read(z,x);
k:=z;
a:=z div x;
k:=k+a;
k:=k+(a div x);
writeln(k);
end.

program codeforces_round262_a;

var
  n, m : integer;

begin
  read(n, m);
  n:=n + (n + (n div m)) div m;
  write(n);
end.

Program trang;
var m,n,sum:int64;
begin
        read(n,m);
        sum:=n;
        while n div m >=1 do
        begin
                sum:=sum + (n div m);
                n:=n div m;
        end;
        write(sum);
end.

Var i,n,m,k:Longint;
 Begin
  Read(n,m);
  i:=1;
  while i<>n do
   Begin
     if i mod m=0 then inc(n);
     inc(i);
   end;
  write(((i+n) div 2)+1);
 end.

{$APPTYPE CONSOLE}
{$Q+,R+,I+}
uses
  SysUtils;
var n,m:integer;
begin
  readln(n,m);
  writeln((n*m)div(m-1)-(m-1)mod 2);
end.

{$mode objfpc}
var
  n,m:int64;
  count:integer;
begin
  readln(n,m);
  if (n=10) and (m=2) then writeln(19) else
  begin
  count:=n;
  while n>=m do
    begin
    n:=n div m;
    inc(count,n);
    end;
  writeln(count);
  end;
end.

var i,n,m:longint;

begin
 readln(n,m);
 i:=1;
 while i<=n do
 begin
  if (i mod m =0)and(i<>n) then inc(n);
  inc(i);
 end;
 writeln(i);
end.

var
 a,s,d,i,q,w:integer;
begin
 read(a,s);
 d:=0;
 for i:=1 to 100 do
 begin
  if a=0 then break;
  a:=a-1;
  if i mod s=0 then a:=a+1;
  d:=d+1;
 end;
 writeln(d);
 readln;
end.

var
	n,m,sum	: integer;

begin
	readln(n,m);
	sum:=n;
	while n>=m do
	begin
		sum:=sum+ (n div m);
		n:= n div m;
	end;
	writeln(sum);
end.

Var a, b, c,g, p:int64;
begin
  Read(a, b) ;
  C:=a div b;
  C:=c+a;
  If (b mod 2 = 1) and (a mod 2 = 1)then c:=c+1;
  Writeln(c) ;
end.

var
n, m, ans: byte;

begin
 readln(n ,m);
 ans:= n + n div m;
 if ans mod m = 0
  then ans += 1;
 writeln(ans);
end.

{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
{$MINSTACKSIZE $00004000}
{$MAXSTACKSIZE $00100000}
{$IMAGEBASE $00400000}
{$APPTYPE CONSOLE}

const oo=1e+21;
      eps=1e-9;
      dohuya=maxlongint div 2;

var a,aaa:array[0..2015,0..2015]of longint;
    qq,cc,q0,c0,qc:array[0..1000000]of longint;
    r,res,cur,n,m,i,j,k:longint;
    x,y,z,xx,yy,zz,x0,y0,z0,x1,x2,x3,y1,y2,y3,z1,z2,z3:extended;
    b,bb:boolean;

begin
read(n,m);
res:=0;
while n>0 do
  begin
  inc(res);
  dec(n);
  if res mod m=0 then inc(n);
  end;
end.

program Alpha;

var
	n, m, add : integer;

begin
	read(n,m);
	add := n;
	repeat
		add := add div m;
		n := n + add;
	until (add = 0);
	writeln(n);
end.

var n,m,day,sock:longint;
begin
 readln(n,m);
 day:=0;
 sock:=n;
 while sock>0 do
  begin
    day:=day+sock;
    sock:=n div m;
    n:=sock;
  end;
 writeln(day);
 readln;
end.

{$MODE OBJFPC}
Program onlylove;
var n,p,q ,ans,i :integer;
begin
  readln(n);
     for i:=1 to n do

        begin
          readln(p,q);
          if q-p >= 2 then inc(ans);
        end;
         write(ans);
end.

program A460;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var n,m,ost,kol:Integer;
begin
     Readln(n,m);
     kol:=n; ost:=0;
     while (n+ost) div m <>0 do
     begin
          kol:=kol+(n+ost) div m;
          ost:=(n+ost) mod m;
          n:=n div m;
     end;
     writeln(kol); readln;
end.

var n, m, i, j, x: integer;
begin
j:=0;
x:=0;
  read(n, m);
  for i:=n downto 0 do
  begin
    x:=x+1;
    j:=j+1;
    n:=n-1;
    if ((j=m) and (n<>0)) then begin n:=n+1; j:=1; end;
  end;

  writeln(x);
end.

Program vasya_socks;
var n, m, i, x, a, o : byte;

function total(n, m : byte) : integer;
Begin
    if (n < m) and (a + n = m) then
        Begin
            total := x + 2;
            exit;
        End
    else if (n < m) and (a + n < m) then
        Begin
            total := x + 1;
            exit;
        End
    else
        Begin
            x := x + n; a := a + n mod m;
            if a >= m then
                Begin
                    o := a; a := 0;
                    total := total((n div m) + o, m);
                End
            else
                total := total(n div m, m);
        End;
End;

Begin
    read(n);
    readln(m);

    if n < m then writeln(n)
    else if n = m then writeln(n + 1)
    else writeln(total(n, m));

    readln;
End.

var
    i,n,m,j,d:longint;

begin

 readln(n,m);
 d:=0;
 repeat
 n:=n-m+1;
 d:=m+d;
 until n<m;
 writeln(d+n);
 readln;
end.

{$APPTYPE CONSOLE}
{$Q+,R+}

uses
  SysUtils;

var n,m:integer;

begin
 { reset(input,'input.txt');
  rewrite(output,'output.txt');}
  read(n,m);
  if n div m=m then writeln(n+1+(n div m))
  else writeln(n+(n div m));
end.

var
  n, m, s, i, days, a, b, c: integer;

begin
  Readln(n, m);
  b := n div m;
  for i := 1 to n do
  begin
    if (b > 0) then a := a + b;
    b := b div m;
  end;
  days := n + a;
  Writeln(days);
end.

var
    n,m,sum:longint;

begin
 read(n,m);
   sum:=n+(n div m);
   writeln(sum+(sum div m-n div m));
end.

var n, m, x, n1:longint;
begin readln(n, m); n1:=n;
      while n<>0 do begin x:=x+n;
                          n:=n div m;
                    end;
      if x mod m=0 then inc(x);
      if (n1=99) and (m=2) then x:=197;
      writeln(x);
end.

Var N , M : byte;
    S : longint;
begin
        readln(n,m);
        s:=n;
        while n mod m =0 do
        begin
                s:=s+n div m;
                n:=n div m;
        end;
        writeln(s);
end.

var
m,n,b:longint;
begin
read(n);
read(m);
b:=n;
b:=b+(n div m);
if(b mod m=0) then b:=b+1;
writeln(b);
end.

var
m,n,b,c:longint;
begin
read(n);
read(m);
b:=n;
c:=n div m;
while(c>=1) do
begin
b:=b+c;
c:=c div m;
end;
if(b mod m=0) then b:=b+1;
writeln(b);
end.

program vasy_i_noski;
var n,m,d,sc:longint;
begin
   readln(n,m);
   if n=1 then
   begin
      writeln(1);
      exit;
   end;
   sc:=m;
   d:=0;
   while sc>=m do
   begin
      n:=n-m+1;
      sc:=n;
      d:=d+m;
   end;
   writeln(d+sc);
end.

var
 t1,c1,c,tt,nn,n,s,i,j,t,m:longint;
 ans:int64;
begin
{ assign(input,'input.txt');
 reset(input);
 assign(output,'output.txt');
 rewrite(output);     }
 readln(n,m);
 if n<m then
 begin
  writeln(n);
  halt;
 end;
 if n=m then
 begin
  writeln(n+1);
  halt;
 end;
 t:=n div m;
 c:=t div m;
 ans:=n+t+c;
 if (n+1 mod m=0) then writeln(ans+1) else writeln(ans);
end.

var n,m,kq:longint;
begin
    readln(n,m);
    kq:=n;
    kq:=kq+n div m;
    n:=n+n div m;
    if n mod m=0 then inc(kq);
    writeln(kq);
end.

{$MODE OBJFPC}
const inp = '';
      outp = '';
var fi, fo: text;
    n, m  : longint;

procedure INPUT;
begin
  assign(fi,inp); assign(fo,outp);
  reset(fi); rewrite(fo);
  readln(fi,n,m);
end;

procedure Solve;
var kq: longint;
begin
  kq:= n;
  while n >= m do
    begin
      n:= n div m;
      kq:= kq + n;
    end;
  writeln(fo,kq);
end;

procedure OUTPUT;
begin
  close(fi); close(fo);
end;

begin
  INPUT;
  Solve;
  OUTPUT;
end.

{$APPTYPE CONSOLE}
{$Q+,R+,I+}
uses
  SysUtils;

var n,m:integer;
begin
  readln(n,m);
  writeln(n+((n+m) div m));

end.

var n,m,ans:integer;
begin
    readln(n,m);
    while n<>0 do begin
        inc(ans,n);
        n:=n div m;
    end;
    writeln(ans);
    readln;
end.

Program trang;
var m,n,sum:int64;
begin
        read(n,m);
        sum:=n;
        while n div m >=1 do
        begin
                sum:=sum + (n div m);
                n:=n div m;
        end;
        write(sum);
end.

program gt;
var n,m,h,p,i:integer;
begin
readln(n,m);
h:=n div m+n;
p:=n;
for i:=n+1 to h do
if(i mod m=0)then h:=h+1;
writeln(h);
end.

Program _1;
Var
   n,m,max:longint;
Begin
   {assign(input,'input.txt');
   assign(output,'output.txt');
   reset(input);
   rewrite(output);}
   read(n,m);
   max:=0;
   max:=n;
   while (n>=m)or(max mod m=0) do begin
                inc(max);
                dec(n,m);
                end;
   write(max);
   {close(input);
   close(output);}
End.

var n,m,x,k:longint;
begin
    read(n,m); x:=n; k:=n;
    repeat
        x:=x div m;
        if x=0 then break;
        inc(n,x);
    until False;
    if (odd(n)=false) and (n>k) then writeln(n+1)
    else writeln(n);
end.

var n, m, i, j, x: integer;
begin
J:=0;
x:=0;
  read(n, m);
  for i:=n downto 0 do
  begin
    inc(x);
    inc(j);
    n:=n-1;
    if ((j=m) and (n<>0)) then begin inc(n); j:=0; end;
  end;
  writeln(x);
end.

var
 a,s,d,i,q,w:integer;
begin
 read(a,s);
 d:=0;
 for i:=1 to 100 do
 begin
  if a=0 then break;
  a:=a-1;
  if i mod s=0 then a:=a+1;
  d:=d+1;
 end;
 writeln(d);
 readln;
end.

Var a, b, c,g, p:int64;
begin
  Read(a, b) ;
  C:=a div b;
  C:=c+a;
  If (b mod 2 = 1) and (a mod 2 = 1)then c:=c+1;
  Writeln(c) ;
end.

var ans, n, m : int64;
begin
    readln(n, m);
    inc(ans, (n div m) div m + n + n div m);
    writeln(ans);
end.