#include<iostream>
using namespace std;
#define MAXSIZE 10000
class QElemType
{
public:
int x,y;
};
class SQueue
{
public:
int len;
int front;//头指针,若队列不空,指向队列头元素
int rear;//尾指针,若队列不空,指向队列尾元素的下一个位置
QElemType base[MAXSIZE];
int InitQueue();//初始化一个队伍
int QueueLength();//求队伍长度
int EnQueue(QElemType e);//插入元素为e的新的队尾元素
int DeQueue(QElemType &e);//若队列不空,则删除队列的队头元素,用e返回,并返回1,否则返回0
};
int SQueue::InitQueue()
{
len=front=rear=0;
return 1;
}
int SQueue::QueueLength()
{
return (rear-front+MAXSIZE)%MAXSIZE;
}
int SQueue::EnQueue(QElemType e)//插入元素为e的新的队尾元素
{
if((rear+1)%MAXSIZE==front)return 0;//队满,返回0
base[rear]=e;
rear=(rear+1)%MAXSIZE;
len++;
return 1;
}
int SQueue::DeQueue(QElemType &e)
{
if(front==rear)return 0;//队空,返回0
e=base[front];
front=(front+1)%MAXSIZE;
len--;
return 1;
}
int steps[1001][1001];
bool map[1001][1001];//true为可以走,false为不可以
//将所有的点坐标+500,fj从(500,500)开始
int main()
{
int x,y;
int n;
int i,j;
int endx,endy;
QElemType offset[4];
QElemType point,point1;
SQueue queue;
offset[0].x=0;
offset[0].y=1;
offset[1].x=0;
offset[1].y=-1;
offset[2].x=-1;
offset[2].y=0;
offset[3].x=1;
offset[3].y=0;
memset(steps,1000000,sizeof(steps));
memset(map,true,sizeof(map));
steps[500][500]=0;
scanf("%d%d%d",&endx,&endy,&n);
endx+=500;
endy+=500;
for(i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
x+=500;
y+=500;
map[x][y]=false;
}
queue.InitQueue();
//初始化起点周围四个
for(i=0;i<4;i++)
{
point.x=500+offset[i].x;
point.y=500+offset[i].y;
if(map[point.x][point.y])//可以走
{
steps[point.x][point.y]=1;
queue.EnQueue(point);
}
}
while(queue.len>0)
{
queue.DeQueue(point);
for(i=0;i<4;i++)
{
point1.x=point.x+offset[i].x;
point1.y=point.y+offset[i].y;
if(map[point1.x][point1.y]&&steps[point.x][point.y]+1<steps[point1.x][point1.y])
{
steps[point1.x][point1.y]=steps[point.x][point.y]+1;
if(point1.x==endx&&point1.y==endy)
{
printf("%d\n",steps[point1.x][point1.y]);
return 0;
}
queue.EnQueue(point1);
}
}//for
}//while
}
using namespace std;
#define MAXSIZE 10000
class QElemType
{
public:
int x,y;
};
class SQueue
{
public:
int len;
int front;//头指针,若队列不空,指向队列头元素
int rear;//尾指针,若队列不空,指向队列尾元素的下一个位置
QElemType base[MAXSIZE];
int InitQueue();//初始化一个队伍
int QueueLength();//求队伍长度
int EnQueue(QElemType e);//插入元素为e的新的队尾元素
int DeQueue(QElemType &e);//若队列不空,则删除队列的队头元素,用e返回,并返回1,否则返回0
};
int SQueue::InitQueue()
{
len=front=rear=0;
return 1;
}
int SQueue::QueueLength()
{
return (rear-front+MAXSIZE)%MAXSIZE;
}
int SQueue::EnQueue(QElemType e)//插入元素为e的新的队尾元素
{
if((rear+1)%MAXSIZE==front)return 0;//队满,返回0
base[rear]=e;
rear=(rear+1)%MAXSIZE;
len++;
return 1;
}
int SQueue::DeQueue(QElemType &e)
{
if(front==rear)return 0;//队空,返回0
e=base[front];
front=(front+1)%MAXSIZE;
len--;
return 1;
}
int steps[1001][1001];
bool map[1001][1001];//true为可以走,false为不可以
//将所有的点坐标+500,fj从(500,500)开始
int main()
{
int x,y;
int n;
int i,j;
int endx,endy;
QElemType offset[4];
QElemType point,point1;
SQueue queue;
offset[0].x=0;
offset[0].y=1;
offset[1].x=0;
offset[1].y=-1;
offset[2].x=-1;
offset[2].y=0;
offset[3].x=1;
offset[3].y=0;
memset(steps,1000000,sizeof(steps));
memset(map,true,sizeof(map));
steps[500][500]=0;
scanf("%d%d%d",&endx,&endy,&n);
endx+=500;
endy+=500;
for(i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
x+=500;
y+=500;
map[x][y]=false;
}
queue.InitQueue();
//初始化起点周围四个
for(i=0;i<4;i++)
{
point.x=500+offset[i].x;
point.y=500+offset[i].y;
if(map[point.x][point.y])//可以走
{
steps[point.x][point.y]=1;
queue.EnQueue(point);
}
}
while(queue.len>0)
{
queue.DeQueue(point);
for(i=0;i<4;i++)
{
point1.x=point.x+offset[i].x;
point1.y=point.y+offset[i].y;
if(map[point1.x][point1.y]&&steps[point.x][point.y]+1<steps[point1.x][point1.y])
{
steps[point1.x][point1.y]=steps[point.x][point.y]+1;
if(point1.x==endx&&point1.y==endy)
{
printf("%d\n",steps[point1.x][point1.y]);
return 0;
}
queue.EnQueue(point1);
}
}//for
}//while
}