#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > comprows(((int)(1e4 + 1)),
                                 pair<int, int>(2000000000, -2000000000)),
    compcols(((int)(1e4 + 1)), pair<int, int>(2000000000, -2000000000));
map<int, int> lit, comp;
int rs[] = {0, 0, -1, 1};
int cs[] = {-1, 1, 0, 0};
int n, m, k;
int main() {
  int i, j, r, c;
  cin >> n >> m >> k;
  for (i = 0; i < k; i++) {
    cin >> r >> c;
    r--, c--;
    lit[((r)*m + c)] = 1;
  }
  int ci = 0;
  for (r = 0; r < n; r++) {
    for (c = 0; c < m; c++) {
      if (comp.count(((r)*m + c)) || !lit.count(((r)*m + c))) continue;
      queue<int> q;
      q.push(((r)*m + c));
      comp[((r)*m + c)] = ++ci;
      while (!q.empty()) {
        int t = q.front();
        q.pop();
        comprows[ci].first = min(comprows[ci].first, ((t) / m));
        comprows[ci].second = max(comprows[ci].second, ((t) / m));
        compcols[ci].first = min(compcols[ci].first, ((t) % m));
        compcols[ci].second = max(compcols[ci].second, ((t) % m));
        for (i = 0; i < 4; i++) {
          int nr = ((t) / m) + rs[i], nc = ((t) % m) + cs[i];
          if ((nr >= 0 && nr < n && nc >= 0 && nc < m) &&
              !comp.count(((nr)*m + nc)) && lit.count(((nr)*m + nc))) {
            comp[((nr)*m + nc)] = ci;
            q.push(((nr)*m + nc));
          }
        }
      }
    }
  }
  vector<vector<int> > g(((int)(1e4 + 1)));
  for (i = 1; i <= ci; i++) {
    for (j = i + 1; j <= ci; j++) {
      if (((compcols[i].first - 2 <= compcols[j].first &&
            compcols[j].first <= compcols[i].second + 2) ||
           (compcols[i].first - 2 <= compcols[j].second &&
            compcols[j].second <= compcols[i].second + 2) ||
           (compcols[j].first - 2 <= compcols[i].first &&
            compcols[i].first <= compcols[j].second + 2)) ||
          ((comprows[i].first - 2 <= comprows[j].first &&
            comprows[j].first <= comprows[i].second + 2) ||
           (comprows[i].first - 2 <= comprows[j].second &&
            comprows[j].second <= comprows[i].second + 2) ||
           (comprows[j].first - 2 <= comprows[i].first &&
            comprows[i].first <= comprows[j].second + 2)))
        g[i].push_back(j), g[j].push_back(i);
    }
  }
  int seen[((int)(1e4 + 1))] = {}, dist[((int)(1e4 + 1))];
  queue<int> q;
  q.push(comp[0]);
  seen[comp[0]] = 1;
  dist[comp[0]] = 0;
  int endComp =
      (comp.count(((n - 1) * m + m - 1))) ? comp[((n - 1) * m + m - 1)] : -1;
  int ex = 2 + comp.count(((n - 1) * m + m - 1)), res = 2000000000;
  while (!q.empty()) {
    int com = q.front();
    q.pop();
    if (com == endComp) res = min(res, dist[com]);
    if (comprows[com].second >= n - ex || compcols[com].second >= m - ex)
      res = min(res, dist[com] + 1);
    for (j = 0; j < g[com].size(); j++) {
      i = g[com][j];
      if (seen[i]) continue;
      seen[i] = 1;
      dist[i] = dist[com] + 1;
      q.push(i);
    }
  }
  cout << (res == 2000000000 ? -1 : res) << '\n';
}
