/*
TASK:lab
LANG:C++
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define MAXN 42
# define MAXCO 12
# define MAXC MAXN*MAXN*MAXN*MAXCO*MAXCO
// Za Gery

char co[MAXN][MAXN][MAXN];
char ch[MAXN][MAXN][MAXN];
char *used;
int *comes;
char *move;
int *tail;
int n,sta,end1,end2,end3;
void readf() {
	int a,b,c;
    char buf,buf1;
//	freopen("lab.in","r",stdin);
    scanf("%d",&n);
    used = (char *)malloc((n+2)*(n+2)*(n+2)*MAXCO*MAXCO*sizeof(char));
    comes = (int *)malloc((n+2)*(n+2)*(n+2)*MAXCO*MAXCO*sizeof(int));
    tail = (int *)malloc((n+2)*(n+2)*(n+2)*MAXCO*MAXCO*sizeof(int));
    move = (char *)malloc((n+2)*(n+2)*(n+2)*MAXCO*MAXCO*sizeof(char));
    for ( int i=0 ; i<(n+2)*(n+2)*(n+2)*MAXCO*MAXCO ; i++ ) {
    	used[i]=0;
        tail[i]=0;
        move[i]=0;
        comes[i]=0;
    }
    scanf("%d%d%d",&a,&b,&c);
    sta = (a*(n+1)+b)*(n+1)+c;
    scanf("%d%d%d\n",&end1,&end2,&end3);

    scanf("%c%c",&buf,&buf1);
    sta = (sta*MAXCO+buf-'A'+1)*MAXCO+buf1-'A'+1;

    scanf("%c",&buf);

    for ( int i=1 ; i<=n ; i++ ) {
    	for ( int j=1 ; j<=n ; j++ ) {
        	for ( int l=1 ; l<=n ; l++ ) {
            	scanf("%c",&co[j][l][i]);
                co[j][l][i]-='A'-1;
            }
            scanf("%c",&buf);
        }
    }

    for ( int i=1 ; i<=n ; i++ ) {
    	for ( int j=1 ; j<=n ; j++ ) {
        	for ( int l=1 ; l<=n ; l++ ) {
            	scanf("%c",&ch[j][l][i]);
                ch[j][l][i]-='A'-1;
            }
            scanf("%c",&buf);
        }
    }
}
int code (int *st) {
	return (((st[0]*(n+1)+st[1])*(n+1)+st[2])*MAXCO+st[3])*MAXCO+st[4];
}
void dec(int *st, int k) {
	st[4] = k%MAXCO;
    k/=MAXCO;
    st[3] = k%MAXCO;
    k/=MAXCO;
    st[2] = k%(n+1);
    k/=(n+1);
    st[1] = k%(n+1);
    k/=(n+1);
    st[0] = k;
}
void writef(int p,int br) {
    if ( comes[p] != 0 ) {
    	writef(comes[p],br+1);
        printf("%c",move[p]);
    } else {
	    printf("%d\n",br+1);
        printf("%c",move[p]);
    }
    
}
void bfs() {
//	printf("tuk");
    int next = sta;
    int st[6],h,t;
    int p1,p2,p3;
    p1 = 0; p2 = p3 = 1; tail[0] = next; comes[0] = 0; used[sta] = 1;
    do {
    	for ( int i=p1 ; i<p2 ; i++ ) {
        	dec(st,tail[i]);
            if ( st[0]==end1 && st[1]==end2 && st[2]==end3 ) {
  //          	printf("here");
            	writef(i,0);
                printf("\n");
                exit(0);
            }
            if ( st[0]<n && (co[st[0]+1][st[1]][st[2]]==st[3] || co[st[0]+1][st[1]][st[2]]==st[4] ) ) {
            	st[0]++;
                h = code(st);
                if ( used[h]==0 ) {
                	tail[p3] = h;
                    move[p3] = 'X';
                    comes[p3++] = i;
                    used[h] = 1;
                }
                st[0]--;
            }
            if ( st[1]<n && (co[st[0]][st[1]+1][st[2]]==st[3] || co[st[0]][st[1]+1][st[2]]==st[4] ) ) {
            	st[1]++;
                h = code(st);
                if ( used[h]==0 ) {
                	tail[p3] = h;
                    move[p3] = 'Y';
                    comes[p3++] = i;
                    used[h] = 1;
                }
                st[1]--;
            }
            if ( st[2]<n && (co[st[0]][st[1]][st[2]+1]==st[3] || co[st[0]][st[1]][st[2]+1]==st[4] ) ) {
            	st[2]++;
                h = code(st);
                if ( used[h]==0 ) {
                	tail[p3] = h;
                    move[p3] = 'Z';
                    comes[p3++] = i;
                    used[h] = 1;
                }
                st[2]--;
            }
            if ( st[0]>1 && (co[st[0]-1][st[1]][st[2]]==st[3] || co[st[0]-1][st[1]][st[2]]==st[4] ) ) {
            	st[0]--;
                h = code(st);
                if ( used[h]==0 ) {
                	tail[p3] = h;
                    move[p3] = 'U';
                    comes[p3++] = i;
                    used[h] = 1;
                }
                st[0]++;
            }
            if ( st[1]>1 && (co[st[0]][st[1]-1][st[2]]==st[3] || co[st[0]][st[1]-1][st[2]]==st[4] ) ) {
            	st[1]--;
                h = code(st);
                if ( used[h]==0 ) {
                	tail[p3] = h;
                    move[p3] = 'V';
                    comes[p3++] = i;
                    used[h] = 1;
                }
                st[1]++;
            }
            if ( st[2]>1 && (co[st[0]][st[1]][st[2]-1]==st[3] || co[st[0]][st[1]][st[2]-1]==st[4] ) ) {
            	st[2]--;
                h = code(st);
                if ( used[h]==0 ) {
                	tail[p3] = h;
                    move[p3] = 'W';
                    comes[p3++] = i;
                    used[h] = 1;
                }
                st[2]++;
            }
            if ( ch[st[0]][st[1]][st[2]]!= 15 ) {
            	if ( st[3] != ch[st[0]][st[1]][st[2]]) {
                	t = st[3];
                    st[3] = ch[st[0]][st[1]][st[2]];
                    h = code(st);
                    if ( used[h]==0 ) {
                		tail[p3] = h;
	                    move[p3] = t+'A'-1;
    	                comes[p3++] = i;
        	            used[h] = 1;
            	    }
                    st[3] = t;
                }
                if ( st[4] != ch[st[0]][st[1]][st[2]]) {
                	t = st[4];
                    st[4] = ch[st[0]][st[1]][st[2]];
                    h = code(st);
                    if ( used[h]==0 ) {
                		tail[p3] = h;
	                    move[p3] = t+'A'-1;
    	                comes[p3++] = i;
        	            used[h] = 1;
            	    }
                    st[4] = t;
                }
            }
        }
        p1 = p2;
        p2 = p3;
    }while(p1!=p2);
}
int main() {
	readf();
	bfs();
    //printf("1");
    return 0;
}
