from heapq import* h,w,n=map(int,input().split()) dh={1,h} dw={1,w} ab=[[*map(int,input().split())]for i in range(n)] for a,b,c,d in ab:dh|={a,c};dw|={b,d} dh=sorted(dh) dhd={j:i for i,j in enumerate(dh)} dw=sorted(dw) dwd={j:i for i,j in enumerate(dw)} D=[[[-1,None]for j in range(len(dw))]+[[1,None]]for i in range(len(dh))]+[[[1,None]]*(len(dw)+1)] for a,b,c,d in ab:D[dhd[a]][dwd[b]][1]=(dhd[c],dwd[d]) h=[(0,0,0)] while h: e,a,b=heappop(h) if D[a][b][0]<0: D[a][b][0]=e if D[a][b][1]: c,d=D[a][b][1] if D[c][d][0]<0:heappush(h,(e+1,c,d)) if D[a-1][b][0]<0:heappush(h,(e+dh[a]-dh[a-1],a-1,b)) if D[a+1][b][0]<0:heappush(h,(e+dh[a+1]-dh[a],a+1,b)) if D[a][b-1][0]<0:heappush(h,(e+dw[b]-dw[b-1],a,b-1)) if D[a][b+1][0]<0:heappush(h,(e+dw[b+1]-dw[b],a,b+1)) print(D[-2][-2][0])