{DEFINE DMI}
{DEFINE DO0}
{DEFINE DEBUG}
{$IFDEF DEBUG}
{$B-,I+,N+,O-,Q+,R+,S+}
{$ELSE}
{$B-,I-,N+,O+,Q-,R-,S-}
{$ENDIF}
{$APPTYPE CONSOLE}
Program OlympSol;
uses SysUtils;
const TaskName = 'chandelier';
 MaxN = 100000004;
var i,minimax,begg,t,ls, j,jj, ii, tt,start, locmax, k, l, m, n: Integer;
ans,ttt,s:string;

num,ves, en:array[0..10000] of integer;
BEGIN
 assign (input, TaskName + '.in'); reset (input);
 assign (output, TaskName + '.out'); rewrite (output);
{$IFDEF DMI} repeat {$ENDIF}
 readln (s);
 ans:='';
 ls:=length(s);
 i:=1;
 while i<=ls do
 begin
   if s[i]='a' then
   begin
     ves[i]:=1;
     en[i]:=i-1;
     ans:=ans+s[i];
     inc(i);
     continue;
   end
   else
   begin
     t:=ord(s[i])-ord('0');
     tt:=t;
     ii:=i-1;
     while tt>0 do
     begin
       num[tt-1]:=ii;
       ii:=en[ii];
       dec(tt);
     end;
     start:=ii+1;
     en[i]:=ii;
     minimax:=maxn;
     begg:=0;
{     for j:=0 to t-1 do
       writeln(num[j]);
     writeln('ves');
     for j:=1 to i-1 do
     writeln(ves[j]);
     writeln;       }
     for j:=0 to t-1 do
     begin
       locmax:=0;
       for jj:=0 to t-1 do
       begin
         if locmax<ves[num[(j+jj)mod t]]+jj then
           locmax:=ves[num[(j+jj)mod t]]+jj;
       end;
       if minimax>locmax then
       begin
         begg:=j;
         minimax:=locmax;
       end;
     end;
     ves[i]:=minimax;
     ttt:=copy(s, en[num[begg]]+1, i-en[num[begg]]-1);
     ttt:=ttt+copy(s, en[i]+1, en[num[begg]] - en[i]);
     for jj:=start to i-1 do
       s[jj]:=ttt[jj-start+1];
     inc(i);
     {writeln(begg+1);
     writeln(s);}
   end;
 end;
 writeln(ves[ls]);
 writeln(s);
{$IFDEF DMI} until seekeof; {$ENDIF}
END.
