from heapq import heapify, heappop, heappush H, W, N = map(int, input().split()) D = {} for _ in range(N): a, b, c, d = map(int, input().split()) a -= 1; b -= 1; c -= 1; d -= 1 D[(a, b)] = (c, d) dist = [10 ** 18] * (H * W) dist[0] = 0 pq = [(0, 0)] heapify(pq) dx = [-1, 0, 0, 1] dy = [0, -1, 1, 0] while len(pq) > 0: d, id = heappop(pq) x, y = id // W, id % W if (x, y) in D: v, w = D[(x, y)] if dist[v * W + w] > dist[id] + 1: dist[v * W + w] = dist[id] + 1 heappush(pq, (dist[v * W + w], v * W + w)) for i in range(4): nx = x + dx[i] ny = y + dy[i] if 0 > nx or nx >= H or 0 > ny or ny >= W: continue if dist[nx * W + ny] > dist[id] + 1: dist[nx * W + ny] = dist[id] + 1 heappush(pq, (dist[nx * W + ny], nx * W + ny)) print(dist[(H - 1) * W + (W - 1)])