{$O+,R+}
const inf=1000000000;
type pnode=^tnode;
     tnode=record
      col:integer;
      ms:integer;
      next:pnode;
      fchild:pnode;
      cbest:pnode;
     end;

var ch:char;
    nd:pnode;
    st:array[1..10001] of pnode;
    sp:integer;

procedure push(a:pnode);
begin
 inc(sp);
 st[sp]:=a;
end;

function pop:pnode;
begin
 result:=st[sp]; st[sp]:=nil; dec(sp);
end;

procedure assemble(c:integer);
var p,ch:pnode;
begin
 p:=new(pnode);
 p.next:=nil;
 p.col:=c;
 ch:=pop;
 p.fchild:=ch;
 ch.next:=nil;
 dec(c);
 while c>0 do begin
  ch:=p.fchild;
  p.fchild:=pop;
  p.fchild.next:=ch;
  dec(c);
 end;
 push(p);
end;

function countms(nd:pnode):integer;
var t,curch:pnode;
    i,j,nn:integer;
    min,mc,c:integer;
begin
 t:=nd.fchild;
 nn:=0;
 while t<>nil do begin
  countms(t);
  t:=t.next;
  inc(nn);
 end;
 curch:=nd.fchild;
 min:=inf;
 for i:=1 to nn do begin
  mc:=0;
  t:=curch;
  for j:=1 to nn do begin
   c:=t.ms+j-1;
   if c>mc then mc:=c;
   t:=t.next;
   if t=nil then t:=nd.fchild;
  end;
  if mc<min then begin min:=mc;
   nd.cbest:=curch;
  end; 
  curch:=curch.next;
 end;
 if min<>inf then nd.ms:=min else nd.ms:=1;
 result:=nd.ms;
end;

procedure outp(nd:pnode);
var t:pnode;
    i:integer;
begin
 t:=nd.cbest;
 if t=nil then begin
  write('a');
  exit;
 end;
 for i:=1 to nd.col do begin
  outp(t);
  t:=t.next;
  if t=nil then t:=nd.fchild;
 end;
 write(nd.col);
end;

begin
 sp:=0;
 assign(input,'chandelier.in'); reset(input);
 assign(output,'chandelier.out'); rewrite(output);
 if seekeof(input) then halt(0);
 read(ch);
 while ch in ['a','1'..'9'] do begin
  if ch='a' then begin
   nd:=new(pnode);
   nd.next:=nil;
   nd.fchild:=nil;
   nd.col:=-1;
   push(nd)
  end else
   assemble(byte(ch)-byte('0'));
  read(ch);
 end;
 nd:=pop;
 writeln(countms(nd));
 outp(nd);
 close(output);
 close(input);
end.