#include using namespace std; using ll = long long; int main () { ll H, W; int N; cin >> H >> W >> N; std::vector X(N * 2 + 2), Y(N * 2 + 2); X[0] = Y[0] = 1; X[1] = W, Y[1] = H; using P = pair; vector

gr[2020]; { ll d = H - 1 + W - 1; gr[0].emplace_back(d, 1); gr[1].emplace_back(d, 0); } for (int i = 2; i < N * 2 + 2; i ++) { cin >> Y[i] >> X[i]; for (int j = 0; j < i; j ++) { ll d = abs(X[i] - X[j]) + abs(Y[i] - Y[j]); gr[j].emplace_back(d, i); gr[i].emplace_back(d, j); } } for (int i = 1; i <= N; i ++) { int p = i * 2, q = i * 2 + 1; gr[p].emplace_back(1, q); gr[q].emplace_back(1, p); } priority_queue, greater

> pque; pque.emplace(0, 0); vector D(N * 2 + 2, (ll)1e18); D[0] = 0; while (!pque.empty()) { auto [l, u] = pque.top(); pque.pop(); if (l != D[u]) continue; for (auto& [d, v] : gr[u]) { if (D[v] > d + l) { D[v] = d + l; pque.emplace(D[v], v); } } } cout << D[1] << endl; }