/*
TASK: lab
LANG: C
*/
#include <stdio.h>

#define MAX 41*41*41*11*3

char col[41][41][41];
char change[41][41][41];
int prev[MAX];
char move[MAX];
int q[MAX];
int n,rx,ry,rz,jx,jy,jz;
char C1,C2;
char stack[MAX];

int t[6][3] = {
{1,0,0},
{0,1,0},
{0,0,1},
{-1,0,0},
{0,-1,0},
{0,0,-1}
};

char let[6] = {'X','Y','Z','U','V','W'};

int encode(int x,int y,int z,char c1,char c2) {
int ch1 = c1-'A'+1;
int ch2 = c2-'A'+1;
    return (x-1)*n*n*10*3+(y-1)*n*10*3+(z-1)*10*3+(ch1-1)*3+ch2-1;
}

void decode(int what,int *x,int *y,int *z,char *c1,char *c2) {
int ch1,ch2;
    *x = what/(n*n*10*3);
    what-=(n*n*10*3)*(*x);
    *y = what/(n*10*3);
    what-=(n*10*3)*(*y);
    *z = what/(10*3);
    what-=(10*3)*(*z);
    ch1 = what/3;
    what-=3*ch1;
    ch2 = what;
    (*x)++;
    (*y)++;
    (*z)++;
    *c1 = ch1+'A';
    *c2 = ch2+'A';
}

void test () {
int x,y,z;
char c1,c2;
int res;
    res = encode(30,30,30,'A','A');
    decode(res,&x,&y,&z,&c1,&c2);
    printf("%d %d %d %c %c\n",x,y,z,c1,c2);    
}

int can(int x,int y,int z) {
    return (x>0 && y>0 && z>0 && x<=n && y<=n && z<=n);
}

int canmove(int x,int y,int z,char c1,char c2) {
int res;

    if (!can(x,y,z)) return 0;
    if (c1!=col[x][y][z] && c2!=col[x][y][z])
       return 0;
    if (c1==col[x][y][z])
       c1 = c2;
    c2 = 'A';
    res = encode(x,y,z,c1,c2);
    if (prev[res]==-1) return 1;
    else
    return 0;
}


void BFS() {
char c1,c2,ch2,cc1,cc2;
int i,index,x,y,z,top,size,res,stsz,ox,oy,oz;
     for (i=0;i<MAX;i++)
         prev[i] = -1;
     top = 1;
     size = 0;
     x = rx;
     y = ry;
     z = rz;
     c1 = C1;
     c2 = C2;
     for (i=0;i<6;i++) {
             ox = t[i][0];
             oy = t[i][1];
             oz = t[i][2];
             if (canmove(x+ox,y+oy,z+oz,c1,c2)) {
               size++;
               cc1 = c1;
               cc2 = c2;
               if (cc1==col[x+ox][y+oy][z+oz])
                 cc1=cc2;
               cc2 = 'A';
               res = encode(x+ox,y+oy,z+oz,cc1,cc2);
               q[size] = res;
               prev[res] = -2;
               move[res] = let[i];
             }
     }
     if (change[x][y][z]!='O') {
        res = encode(x,y,z,c1,'B');
        if (prev[res]==-1) {
              size++;
              q[size] = res;
              prev[res] = -2;
              move[res] = c2;
        }
        res = encode(x,y,z,c2,'B');
        if (prev[res]==-1) {
              size++;
              q[size] = res;
              prev[res] = -2;
              move[res] = c1;
        }
     }
     while (top<=size) {
           index = q[top++];
           decode(index,&x,&y,&z,&c1,&ch2);
           c2 = change[x][y][z];
           if (ch2=='A') {
              c2 = col[x][y][z];
           }
           if (x==jx && y==jy && z==jz) {
              // print solution
              stsz = 0;
              res = index;
              while (res!=-2) {
                    stack[stsz++] = move[res];
                    res = prev[res];
              }
              printf("%d\n",stsz);
              for (i=stsz-1;i>=0;i--)
                  printf("%c",stack[i]);
              printf("\n");
              return;
           }
           for (i=0;i<6;i++) {
             ox = t[i][0];
             oy = t[i][1];
             oz = t[i][2];
             if (canmove(x+ox,y+oy,z+oz,c1,c2)) {
               size++;
               cc1 = c1;
               cc2 = c2;
               if (cc1==col[x+ox][y+oy][z+oz])
                 cc1=cc2;
               cc2 = 'A';
               res = encode(x+ox,y+oy,z+oz,cc1,cc2);
               q[size] = res;
               prev[res] = index;
               move[res] = let[i];
             }
           }
           if (change[x][y][z]!='O') {
              res = encode(x,y,z,c1,'B');
              if (prev[res]==-1) {
                 size++;
                 q[size] = res;
                 prev[res] = index;
                 move[res] = c2;
              }
              res = encode(x,y,z,c2,'B');
              if (prev[res]==-1) {
                 size++;
                 q[size] = res;
                 prev[res] = index;
                 move[res] = c1;
              }
           }
     }
     printf("No solution %d %d!\n",size,MAX);
}

int main () {
int x,y,z;
char ch;
//    freopen ( "lab.in" , "r" , stdin);
    scanf("%d%d%d%d%d%d%d\n%c%c\n",&n,&rx,&ry,&rz,&jx,&jy,&jz,&C1,&C2);
    for (z=1;z<=n;z++) {
        for (x=1;x<=n;x++) {
            for (y=1;y<=n;y++)
                scanf("%c",&col[x][y][z]);
            scanf("\n");
        }
    }
    for (z=1;z<=n;z++) {
        for (x=1;x<=n;x++) {
            for (y=1;y<=n;y++)
                scanf("%c",&change[x][y][z]);
            scanf("\n");
        }
    }
    if (rx==jx && ry==jy && rz==jz) {
       printf("0\n");
       return 0;
    }
    //test();
    BFS();
    return 0;
}

