#include <string>
#include <algorithm>
#include <fstream>

using namespace std;

ifstream fin( "chandelier.in" );
ofstream fout( "chandelier.out" );

const int MAX_N = 10000;
const int MAX_M = 10;
const int INF = 1000000;

struct SNode
{   
   SNode *Childs[ MAX_M ];
   int Height;
   int Count;
};

class CStack
{
private:
   SNode *Data[ MAX_N ];
   int Top;

public:
   CStack()
   {
      Top = 0;
   }

   void Clear()
   {
      Top = 0;
   }

   bool IsEmpty()
   {
      return Top == 0;
   }

   void Push( SNode *node )
   {
      Data[ Top++ ] = node;
   }

   SNode *Pop()
   {
      return Data[ --Top ];
   }
};


int NodesCount;
SNode Nodes[ MAX_N ];

SNode *Root;

CStack Stack;

void ReadData()
{
   int i;
   char c;

   NodesCount = 0;

   Stack.Clear();

   while( true )
   {
      fin >> c;

      if( fin.eof() )
         break;

      if( c == 'a' )
      {
         SNode *node = &Nodes[ NodesCount++ ];

         node->Count = 0;

         Stack.Push( node );
      }
      else if( c >= '1' && c <= '9' )
      {
         SNode *node = &Nodes[ NodesCount++ ];

         node->Count = c - '0';

         for( i = 0; i < c - '0'; i++ )
            node->Childs[ i ] = Stack.Pop();

         Stack.Push( node );
      }
   }

   Root = Stack.Pop();
}

void Work()
{
   int i, j, k;

   for( i = 0; i < NodesCount; i++ )
   {
      int count = Nodes[ i ].Count;

      if( count == 0 )
         Nodes[ i ].Height = 1;
      else
      {
         int minHeight = INF;
         int jmin = 0;

         for( j = 0; j < count; j++ )
         {
            int maxHeight = 0;

            for( k = 0; k < count; k++ )
            {
               if( maxHeight < Nodes[ i ].Childs[ ( j + k ) % count ]->Height + ( count - 1 - k ) )
                  maxHeight = Nodes[ i ].Childs[ ( j + k ) % count ]->Height + ( count - 1 - k );
            }

            if( maxHeight < minHeight )
            {
               minHeight = maxHeight;
               jmin = j;
            }
         }
         rotate( Nodes[ i ].Childs + 0, Nodes[ i ].Childs + jmin, Nodes[ i ].Childs + count );
         Nodes[ i ].Height = minHeight;
      }
   }
}

void Output()
{
   fout << Root->Height << endl;

   string str = "";

   Stack.Clear();

   Stack.Push( Root );

   while( !Stack.IsEmpty() )
   {      
      SNode *node = Stack.Pop();

      if( node->Count == 0 )
         str += 'a';
      else
      {
         int i;

         for( i = node->Count-1; i>=0; --i )
            Stack.Push( node->Childs[ i ] );

         str += ( char )( '0' + node->Count );
      }
   }

   reverse( str.begin(), str.end() );

   fout << str;
}

int main()
{
   ReadData();

   Work();

   Output();

   return 0;
}
