/*
TASK:lab
LANG:C++
*/
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;

const int N = 31;
const int M = 10;

const int UNKNOWN = -2;
const int inf = 10789;

struct Type{
	Type(){}
  Type(int _x, int _y, int _z, int _k1, int _k2, int _time, int _prev){
  	x = _x;
    y = _y;
    z = _z;
    k1 = _k1;
    k2 = _k2;
    time = _time;
    prev = _prev;
  }
	int x, y, z;
  int k1, k2;
  int time;
  int prev;
};


vector<int> res;

int T[N][N][N][M][M];
int P[N][N][N][M][M];
int map[N][N][N];
int key[N][N][N];

int Rx, Ry, Rz, Jx, Jy, Jz;
int i, j, n;
int x, y, z;
int key1, key2;
int prev;

void Input(){

//	freopen("lab.in", "rt", stdin);

  scanf("%d%d%d%d%d%d%d", &n, &Rx, &Ry, &Rz, &Jx, &Jy, &Jz);
  char ch;
  scanf("%c", &ch);
  scanf("%c", &ch);
  key1 = ch - 'A';
  scanf("%c", &ch);
  key2 = ch - 'A';
	scanf("%c", &ch);
  for(z=1; z<=n; z++)
  	for(x=1; x<=n; x++){
    	for(y=1; y<=n; y++){
      	scanf("%c", &ch);
        map[x][y][z] = ch - 'A';
      }
      scanf("%c", &ch);
    }
 	for(z=1; z<=n; z++)
	  for(x=1; x<=n; x++){
    	for(y=1; y<=n; y++){
      	scanf("%c", &ch);
        key[x][y][z] = ch - 'A';
      }
      scanf("%c", &ch);
    }
}


void swap(int &p, int &q){
	int z = p;
  p = q;
  q = z;
}

void Solve(){

  queue<Type> q;
  Type t;
  
  q.push(Type(Rx, Ry, Rz, key1, key2, 0, 666));

  while(!q.empty()){

    t = q.front();
    q.pop();

    if(T[t.x][t.y][t.z][t.k1][t.k2] <= t.time)
    	continue;
    if(!(t.x>=1 && t.x<=n && t.y>=1 && t.y<=n && t.z>=1 && t.z<=n))
    	continue;
      
    T[t.x][t.y][t.z][t.k1][t.k2] = t.time;
    P[t.x][t.y][t.z][t.k1][t.k2] = t.prev;
    T[t.x][t.y][t.z][t.k2][t.k1] = t.time;
    P[t.x][t.y][t.z][t.k2][t.k1] = t.prev;

    int reload = key[t.x][t.y][t.z];

    if(reload != t.k1 && reload != t.k2 && reload != 'O'-'A'){
			q.push(Type(t.x, t.y, t.z, t.k1, reload, t.time+1, t.k2));
      q.push(Type(t.x, t.y, t.z, t.k2, reload, t.time+1, t.k1));
		}

		bool doit = false;

    again:
    doit = !doit;
    //move X
    if(map[t.x+1][t.y][t.z] == t.k1){
    	q.push(Type(t.x+1,t.y,t.z,t.k1,t.k2,t.time+1, -10));
    }
    if(map[t.x-1][t.y][t.z] == t.k1){
    	q.push(Type(t.x-1,t.y,t.z,t.k1,t.k2,t.time+1, -11));
    }
    //move Y
    if(map[t.x][t.y+1][t.z] == t.k1){
    	q.push(Type(t.x,t.y+1,t.z,t.k1,t.k2,t.time+1, -20));
    }
    if(map[t.x][t.y-1][t.z] == t.k1){
    	q.push(Type(t.x,t.y-1,t.z,t.k1,t.k2,t.time+1, -21));
    }
		//move Z
    if(map[t.x][t.y][t.z+1] == t.k1){
    	q.push(Type(t.x,t.y,t.z+1,t.k1,t.k2,t.time+1, -30));
    }
    if(map[t.x][t.y][t.z-1] == t.k1){
    	q.push(Type(t.x,t.y,t.z-1,t.k1,t.k2,t.time+1, -31));
    }    

		if(doit){
	    swap(t.k1, t.k2);
    	goto again;
  	}

  }

}

void Output(Type b){

  printf("sdklfjlksd\n");
}

void WriteOutputData(){
	for(i=res.size()-1; i>=0; i--){
  	if(res[i] < 0){
    	if(res[i] == -10)			printf("X");
    	if(res[i] == -11)			printf("U");
    	if(res[i] == -20)			printf("Y");
    	if(res[i] == -21)			printf("V");
    	if(res[i] == -30)			printf("Z");
    	if(res[i] == -31)			printf("W");
    }
    else{
    	printf("%c", res[i] + 'A');
    }
  }
  printf("\n");
}

int main(){
	Input();
  for(x=0; x<=n+1; x++)
  	for(y=0; y<=n+1; y++)
    	for(z=0; z<=n+1; z++)
      	for(int k1=0; k1<=10; k1++)
        	for(int k2=0; k2<=10; k2++)
          	T[x][y][z][k1][k2] = inf;

  Solve();

  Type b;
  b.time = inf;
  
  for(i=0; i<10; i++)
  	for(j=0; j<10; j++){
    	if(T[Jx][Jy][Jz][i][j] < b.time){
      	b.time = T[Jx][Jy][Jz][i][j];
        b.k1 = i;
        b.k2 = j;
      }
    }
	b.x = Jx;
  b.y = Jy;
  b.z = Jz;

//  res.push_back(3);
//  Output(best);

  while(1){

    prev = P[b.x][b.y][b.z][b.k1][b.k2];
    if(prev == 666)
    	break ;
		if(res.size()>10){
    	printf("asdjkasdkl\n");
      break;
    }
   	res.push_back(prev);

    if(prev < 0){
      if(prev == -11)			b.x++;
      if(prev == -10)			b.x--;
      if(prev == -21)			b.y++;
      if(prev == -20)			b.y--;
      if(prev == -31)			b.z++;
      if(prev == -30)			b.z--;
  	}
    else{
      b.k2 = prev;
    }//*/break;
	}

  printf("%d\n", b.time);
  
  WriteOutputData();

	return 0;
}
