#include <bits/stdc++.h>
using namespace std;
const int maxn = 1E4 + 77;
const int S = maxn - 1;
const int T = maxn - 2;
const int dirx[4] = {-1, 1, 0, 0}, diry[4] = {0, 0, -1, 1};
struct Link {
  int v, next;
} links[maxn * 4];
int rows[maxn], cols[maxn], cnt = 1;
void addLink(int &k, int v) {
  links[cnt] = (Link){v, k};
  k = cnt++;
}
int N, M, K, adj[maxn][4], X[maxn], Y[maxn];
map<int, map<int, int> > Map;
bool legalPos(int x, int y) { return x > 0 && x <= N && y > 0 && y <= M; }
int mindis[maxn], Q[maxn], qhead, qrear;
bool inQ[maxn];
void spfa_solve(int u, int v, int w) {
  if (mindis[v] == -1 || mindis[v] > mindis[u] + w) {
    mindis[v] = mindis[u] + w;
    if (inQ[v]) return;
    if (mindis[v] < mindis[qhead]) {
      qhead = qhead ? qhead - 1 : maxn - 1;
      Q[qhead] = v;
    } else {
      Q[qrear++] = v;
      qrear %= maxn;
    }
    inQ[v] = true;
  }
}
void spfa(int s) {
  memset(mindis, -1, sizeof(mindis));
  mindis[s] = 0;
  qhead = qrear = 0;
  Q[qrear++] = s;
  inQ[s] = true;
  while (qhead != qrear) {
    int u = Q[qhead++];
    inQ[u] = false;
    qhead %= maxn;
    for (int d = 0; d < 4; ++d)
      if (adj[u][d]) spfa_solve(u, adj[u][d], 0);
    int &x = X[u], &y = Y[u];
    if (x == N && y == M) {
      spfa_solve(u, T, 0);
    } else if (x == N || y == M || x == N - 1 || y == M - 1) {
      spfa_solve(u, T, 1);
    }
    for (int i = -2; i <= 2; ++i)
      if (0 < x + i && x + i <= N) {
        for (int k = rows[x + i]; k; k = links[k].next) {
          spfa_solve(u, links[k].v, 1);
        }
      }
    for (int i = -2; i <= 2; ++i)
      if (0 < y + i && y + i <= M) {
        for (int k = cols[y + i]; k; k = links[k].next) {
          spfa_solve(u, links[k].v, 1);
        }
      }
  }
}
int main() {
  scanf("%d%d%d", &N, &M, &K);
  for (int i = 1; i <= K; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    X[i] = x;
    Y[i] = y;
    Map[x][y] = i;
    addLink(rows[x], i);
    addLink(cols[y], i);
  }
  for (int i = 1; i <= K; ++i) {
    int x = X[i], y = Y[i];
    for (int d = 0; d < 4; ++d) {
      int nx = x + dirx[d], ny = y + diry[d];
      if (legalPos(nx, ny) && Map.count(nx) && Map[nx].count(ny)) {
        adj[i][d] = Map[nx][ny];
      }
    }
  }
  spfa(1);
  printf("%d\n", mindis[T]);
}
