bfs
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct Node
{
int dx,dy,dd;
int tx,ty,td;
}tmp;
int n;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int tvis[1100][1100],dvis[1100][1100];
void next(Node &u)
{
int x,y;
x=u.dx+dir[u.dd][0];
y=u.dy+dir[u.dd][1];
if(x>=0&&x<n&&y>=0&&y<n&&!dvis[x][y])
{
u.dx=x;
u.dy=y;
}
else
{
x=u.dx+dir[(u.dd+1)%4][0];
y=u.dy+dir[(u.dd+1)%4][1];
if(x>=0&&x<n&&y>=0&&y<n&&!dvis[x][y])
{
u.dx=x;
u.dy=y;
u.dd=(u.dd+1)%4;
}
}
x=u.tx+dir[u.td][0];
y=u.ty+dir[u.td][1];
if(x>=0&&x<n&&y>=0&&y<n&&!tvis[x][y])
{
u.tx=x;
u.ty=y;
}
else
{
x=u.tx+dir[(u.td-1+4)%4][0];
y=u.ty+dir[(u.td-1+4)%4][1];
if(x>=0&&x<n&&y>=0&&y<n&&!tvis[x][y])
{
u.tx=x;
u.ty=y;
u.td=(u.td-1+4)%4;
}
}
}
int bfs(Node u)
{
int dprex=-1,dprey=-1;
int tprex=-1,tprey=-1;
queue<Node> q;
dvis[u.dx][u.dy]=1;
tvis[u.tx][u.ty]=1;
q.push(u);
while(!q.empty())
{
u=q.front();
//printf("\n%d %d\n%d %d\n\n",u.dx,u.dy,u.tx,u.ty);
q.pop();
next(u);
//printf("\n%d %d\n%d %d\n\n",u.dx,u.dy,u.tx,u.ty);
if(u.dx==u.tx&&u.dy==u.ty)
{
tmp=u;
return 1;
}
else
{
if(dprex==u.dx&&dprey==u.dy&&tprex==u.tx&&tprey==u.ty)
return 0;
dvis[u.dx][u.dy]=1;
tvis[u.tx][u.ty]=1;
dprex=u.dx;dprey=u.dy;
tprex=u.tx;tprey=u.ty;
q.push(u);
}
}
return 0;
}
int main()
{
while(scanf("%d",&n)&&n)
{
memset(dvis,0,sizeof(dvis));
memset(tvis,0,sizeof(tvis));
scanf("%d%d%d",&tmp.dx,&tmp.dy,&tmp.dd);
scanf("%d%d%d",&tmp.tx,&tmp.ty,&tmp.td);
if(tmp.dx==tmp.tx&&tmp.dy==tmp.ty)
{
printf("%d %d\n",tmp.dx,tmp.dy);
continue;
}
if(bfs(tmp)==0)
printf("-1\n");
else
printf("%d %d\n",tmp.dx,tmp.dy);
}
return 0;
}