#include <bits/stdc++.h>
using namespace std;
int tot, head[50005], ver[1000005], nxt[1000005], quan[1000005];
void add(int a, int b, int c) {
  tot++;
  nxt[tot] = head[a];
  head[a] = tot;
  ver[tot] = b;
  quan[tot] = c;
  return;
}
queue<int> q;
int dis[50005], in[50005];
void diji(int S) {
  memset(dis, 0x3f, sizeof(dis));
  q.push(S);
  dis[S] = 0;
  while (!q.empty()) {
    int tmp = q.front();
    q.pop();
    in[tmp] = 0;
    for (int i = head[tmp]; i; i = nxt[i]) {
      if (dis[ver[i]] > dis[tmp] + quan[i]) {
        dis[ver[i]] = dis[tmp] + quan[i];
        if (!in[ver[i]]) {
          in[ver[i]] = 1;
          q.push(ver[i]);
        }
      }
    }
  }
  return;
}
int n, m, k;
int k1[] = {0, 0, 1, -1};
int k2[] = {1, -1, 0, 0};
map<pair<int, int>, int> mp;
vector<int> v1[50005], v2[50005];
int xx[50005], yy[50005];
int main() {
  scanf("%d%d%d", &n, &m, &k);
  int t1, t2;
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &t1, &t2);
    mp[make_pair(t1, t2)] = i;
    xx[i] = t1;
    yy[i] = t2;
    v1[t1].push_back(i);
    v2[t2].push_back(i);
    add(i, k + t1, 1);
    add(k + t1, i, 0);
    add(i, k + n + t2, 1);
    add(k + n + t2, i, 0);
    if (t1 != 1) {
      add(k + t1 - 1, i, 0);
      if (t1 != 2) {
        add(k + t1 - 2, i, 0);
      }
    }
    if (t1 != n) {
      add(k + t1 + 1, i, 0);
      if (t1 != n - 1) {
        add(k + t1 + 2, i, 0);
      }
    }
    if (t2 != 1) {
      add(k + n + t2 - 1, i, 0);
      if (t2 != 2) {
        add(k + t2 + n - 2, i, 0);
      }
    }
    if (t2 != m) {
      add(k + n + t2 + 1, i, 0);
      if (t2 != m - 1) {
        add(k + t2 + n + 2, i, 0);
      }
    }
  }
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j <= 3; j++) {
      int x = xx[i] + k1[j], y = yy[i] + k2[j];
      if (mp.find(make_pair(x, y)) != mp.end()) {
        int tmp = mp[make_pair(x, y)];
        add(i, tmp, 0);
      }
    }
  }
  diji(mp[make_pair(1, 1)]);
  if (mp.find(make_pair(n, m)) != mp.end()) {
    if (dis[mp[make_pair(n, m)]] == 0x3f3f3f3f)
      puts("-1");
    else
      printf("%d\n", dis[mp[make_pair(n, m)]]);
  } else {
    int ans = 0x3f3f3f3f;
    for (int i = 1; i <= k; i++) {
      if (xx[i] == n || yy[i] == m || xx[i] == n - 1 || yy[i] == m - 1) {
        ans = min(ans, dis[i] + 1);
      }
    }
    if (ans == 0x3f3f3f3f)
      puts("-1");
    else
      printf("%d\n", ans);
  }
  return 0;
}
