const
 fin='chandelier.in';
 fout='chandelier.out';

TYPE
   PLink=^TLInk;
   PNode = ^TNode;
   TLink =
         record
           V:Pnode;
           Next:PLink;
          End;
   TNode =
   record
     Size,L:LongInt;
     Next: PLink;
   End;

Var
 Stack:array[1..100000] of PNode;
 Top,Num:LongInt;

 ch:char;

 Head:PNode;

 Procedure Update(N:LongInt);
 Var
  P:PNode;
  R:PLink;
  I:LongInt;
 Begin
  New(P);
  P^.Next:=nil; P^.L:=N;
  For I:=Top downto Top-N+1 do
   begin
     new(R);
     R^.V:=Stack[I];
     R^.Next:=P^.Next;
     P^.Next:=R;
   End;
   Top:=Top-N+1;
   Stack[Top]:=P;
 End;

Var
 A:array[1..100000] of LongInt;
 B:array[1..100000] of PNode;
 Best, BestS:Longint;

 function Get(N:LongInt):LongInt;
 Var
  R, S, T, K, I : LongInt;
 Begin
  Best:=1000000;
  FOR S:=1 tO N do
  BEgin
   R:=A[S]; K:=0; T:=N; I:=S;
   While (T>0) do
   Begin
    IF (R<A[I]+K) then R:=A[I]+K;
    Inc(I); If (I>N) then I:=1;
    dec(T); Inc(K);
   End;
   if (Best>R) then
   Begin
     Best:=R;
     BEstS:=S;
   End;
  End;
  Get:=BestS;
 End;

 procedure BFS(P:PNode);
 Var
  L:PLink;
 Begin
  If (P^.L=1) then
  Begin
   Write('a');
   exit;
  End;
  L:=p^.Next;
  while (L<>nil) do
  Begin
    BFS(L^.V);
    L:=L^.Next;
  End;
  Write(P^.L)
 End;

 procedure Solve(P:PNode);
 Var
  L:PLink;
  Tmp:pNode;
  I, K, SS:LongInt;
 Begin
  If (P^.L=1) then exit;
  L:=p^.Next;
  while (L<>nil) do
  Begin
    Solve(L^.V);
    L:=L^.Next;
  End;
  I:=0;
  L:=p^.Next;
  while (L<>nil) do
  Begin
    Inc(I); Tmp:=L^.V; B[I]:=Tmp;
    A[I]:=Tmp^.Size;
    L:=L^.Next;
  End;
  K:=Get(I); SS:=I; P^.Next:=nil;

  Dec(K); If (K=0) then K:=SS;

  While (I>0) do
  Begin
    new(L);
    L^.V:=B[K]; L^.Next:=P^.Next; P^.Next:=L;
    dec(K); if (K=0) then K:=SS;  dec(I);
  End;

 P^.Size:=Best;

End;

begin
 assign(Input,fin); reset(input);
 assign(Output,fout); rewrite(output);
 Top:=0;
 While (Not eof(Input))or(Not eoln(Input)) do
 Begin
  read(ch);
  if (Ch='a') then
  Begin
    Inc(Top); new(Stack[Top]); Stack[Top]^.Size:=1; Stack[Top]^.L:=1; Stack[Top].Next:=nil;
  End
  else
  Begin
    Num:=0;
    Num:=Num*10+ord(Ch)-ord('0');
    Update(Num);
  End;
 End;
 if (TOP<>1) then
 Begin
   WHILE TRUE DO ; 
   close(input); close(output);
   Halt;
 End;
 Head:=Stack[1];
 Solve(Head);
 WriteLn(Head^.Size);
 BFS(Head);
 close(input); close(output);
end.