/*
TASK:wappo
LANG:C++
*/
#include <iostream>
#define maxN 200
using namespace std;
typedef struct Poz { int x,y,w8,step; };

int A[maxN+1][maxN+1][4],T[maxN+1][maxN+1],V[maxN+1][maxN+1];
int N,TN,best=maxN*maxN,P[maxN+1],R[maxN+1],cnt;
Poz Hero,M1,M2,Super;

void init();
void scan(Poz h,Poz m1,Poz m2,Poz s);
void Add();
Poz move(Poz hero,Poz mon);

int main()
{
 init();
 scan(Hero,M1,M2,Super);
 cout<<best<<endl;
 for (int br=1;br<=best;br++)
    cout<<R[br]<<endl;
 return 0;
}

void init()
{
 int i,j,x,y;
 for (x=1;x<=maxN;x++)
    for (y=1;y<=maxN;y++)
       for (i=0;i<4;i++) A[x][y][i]=1;
 cin>>N>>Hero.y>>Hero.x>>M1.y>>M1.x>>M2.y>>M2.x>>TN;
 Hero.step=1;M1.step=2;M2.step=2;Super.step=3;
 Super.x=maxN;Super.y=maxN;
 for (i=1;i<=TN;i++) {
    cin>>y>>x;
    T[x][y]=1; }
 for (j=1;j<=N;j++)
    for (i=1;i<=N;i++) {
       cin>>x;
       if (x%2==1) A[i][j][2]=0;
       if ((x/2)%2==1) A[i][j][3]=0;
       if ((x/4)%2==1) A[i][j][0]=0;
       if ((x/8)%2==1) A[i][j][1]=0; }
}

void scan(Poz h,Poz m1,Poz m2,Poz s)
{
 if (V[h.x][h.y]) return;
 V[h.x][h.y]=1;
 if (h.x==0||h.y==0||h.x>N||h.y>N) { Add();V[h.x][h.y]=0;return; }
 if (h.x==m1.x&&h.y==m1.y) {  V[h.x][h.y]=0;return; }
 if (h.x==m2.x&&h.y==m2.y) {  V[h.x][h.y]=0;return; }
 if (h.x==s.x&&h.y==s.y) {  V[h.x][h.y]=0;return; }
 if (T[h.x][h.y]) return;
 if (m1.x==m2.x&&m1.y==m2.y) { s.x=m1.x;s.y=m2.y; }
 if (h.x!=Hero.x||h.y!=Hero.y) {
    if (!m1.w8) m1=move(h,m1);
       else m1.w8--;
    if (!m1.w8) m1=move(h,m1);
    if (!m2.w8) m2=move(h,m2);
       else m2.w8--;
    if (!m2.w8) m2=move(h,m2);
    s=move(h,s);
    s=move(h,s);
    s=move(h,s); }
 if (h.x==m1.x&&h.y==m1.y) { V[h.x][h.y]=0;return; }
 if (h.x==m2.x&&h.y==m2.y) {  V[h.x][h.y]=0;return; }
 if (h.x==s.x&&h.y==s.y) {  V[h.x][h.y]=0; return; }
 Poz nh=h;
 if (A[h.x][h.y][0]) {
    nh.y--;P[++cnt]=0;scan(nh,m1,m2,s);P[cnt--]=0;nh.y++; }
 if (A[h.x][h.y][1]) {
    nh.y++;P[++cnt]=1;scan(nh,m1,m2,s);P[cnt--]=0;nh.y--; }
 if (A[h.x][h.y][2]) {
    nh.x--;P[++cnt]=2;scan(nh,m1,m2,s);P[cnt--]=0;nh.x++; }
 if (A[h.x][h.y][3]) {
    nh.x++;P[++cnt]=3;scan(nh,m1,m2,s);P[cnt--]=0;nh.x--; }
 V[h.x][h.y]=0;
}

void Add()
{
 if (cnt>=best) return;
 best=cnt;
 for (int i=1;i<=cnt;i++) R[i]=P[i];
}

Poz move(Poz hero,Poz mon)
{
 Poz Cr=mon;Cr.x=mon.x;Cr.y=mon.y;Cr.w8=mon.w8;Cr.step=mon.step;
 if (hero.x<mon.x&&A[mon.x][mon.y][2]) {
    Cr.x--;
    if (T[Cr.x][Cr.y]&&mon.step<3) Cr.w8=3;
    return Cr; }
 if (hero.x>mon.x&&A[mon.x][mon.y][3]) {
    Cr.x++;
    if (T[Cr.x][Cr.y]&&mon.step<3) Cr.w8=3;
    return Cr; }
 if (hero.y<mon.y&&A[mon.x][mon.y][0]) {
    Cr.y--;
    if (T[Cr.x][Cr.y]&&mon.step<3) Cr.w8=3;
    return Cr; }
 if (hero.y>mon.y&&A[mon.x][mon.y][1]) {
    Cr.y++;
    if (T[Cr.x][Cr.y]&&mon.step<3) Cr.w8=3;
    return Cr; }
 return Cr;
}
