#include <bits/stdc++.h>
using namespace std;
const int N = 1e4;
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    pq;
int dist[N + 100];
const int inf = 1e6;
map<pair<int, int>, int> M;
pair<int, int> arr[N + 100];
vector<int> col[N + 100], row[N + 100];
int dr[] = {0, 0, 1, -1};
int dc[] = {1, -1, 0, 0};
void fillrow(int r, int val) {
  for (int c : row[r]) {
    int tmp = M[make_pair(r, c)];
    if (dist[tmp] > val) {
      dist[tmp] = val;
      pq.push(make_pair(val, tmp));
    }
  }
}
void fillcol(int c, int val) {
  for (int r : col[c]) {
    int tmp = M[make_pair(r, c)];
    if (dist[tmp] > val) {
      dist[tmp] = val;
      pq.push(make_pair(val, tmp));
    }
  }
}
int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    int r, c;
    scanf("%d%d", &r, &c);
    col[c].push_back(r);
    row[r].push_back(c);
    arr[i] = make_pair(r, c);
    M[make_pair(r, c)] = i;
  }
  for (int i = 0; i <= N; i++) dist[i] = inf;
  if (M.find(make_pair(n, m)) == M.end()) {
    fillrow(n, 1);
    fillrow(n - 1, 1);
    fillcol(m, 1);
    fillcol(m - 1, 1);
  } else {
    dist[M[make_pair(n, m)]] = 0;
    pq.push(make_pair(0, M[make_pair(n, m)]));
  }
  while (!pq.empty()) {
    pair<int, int> u = pq.top();
    pq.pop();
    if (dist[u.second] != u.first) continue;
    for (int i = 0; i < 4; i++) {
      int x = arr[u.second].first + dr[i], y = arr[u.second].second + dc[i];
      if (M.find(make_pair(x, y)) == M.end()) continue;
      int now = M[make_pair(x, y)];
      if (M.find(make_pair(x, y)) != M.end() && dist[now] > u.first) {
        dist[now] = u.first;
        pq.push(make_pair(u.first, now));
      }
    }
    for (int i = arr[u.second].first - 2; i <= arr[u.second].first + 2; i++) {
      if (i < 1 || i > n) continue;
      fillrow(i, u.first + 1);
    }
    for (int i = arr[u.second].second - 2; i <= arr[u.second].second + 2; i++) {
      if (i < 1 || i > m) continue;
      fillcol(i, u.first + 1);
    }
  }
  int lst = M[make_pair(1, 1)];
  if (dist[lst] >= inf) {
    printf("-1\n");
  } else {
    printf("%d\n", dist[lst]);
  }
}
