#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
const int INF = 0x3f3f3f3f;
int n, m, tot, s, t, k, d[MAXN * 10], inq[MAXN * 10], inr[MAXN], outr[MAXN],
    inc[MAXN], outc[MAXN];
vector<int> G[MAXN * 10];
queue<int> q;
struct node {
  int id, x, y;
} nodes[MAXN * 10];
struct edge {
  int from, to, cost;
};
vector<edge> edges;
bool cmpx(const node& l, const node& r) {
  return l.x < r.x || (l.x == r.x && l.y < r.y);
}
bool cmpy(const node& l, const node& r) {
  return l.y < r.y || (l.y == r.y && l.x < r.x);
}
void addt(int u, int v, int c) {
  edges.push_back(edge{u, v, c});
  edges.push_back(edge{v, u, c});
  int m = edges.size();
  G[u].push_back(m - 2);
  G[v].push_back(m - 1);
}
void add(int u, int v, int c) {
  edges.push_back(edge{u, v, c});
  G[u].push_back(edges.size() - 1);
}
int SPFA() {
  for (int i = 0; i < tot; ++i) inq[i] = 0, d[i] = INF;
  d[s] = 0, inq[s] = 1;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    inq[u] = 0;
    for (int i = 0; i < G[u].size(); ++i) {
      edge& e = edges[G[u][i]];
      if (d[e.to] > d[u] + e.cost) {
        d[e.to] = d[u] + e.cost;
        if (!inq[e.to]) {
          q.push(e.to);
          inq[e.to] = 1;
        }
      }
    }
  }
  return d[t] == INF ? -1 : d[t];
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  int u, v;
  tot = k;
  bool flag = 0;
  for (int i = 0; i < k; ++i) {
    scanf("%d%d", &u, &v);
    nodes[i] = node{i, u, v};
    if (u == n && v == m) flag = 1, t = i;
    if (u == 1 && v == 1) s = i;
  }
  sort(nodes, nodes + k, cmpx);
  for (int i = 0; i < k - 1; ++i)
    if (nodes[i].x == nodes[i + 1].x && nodes[i].y + 1 == nodes[i + 1].y)
      addt(nodes[i].id, nodes[i + 1].id, 0);
  sort(nodes, nodes + k, cmpy);
  for (int i = 0; i < k - 1; ++i)
    if (nodes[i].y == nodes[i + 1].y && nodes[i].x + 1 == nodes[i + 1].x)
      addt(nodes[i].id, nodes[i + 1].id, 0);
  for (int i = 1; i <= n; ++i) {
    inr[i] = tot++, outr[i] = tot++;
    add(inr[i], outr[i], 1);
  }
  for (int i = 1; i <= m; ++i) {
    inc[i] = tot++, outc[i] = tot++;
    add(inc[i], outc[i], 1);
  }
  for (int i = 0; i < k; ++i) {
    int x = nodes[i].x, y = nodes[i].y, id = nodes[i].id;
    add(id, inr[x], 0);
    if (x > 1) add(id, inr[x - 1], 0);
    if (x < n) add(id, inr[x + 1], 0);
    add(id, inc[y], 0);
    if (y > 1) add(id, inc[y - 1], 0);
    if (y < m) add(id, inc[y + 1], 0);
    add(outr[x], id, 0);
    if (x > 1) add(outr[x - 1], id, 0);
    if (x < n) add(outr[x + 1], id, 0);
    add(outc[y], id, 0);
    if (y > 1) add(outc[y - 1], id, 0);
    if (y < m) add(outc[y + 1], id, 0);
  }
  if (!flag) {
    t = tot++;
    add(outr[n], t, 0);
    add(outc[m], t, 0);
  }
  printf("%d\n", SPFA());
  return 0;
}
