#include <bits/stdc++.h>   using namespace std;   const int N = 1e4 + 10, inf = 1e9;   int r[N], c[N], dist[N];   bool seen[N] = {0};   int main() {     int row, col, k;     cin >> row >> col >> k;     for (int i = 1; i <= k; i++) {       cin >> r[i] >> c[i];       dist[i] = inf;       if (r[i] == 1 and c[i] == 1) dist[i] = 0;     }     dist[0] = inf;     while (1) {       int cur = 0;       for (int i = 1; i <= k; i++) {         if (!seen[i] and dist[cur] > dist[i]) {           cur = i;         }       }       if (!cur) break;       seen[cur] = 1;       for (int i = 1; i <= k; i++) {         if (seen[i]) continue;         if (abs(r[i] - r[cur]) > 2 and abs(c[i] - c[cur]) > 2) continue;         int temp = 1;         if ((abs(r[i] - r[cur]) + abs(c[i] - c[cur])) == 1) temp = 0;         if (dist[cur] + temp < dist[i]) dist[i] = temp + dist[cur];       }     }     int ans = inf;     for (int i = 1; i <= k; i++) {       if (r[i] == row and c[i] == col)         ans = min(ans, dist[i]);       else if (r[i] >= row - 1 or c[i] >= col - 1)         ans = min(ans, dist[i] + 1);     }     if (ans == inf)       cout << -1;     else       cout << ans;   }   