program ioi94day2prb1ver3(input, output, inp, out);
{ Tom Verhoeff, Eindhoven University of Technology }
{ Solution based on using a pre-computed inverse to obtain the solution. }
{ The inverse is generated in a pre-processing phase by program invert.pas. }

{ General Section }

const
  Test = true ;

var 
  inp, out: text ;

procedure Init ;
  begin
  if Test then
    writeln('IOI''94 - Day 2 - Problem 1: The Clocks') ;
  assign(inp, 'input.txt') ;
  reset(inp) ;
  assign(out, 'output.txt') ;
  rewrite(out) ;
  if Test then writeln('Initialized')
  end { Init } ;

procedure Fini ;
  begin
  close(inp) ;
  close(out)
  end { Fini } ;


{ Problem Specific Section }

var
  s: array ['A'..'I'] of integer; { s[c] is state of dial c, in quarter turns }
  n: integer; { # moves in solution }
  move: array [1..27] of integer; { move[1..n] is solution sequence }

procedure ReadInput ;
  { read state of clocks into s['A'..'I'] }
  begin
  readln(inp, s['A'], s['B'], s['C']) ;
  readln(inp, s['D'], s['E'], s['F']) ;
  readln(inp, s['G'], s['H'], s['I']) ;
  if Test then begin
    writeln('The starting configuration is:') ;
    writeln(s['A']:2, s['B']:2, s['C']:2) ;
    writeln(s['D']:2, s['E']:2, s['F']:2) ;
    writeln(s['G']:2, s['H']:2, s['I']:2)
    end { if }
  end { ReadInput } ;

procedure ComputeAnswer ;
  var
    A: array[1..9, 'A'..'I'] of integer; { inverse matrix of coefficients }
    t: array [1..9] of integer; { components of solution }
    { The computation to do (where addition and multiplication are modulo 4)}
    {   t[i] := (Sum j: j in ['A'..'I']: A[i,j] * 3 * s[j])  for i in [1..9] }

  procedure WriteMatrix ; { for testing }
    var i: integer; j: char;
    begin
    for i := 1 to 9 do begin
      for j := 'A' to 'I' do write(A[i, j]:2) ;
      writeln
      end { for i } ;
    writeln
    end { WriteMatrix } ;

  procedure SetUp ;
    var f: text; i: integer; j: char;
    begin
    assign(f, 'inverse.dat') ;
    reset(f) ;
    for i := 1 to 9 do begin
      for j := 'A' to 'I' do read(f, A[i, j]) ;
      readln(f) ;
      end { for i } ;
    close(f) ;
    if Test then begin
      writeln('The inverse matrix is:') ;
      WriteMatrix
      end { if }
    end { SetUp } ;

  procedure Solve ; { by matrix-vector multiplication }
    var i: integer; j: char;
    begin
    for i := 1 to 9 do begin
      t[i] := 0 ;
      for j := 'A' to 'I' do
        t[i] := (t[i] + A[i, j] * 3 * s[j]) mod 4
      end { for i }
    end { Solve } ;

  procedure Solution ;
    var p, m: integer;
    begin
    n := 0 ;
    for p := 1 to 9 do
      for m := 1 to t[p] do begin inc(n) ; move[n] := p end
    end { Solution } ;

  begin { ComputeAnswer }
  SetUp ;
  Solve ;
  Solution
  end { ComputeAnswer } ;

procedure WriteOutput ;
  var m: integer;
  begin
  if Test then begin
    write('A ', n:1, '-move solution is:') ;
    for m := 1 to n do
      write(move[m]:2) ;
    writeln
    end { if } ;
  for m := 1 to n do
    write(out, move[m]:2) ;
  writeln(out)
  end { WriteOutput } ;

begin
Init ;
ReadInput ;
ComputeAnswer ;
WriteOutput ;
Fini
end.
