/*
TASK:wappo
LANG:C++
*/
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# define MOD 6
# define L 8
# define MAXTAIL (1<<20)

int indx[4] = {-1,1,0,0};
int indy[4] = {0,0,-1,1};
int inds[4] = {2,3,0,1};

int t[MOD][MOD], trap[MOD][MOD];
int pos,cpos[L];
int n, tbr;
    int p1,p2;
    int tail[MAXTAIL], comes[MAXTAIL], moves[MAXTAIL];
    bool used[MAXTAIL];

    int code(int m[]) {
	int res=0;
	for ( int i=0 ; i<MOD ; i++ ) {
	    res*=MOD;
	    res+=m[i];
	}
	for ( int i=6 ; i<8 ; i++ ) {
	    res*=4;
	    res+=m[i];
	}
	return res;
    }
void decode(int num, int m[]) {
    for ( int i=7 ; i>5 ; i-- ) {
	m[i]=num%4;
	num/=4;
    }
    for ( int i=5 ; i>=0 ; i-- ) {
	m[i]=num%MOD;
	num/=MOD;
    }    
}


void readf() {
    int a,b;
//    freopen("wappo.in","r",stdin);
    scanf("%d",&n);
    for ( int i=0 ; i<6 ; i++ ) {
	scanf("%d",&cpos[i]);
	cpos[i]--;
    }
    pos = code(cpos);
    scanf("%d",&tbr);
    for ( int i=0 ; i<tbr ; i++ ) {
	scanf("%d %d",&a,&b);
	trap[a-1][b-1] = 1;
    }
    for ( int i=0 ; i<n ; i++ )
	for ( int j=0 ; j<n ; j++ ) {
	    scanf("%d",&t[i][j]);
	}
}

bool movemonst(int m[], int k) {
    int x,y,tra;
    x = k*2;
    y = k*2+1;
    tra = (k==1)? 6:7;
    if ( m[2]!=m[4] || m[3]!=m[5] ) {
	if ( m[tra] ) {
	    m[tra]--;
	    return 0;
	} 
	if ( m[y]<m[1] && (t[m[x]][m[y]]&(1<<inds[3]))==0 ) {
	    m[x] += indx[3];
	    m[y] += indy[3];
	    if ( trap[m[x]][m[y]]==1 ) {
		m[tra] = 3;
		return 0;		    
	    }
	    return 1;
	} else if ( m[y]>m[1] && (t[m[x]][m[y]]&(1<<inds[2]))==0 ) {
	    m[x] += indx[2];
	    m[y] += indy[2];
	    if ( trap[m[x]][m[y]]==1 ) {
		m[tra] = 3;
		return 0;		    
	    }
	    return 1;
	} else if ( m[x]<m[0] && (t[m[x]][m[y]]&(1<<inds[1]))==0 ) {
	    m[x] += indx[1];
	    m[y] += indy[1];
	    if ( trap[m[x]][m[y]]==1 ) {
		m[tra] = 3;
		return 0;		    
	    }
	    return 1;
	} else if ( m[x]>m[0] && (t[m[x]][m[y]]&(1<<inds[0]))==0 ) {
	    m[x] += indx[0];
	    m[y] += indy[0];
	    if ( trap[m[x]][m[y]]==1 ) {
		m[tra] = 3;
		return 0;		    
	    }
	    return 1;
	}
	return 0;	
    } else {
	//SUPER JUDOWISHTE
	if ( m[y]<m[1] && (t[m[x]][m[y]]&(1<<inds[3]))==0 ) {
	    m[x] += indx[3];
	    m[y] += indy[3];
	    m[x+2] += indx[3];
	    m[y+2] += indy[3];
	    return 1;
	} else if ( m[y]>m[1] && (t[m[x]][m[y]]&(1<<inds[2]))==0 ) {
	    m[x] += indx[2];
	    m[y] += indy[2];
	    m[x+2] += indx[2];
	    m[y+2] += indy[2];
	    return 1;
	} else if ( m[x]<m[0] && (t[m[x]][m[y]]&(1<<inds[1]))==0 ) {
	    m[x] += indx[1];
	    m[y] += indy[1];
	    m[x+2] += indx[1];
	    m[y+2] += indy[1];
	    return 1;
	} else if ( m[x]>m[0] && (t[m[x]][m[y]]&(1<<inds[0]))==0 ) {
	    m[x] += indx[0];
	    m[y] += indy[0];
	    m[x+2] += indx[0];
	    m[y+2] += indy[0];
	    return 1;
	}
	return 0;
    }
}

