#include <bits/stdc++.h>
using namespace std;
map<int, int> vis[10004];
vector<int> r[10004], c[10004];
int main() {
  ios_base::sync_with_stdio(0);
  int i, j, n, m, k, x, y, ans = 1000000007;
  cin >> n >> m >> k;
  for (int(i) = (1); (i) <= (k); ++i) {
    cin >> x >> y;
    r[x].push_back(y);
    c[y].push_back(x);
    vis[x][y] = -1;
  }
  vis[n][m] = -1;
  set<pair<int, pair<int, int> > > s;
  s.insert(make_pair(0, make_pair(1, 1)));
  vis[1][1] = 0;
  while (!s.empty()) {
    int ta = s.begin()->second.first, tb = s.begin()->second.second,
        tp = s.begin()->first;
    s.erase(s.begin());
    for (int(j) = (-2); (j) <= (2); ++j) {
      if (ta + j > 0 && ta + j <= n && (!r[ta + j].empty())) {
        x = ta + j;
        for (int(i) = (0); (i) <= (r[x].size() - 1); ++i) {
          if (j == 0 && abs(r[x][i] - tb) <= 1) {
            if (vis[x][r[x][i]] == -1) {
              s.insert(make_pair(tp, make_pair(x, r[x][i])));
              vis[x][r[x][i]] = tp;
            } else if (vis[x][r[x][i]] > tp) {
              s.erase(make_pair(vis[x][r[x][i]], make_pair(x, r[x][i])));
              s.insert(make_pair(tp, make_pair(x, r[x][i])));
              vis[x][r[x][i]] = tp;
            }
            if (x == n && r[x][i] == m) ans = min(ans, tp);
          } else {
            if (x == n && r[x][i] == m) ans = min(ans, tp + 1);
            if (vis[x][r[x][i]] == -1) {
              vis[x][r[x][i]] = tp + 1;
              s.insert(make_pair(tp + 1, make_pair(x, r[x][i])));
            }
          }
        }
      }
      if (ta + j == n && j < 2) {
        ans = min(ans, tp + 1);
      }
      if (tb + j == m && j < 2) ans = min(ans, tp + 1);
      if (tb + j > 0 && tb + j <= m && (!c[tb + j].empty())) {
        x = tb + j;
        for (int(i) = (0); (i) <= (c[x].size() - 1); ++i) {
          if (j == 0 && abs(c[x][i] - ta) <= 1) {
            if (vis[c[x][i]][x] == -1) {
              s.insert(make_pair(tp, make_pair(c[x][i], x)));
              vis[c[x][i]][x] = tp;
            } else if (vis[c[x][i]][x] > tp) {
              s.erase(make_pair(vis[c[x][i]][x], make_pair(c[x][i], x)));
              s.insert(make_pair(tp, make_pair(c[x][i], x)));
              vis[c[x][i]][x] = tp;
            }
            if (x == m && c[x][i] == n) ans = min(ans, tp);
          } else {
            if (x == m && c[x][i] == n) ans = min(ans, tp + 1);
            if (vis[c[x][i]][x] == -1) {
              vis[c[x][i]][x] = tp + 1;
              s.insert(make_pair(tp + 1, make_pair(c[x][i], x)));
            }
          }
        }
      }
    }
  }
  if (ans == 1000000007) ans = -1;
  cout << ans;
  return 0;
}
