# include <bits/stdc++.h>
using namespace std;
char g[ 2 ] [ 4 ] ;
const int N = 10 ;
unordered_map< string, pair< char , string> > pre;
unordered_map< string, int > d;
void Set ( string s) {
for ( int i= 0 ; i< 4 ; i++ ) g[ 0 ] [ i] = s[ i] ;
for ( int i= 7 , j= 0 ; i> 3 ; i-- , j++ ) g[ 1 ] [ j] = s[ i] ;
}
string get ( ) {
string res;
for ( int i= 0 ; i< 4 ; i++ ) res += g[ 0 ] [ i] ;
for ( int i= 3 ; i>= 0 ; i-- ) res += g[ 1 ] [ i] ;
return res;
}
string move0 ( string s) {
Set ( s) ;
for ( int i= 0 ; i< 4 ; i++ ) swap ( g[ 0 ] [ i] , g[ 1 ] [ i] ) ;
return get ( ) ;
}
string move1 ( string s) {
Set ( s) ;
char v0 = g[ 0 ] [ 3 ] , v1 = g[ 1 ] [ 3 ] ;
for ( int i= 2 ; i>= 0 ; i-- ) {
g[ 0 ] [ i+ 1 ] = g[ 0 ] [ i] ;
g[ 1 ] [ i+ 1 ] = g[ 1 ] [ i] ;
}
g[ 0 ] [ 0 ] = v0, g[ 1 ] [ 0 ] = v1;
return get ( ) ;
}
string move2 ( string s) {
Set ( s) ;
char v0 = g[ 0 ] [ 1 ] ;
g[ 0 ] [ 1 ] = g[ 1 ] [ 1 ] ;
g[ 1 ] [ 1 ] = g[ 1 ] [ 2 ] ;
g[ 1 ] [ 2 ] = g[ 0 ] [ 2 ] ;
g[ 0 ] [ 2 ] = v0;
return get ( ) ;
}
int bfs ( string start, string end) {
if ( start == end) return 0 ;
queue< string> q;
q. push ( start) ;
d[ start] = 0 ;
while ( q. size ( ) ) {
auto t = q. front ( ) ; q. pop ( ) ;
string m[ 3 ] ;
m[ 0 ] = move0 ( t) ;
m[ 1 ] = move1 ( t) ;
m[ 2 ] = move2 ( t) ;
for ( int i= 0 ; i< 3 ; i++ ) {
string a = m[ i] ;
if ( ! d. count ( a) ) {
pre[ a] = { 'A' + i, t} ;
d[ a] = d[ t] + 1 ;
q. push ( a) ;
if ( a == end) return d[ end] ;
}
}
}
return - 1 ;
}
int main ( ) {
string start, end;
char x;
for ( int i= 0 ; i< 8 ; i++ ) {
cin >> x;
end += x;
}
for ( int i= 0 ; i< 8 ; i++ ) start += ( '1' + i) ;
cout << bfs ( start, end) << endl;
string res;
while ( start != end) {
res += pre[ end] . first;
end = pre[ end] . second;
}
reverse ( res. begin ( ) , res. end ( ) ) ;
cout << res;
return 0 ;
}