void writef(int mo, int poss, int cnt) {
    if ( poss==pos ) {
	printf("%d\n",cnt);
	printf("%d\n",mo);
    } else {
	writef(moves[poss],comes[poss],cnt+1);
	printf("%d\n",mo);
    }
}
void print(int cp[]) {
    for ( int i=0 ; i<8 ; i++ )
	printf("%d ",cp[i]);
    printf("\b\n");
}

void move(int poss) {
    int curpos[6],nextpos[6],cp;
    bool p,q;
    decode(poss,curpos);
//    print(curpos);
    for ( int i=0 ; i<4 ; i++ ) {
//	printf("%d %d\n",t[curpos[0]][curpos[1]]&(1<<inds[i]),trap[curpos[0]+indx[i]][curpos[1]+indy[i]] );
	if ( (t[curpos[0]][curpos[1]]&(1<<inds[i]))==0 && 
		(curpos[0]+indx[i]<0 || curpos[0]+indx[i]>=n || curpos[1]+indy[i]<0 || curpos[1]+indy[i]>=n 
		 || (trap[curpos[0]+indx[i]][curpos[1]+indy[i]]==0) )  ) {
//	    printf("tuk");

	    nextpos[0] = curpos[0]+indx[i];
	    nextpos[1] = curpos[1]+indy[i];
	    if ( nextpos[0]<0 || nextpos[0]>=n || nextpos[1]<0 || nextpos[1]>=n ) {
		writef(i,poss,1);
		exit(0);
	    }
	    memcpy(nextpos+2,curpos+2,6*sizeof(int));
//	    printf("next new");
//	    print(nextpos);
	    if ( curpos[2]!=curpos[4] || curpos[3]!=curpos[5] ) {
		p=movemonst(nextpos,1);
//		printf("next ");
  //  		print(nextpos);
		q=movemonst(nextpos,2);
//		printf("next ");
  //  		print(nextpos);
		if ( p ) movemonst(nextpos,1);
//		printf("next ");
  //  		print(nextpos);
		if ( q ) movemonst(nextpos,2);
//		printf("next end");
  //  		print(nextpos);
		if ( (nextpos[0]!=nextpos[2] || nextpos[1]!=nextpos[3])&&
			(nextpos[0]!=nextpos[4] || nextpos[1]!=nextpos[5]) ) {
		    
//		    printf("add\n");
		    cp = code(nextpos);
		    tail[p2] = cp;
		    moves[cp] = i;
		    comes[cp] = poss;
		    used[cp] = 1;
		    p2++;
		}
	    } else {
		movemonst(nextpos,1);
		movemonst(nextpos,1);
		movemonst(nextpos,1);
		if ( (nextpos[0]!=nextpos[2] || nextpos[1]!=nextpos[3])&&
			(nextpos[0]!=nextpos[4] || nextpos[1]!=nextpos[5]) ) {
//		    printf("add\n");
		    cp = code(nextpos);
		    tail[p2] = cp;
		    moves[cp] = i;
		    comes[cp] = poss;
		    used[cp] = 1;		    
		    p2++;
		}
	    }
	}
    }
}

void bfs() {
    p1 = 0; p2 = 1;
    tail[0] = pos;
    used[pos] = 1;
    while ( p1<p2 ) {
	move(tail[p1]);
	p1++;
    }
    printf("Exit %d\n",p1);
}

int main() {    
    readf();
    bfs();

    return 0;
}
