program chandelier;

{$APPTYPE CONSOLE}

type
	pTree	= ^tTree;
   tTree	= record
   	n, s, p	: integer;
      a			: array [1..9] of pTree;
   end;

var
	Ch		: char;
   i, n	: integer;
   a		: array [1..10000] of byte;
   t		: pTree;

   procedure Create(var t : pTree);
   var
   	j	: integer;
   begin
   	new(t);
      t^.n := a[i];
      t^.s := -1;
      i := i - 1;
      for j := t^.n downto 1 do
      	Create(t^.a[j]);
   end;

   function CountSt(var t : pTree) : integer;
   var
   	i, p, ss, j	: integer;
   	s				: array [1..9] of integer;
   begin
   	if t^.s >= 0 then
      	CountSt := t^.s
      else if t^.n = 0 then begin
      	CountSt := 1;
         t^.s := 1;
         end
      else begin
      	for i := 1 to t^.n do
         	s[i] := CountSt(t^.a[i]);
         t^.s := maxint;
         for p := 1 to t^.n do begin
         	ss := 0;
            for j := 1 to t^.n do
            	if s[(j + p - 2) mod t^.n + 1] + j - 1 > ss then
               	ss := s[(j + p - 2) mod t^.n + 1] + j - 1;
            if ss < t^.s then begin
            	t^.s := ss;
               t^.p := p;
            end;
         end;
         CountSt := t^.s;
      end;
   end;

   procedure Print(var t : pTree);
   var
   	i	: integer;
   begin
   	if t^.n = 0 then
      	write('a')
      else begin
      	for i := 1 to t^.n do
         	Print(t^.a[(i + t^.p - 2) mod t^.n + 1]);
         write(t^.n);
      end;
   end;

begin
	assign(input, 'chandelier.in');
   reset(input);
	assign(output, 'chandelier.out');
   rewrite(output);

   n := 0;
   while not eoln do begin
   	n := n + 1;
   	read(Ch);
      if Ch = 'a' then
      	a[n] := 0
      else
      	a[n] := ord(Ch) - ord('0');
   end;

	i := n;
   t := nil;
   Create(t);
   writeln(CountSt(t));
   Print(t);
end.
