import std.stdio;
import core.stdc.stdio;
import std.algorithm;
import std.container;
import std.typecons;

void main(){
	int m,n,k;
	scanf("%d%d%d",&m,&n,&k);
	alias Tuple!(int,"v",int,"i") vi;
	vi[][int] xi;
	vi[][int] yi;
	int si=k*2,gi=k*2+1;
	foreach(i;0..k){
		int x,y;
		scanf("%d%d",&x,&y);
		xi[x]~=vi(y,i*2);
		yi[y]~=vi(x,i*2+1);
		if(x==1&&y==1)
			si=i*2;
		if(x==m&&y==n)
			gi=i*2;
	}
	if(si==k*2)
		xi[1]~=vi(1,si);
	if(gi==k*2+1){
		xi[m]~=vi(n,gi);
		yi[n]~=vi(m,gi+1);
	}
	vi[][] graph = new vi[][k*2+3];
	foreach(xs;xi){
		sort(xs);
		foreach(int i;0..xs.length-1){
			graph[xs[i].i]~=vi(xs[i+1].v-xs[i].v,xs[i+1].i);
			graph[xs[i+1].i]~=vi(xs[i+1].v-xs[i].v,xs[i].i);
		}
	}
	foreach(ys;yi){
		sort(ys);
		foreach(int i;0..ys.length-1){
			graph[ys[i].i]~=vi(ys[i+1].v-ys[i].v,ys[i+1].i);
			graph[ys[i+1].i]~=vi(ys[i+1].v-ys[i].v,ys[i].i);
		}
	}
	foreach(i;0..k){
		graph[i*2]~=vi(1,i*2+1);
		graph[i*2+1]~=vi(1,i*2);
	}
	alias Tuple!(long,"cost",int,"i") ci;
	auto pq = BinaryHeap!(ci[],"a>b")(new ci[1919810],0);
	pq.insert(ci(0,si));
	long ans=-1;
	long[] nc = new long[k*2+3];
	nc[] = 1145141919810893;
	while(!pq.empty){
		auto t=pq.front;
		pq.popFront;
		with(t){
			if(cost>nc[i])
				continue;
			if(i==gi||i==gi+1){
				ans=cost;
				break;
			}
			foreach(c;graph[i])
				if(cost+c.v<nc[c.i]){
					nc[c.i]=cost+c.v;
					pq.insert(ci(cost+c.v,c.i));
				}
		}
	}
	printf("%lld\n",ans);
}