import java.io.*;
import java.util.*;

public class CF821D {
	static final int INF = Integer.MAX_VALUE;
	static class V implements Comparable<V> {
		int r, c, i, d = INF;
		V(int r, int c, int i) {
			this.r = r; this.c = c; this.i = i;
		}
		@Override public int compareTo(V v) {
			return d != v.d ? d - v.d : i - v.i;
		}
	}
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		V[] vv = new V[k];
		ArrayList<V>[] rr = new ArrayList[n];	// unchecked
		ArrayList<V>[] cc = new ArrayList[m];	// unchecked
		for (int r = 0; r < n; r++)
			rr[r] = new ArrayList<>();
		for (int c = 0; c < m; c++)
			cc[c] = new ArrayList<>();
		for (int i = 0; i < k; i++) {
			st = new StringTokenizer(br.readLine());
			int r = Integer.parseInt(st.nextToken()) - 1;
			int c = Integer.parseInt(st.nextToken()) - 1;
			V v = new V(r, c, i);
			vv[i] = v;
			rr[r].add(v);
			cc[c].add(v);
		}
		TreeSet<V> queue = new TreeSet<>();
		for (V u : vv)
			if (u.r == 0 && u.c == 0) {
				u.d = 0;
				queue.add(u);
				break;
			}
		int min = INF;
		while (!queue.isEmpty()) {
			V u = queue.first();
			queue.remove(u);
			int r_ = Math.abs(n - 1 - u.r);
			int c_ = Math.abs(m - 1 - u.c);
			if (r_ == 0 && c_ == 0)
				min = Math.min(min, u.d);
			else if (r_ <= 1 || c_ <= 1)
				min = Math.min(min, u.d + 1);
			for (int r = u.r - 2; r <= u.r + 2; r++)
				if (r >= 0 && r < n)
					for (V v : rr[r]) {
						int d = u.d + (Math.abs(u.r - v.r) + Math.abs(u.c - v.c) == 1 ? 0 : 1);
						if (d < v.d) {
							queue.remove(v);
							v.d = d;
							queue.add(v);
						}
					}
			for (int c = u.c - 2; c <= u.c + 2; c++)
				if (c >= 0 && c < m)
					for (V v : cc[c]) {
						int d = u.d + (Math.abs(u.r - v.r) + Math.abs(u.c - v.c) == 1 ? 0 : 1);
						if (d < v.d) {
							queue.remove(v);
							v.d = d;
							queue.add(v);
						}
					}
		}
		System.out.println(min == INF ? -1 : min);
	}
}


import java.io.*;
import java.util.*;

public class CF821D {
	static final int INF = Integer.MAX_VALUE;
	static class V implements Comparable<V> {
		HashSet<V> set = new HashSet<>();
		int r, c, i, d = INF;
		V(int r, int c, int i) {
			this.r = r; this.c = c; this.i = i;
		}
		@Override public int compareTo(V v) {
			return d != v.d ? d - v.d : i - v.i;
		}
	}
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		V[] vv = new V[k];
		ArrayList<V>[] rr = new ArrayList[n];	// unchecked
		ArrayList<V>[] cc = new ArrayList[m];	// unchecked
		for (int r = 0; r < n; r++)
			rr[r] = new ArrayList<>();
		for (int c = 0; c < m; c++)
			cc[c] = new ArrayList<>();
		for (int i = 0; i < k; i++) {
			st = new StringTokenizer(br.readLine());
			int r = Integer.parseInt(st.nextToken()) - 1;
			int c = Integer.parseInt(st.nextToken()) - 1;
			V v = new V(r, c, i);
			vv[i] = v;
			rr[r].add(v);
			cc[c].add(v);
		}
		for (int i = 0; i < k; i++) {
			V u = vv[i];
			for (int r = u.r - 2; r <= u.r + 2; r++)
				if (r >= 0 && r < n)
					for (V v : rr[r])
						u.set.add(v);
			for (int c = u.c - 2; c <= u.c + 2; c++)
				if (c >= 0 && c < m)
					for (V v : cc[c])
						u.set.add(v);
		}
		TreeSet<V> queue = new TreeSet<>();
		for (V u : vv)
			if (u.r == 0 && u.c == 0) {
				u.d = 0;
				queue.add(u);
				break;
			}
		int min = INF;
		while (!queue.isEmpty()) {
			V u = queue.first();
			queue.remove(u);
			int r_ = Math.abs(n - 1 - u.r);
			int c_ = Math.abs(m - 1 - u.c);
			if (r_ == 0 && c_ == 0)
				min = Math.min(min, u.d);
			else if (r_ <= 1 || c_ <= 1)
				min = Math.min(min, u.d + 1);
			for (V v : u.set) {
				int d = u.d + (Math.abs(u.r - v.r) + Math.abs(u.c - v.c) == 1 ? 0 : 1);
				if (d < v.d) {
					queue.remove(v);
					v.d = d;
					queue.add(v);
				}
			}
		}
		System.out.println(min == INF ? -1 : min);
	}
}


import java.util.*;

public class D {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int k=  qwe.nextInt();
		k++;
		int[] r = new int[k];
		int[] c = new int[k];

		ArrayList<Integer>[] byr = new ArrayList[n+1];
		for (int i = 0; i < byr.length; i++) {
			byr[i] = new ArrayList<Integer>();
		}
		ArrayList<Integer>[] byc = new ArrayList[m+1];
		for (int i = 0; i < byc.length; i++) {
			byc[i] = new ArrayList<Integer>();
		}

		int idf = 0;
		int idl = -1;
		for (int i = 0; i < c.length; i++) {
			if(i == c.length-1){
				r[i] = n;
				c[i] = m;
			}
			else{
				r[i] = qwe.nextInt();
				c[i] = qwe.nextInt();
				if(r[i] == 1 && c[i] == 1) idf = i;
				if(r[i] == n && c[i] == m) idl = i;
			}

			byr[r[i]].add(i);
			byc[c[i]].add(i);
		}

		int[][] been = new int[k][7];
		for (int i = 0; i < been.length; i++) {
			Arrays.fill(been[i], -1);
		}

		ArrayDeque<state> dq = new ArrayDeque<state>();
		dq.add(new state(idf,0,0));

		while(!dq.isEmpty()){
			state at = dq.pollFirst();

			int dex = at.kdex;

			if(been[dex][at.lit] != -1) continue;
			been[dex][at.lit] = at.dist;

			//System.out.println("at: r" + r[dex] + " c " + c[dex] + " cost: " + been[dex][at.lit] + " lit: " + at.lit);

			for(int rgo = -1; rgo <= 1; rgo++){
				int row = r[dex] + rgo;
				if(row >= 0 && row <= n){
					for(int kto : byr[row]){
						if((c[kto] == c[dex] || rgo == 0 && Math.abs(c[kto]-c[dex]) == 1 || at.lit == 2) && kto != k-1){
							int newlit = getnewlit(r[dex],c[dex],at.lit,r[kto],c[kto]);
							if(been[kto][newlit] == -1)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else{
							if(been[kto][2] == -1){
								int ecost = 1;
								if(rgo + 2 == at.lit) ecost = 0;
								dq.addLast(new state(kto,2,at.dist+ecost));
							}
						}
					}
				}
			}

			for(int cgo = -1; cgo <= 1; cgo++){
				int col = c[dex]+cgo;
				if(col >= 0 && col <= m){
					for(int kto: byc[col]){
						if((r[kto] == r[dex] || cgo == 0 && Math.abs(r[kto]-r[dex]) == 1 || at.lit == 5) && kto != k-1){
							int newlit = getnewlit(r[dex],c[dex],at.lit,r[kto],c[kto]);
							if(been[kto][newlit] == -1)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else{
							if(been[kto][5] == -1){
								int ecost = 1;
								if(cgo + 5 == at.lit) ecost = 0;
								dq.addLast(new state(kto,5,at.dist+ecost));
							}
						}
					}
				}
			}

//			if(at.lit == 0){
//
//				for(int nlit = 1; nlit <= 3; nlit++){
//
//				}
//				//dq.addLast(new );
//
//			}

		}

		int ans = -1;
		int dexl = idl == -1 ? k-1 : idl;
		for (int i = 0; i < 7; i++) {
			if(been[dexl][i] != -1){
				if(ans == -1) ans = been[dexl][i];
				else ans = Math.min(ans, been[dexl][i]);
			}
		}

		System.out.println(ans);

		qwe.close();
	}

	static int getnewlit(int r, int c, int lit, int nr, int nc){
		if(lit == 0) return 0;

		if(lit < 4){
			int dr = r-nr;
			int ret = lit+dr;
			if(ret < 1 || ret > 4) ret = 0;
			return ret;
		}
		else{
			int dc = c-nc;
			int ret = lit+dc;
			if(ret < 4 || ret > 6) ret= 0;
			return ret;
		}

	}

	static class state{
		int kdex;
		int lit;
		int dist;

		public state(int kdex, int lit, int dist) {
			this.kdex = kdex;
			this.lit = lit;
			this.dist = dist;
		}

	}

}


import java.util.*;

public class D420 {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int k=  qwe.nextInt();
		k+=2;
		int[] r = new int[k];
		int[] c = new int[k];

		ArrayList<Integer>[] byr = new ArrayList[n+2];
		for (int i = 0; i < byr.length; i++) {
			byr[i] = new ArrayList<Integer>();
		}
		ArrayList<Integer>[] byc = new ArrayList[m+2];
		for (int i = 0; i < byc.length; i++) {
			byc[i] = new ArrayList<Integer>();
		}

		int idf = 0;
		int idl = -1;
		for (int i = 0; i < c.length; i++) {
			if(i == c.length-1){
				r[i] = n+1;
				c[i] = m;
			}
			else if(i == c.length-2){
				r[i] = n;
				c[i] = m+1;
			}
			else{
				r[i] = qwe.nextInt();
				c[i] = qwe.nextInt();
				if(r[i] == 1 && c[i] == 1) idf = i;
				if(r[i] == n && c[i] == m) idl = i;
			}

			byr[r[i]].add(i);
			byc[c[i]].add(i);
		}

		int[][] been = new int[k][3];
		boolean[][] seen = new boolean[k][3];
		int oo = 987654321;
		for (int i = 0; i < been.length; i++) {
			Arrays.fill(been[i], oo);
		}

		ArrayDeque<state> dq = new ArrayDeque<state>();
		dq.add(new state(idf,0,0));

		while(!dq.isEmpty()){
			state at = dq.pollFirst();

			int dex = at.kdex;
			if(been[dex][at.lit] < at.dist || seen[dex][at.lit]) continue;

			//System.out.println(dq + ": before");
			//System.out.println("at: (" + r[dex] + ", " + c[dex] + ") " + at.dist + " kdex: " + dex);

			seen[dex][at.lit] = true;

			//System.out.println("at: r" + r[dex] + " c " + c[dex] + " cost: " + been[dex][at.lit] + " lit: " + at.lit);

			for(int rgo = -1; rgo <= 1; rgo++){
				int row = r[dex] + rgo;
				if(row >= 0 && row < byr.length){
					for(int kto : byr[row]){
						if(at.lit == 1){
							int newlit = rgo == 0 ? 1 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(c[kto] == c[dex] || rgo == 0 && Math.abs(c[kto]-c[dex]) == 1){
							int newlit=0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}

						}
						else{
							int newlit = 1;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
							}

						}
					}
				}
			}

			for(int cgo = -1; cgo <= 1; cgo++){
				int col = c[dex]+cgo;
				if(col >= 0 && col <= m){
					for(int kto: byc[col]){
						if(at.lit == 2){
							int newlit = cgo == 0 ? 2 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(r[kto] == r[dex] || cgo == 0 && Math.abs(r[kto]-r[dex]) == 1){
							int newlit = 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else{
							int newlit = 2;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
							}
						}
					}
				}
			}

			//System.out.println(dq);
			//System.out.println();

		}

		int ans = -1;
		for (int i = 2; i <= 2; i++) {
			if(been[k-1][i] != oo){
				if(ans == -1) ans = been[k-1][i];
				else ans = Math.min(ans, been[k-1][i]);
			}
		}

		int ans2 = -1;
		for (int i = 1; i <= 1; i++) {
			if(been[k-2][i] != oo){
				if(ans2 == -1) ans2 = been[k-2][i];
				else ans2 = Math.min(ans2, been[k-2][i]);
			}
		}

		int min = Math.min(ans, ans2);

		if(idl != -1) min = Math.min(min, been[idl][0]);

		System.out.println(min);

		qwe.close();
	}

//	static int getnewlit(int r, int c, int lit, int nr, int nc){
//		if(lit == 0) return 0;
//
//		if(lit < 4){
//			int dr = r-nr;
//			int ret = lit+dr;
//			if(ret < 1 || ret > 4) ret = 0;
//			return ret;
//		}
//		else{
//			int dc = c-nc;
//			int ret = lit+dc;
//			if(ret < 4 || ret > 6) ret= 0;
//			return ret;
//		}
//
//	}

	static class state{
		int kdex;
		int lit;
		int dist;

		public state(int kdex, int lit, int dist) {
			this.kdex = kdex;
			this.lit = lit;
			this.dist = dist;
		}

		@Override
		public String toString() {
			return "state [kdex=" + kdex + ", lit=" + lit + ", dist=" + dist + "]";
		}

	}

}


import java.util.*;
/*

12 12 3
1 1
10 3
8 11

 */
public class D420 {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int k=  qwe.nextInt();
		k+=2;
		int[] r = new int[k];
		int[] c = new int[k];

		ArrayList<Integer>[] byr = new ArrayList[n+2];
		for (int i = 0; i < byr.length; i++) {
			byr[i] = new ArrayList<Integer>();
		}
		ArrayList<Integer>[] byc = new ArrayList[m+2];
		for (int i = 0; i < byc.length; i++) {
			byc[i] = new ArrayList<Integer>();
		}

		int idf = 0;
		int idl = -1;
		for (int i = 0; i < c.length; i++) {
			if(i == c.length-1){
				r[i] = n+1;
				c[i] = m;
			}
			else if(i == c.length-2){
				r[i] = n;
				c[i] = m+1;
			}
			else{
				r[i] = qwe.nextInt();
				c[i] = qwe.nextInt();
				if(r[i] == 1 && c[i] == 1) idf = i;
				if(r[i] == n && c[i] == m) idl = i;
			}

			byr[r[i]].add(i);
			byc[c[i]].add(i);
		}

		int[][] been = new int[k][3];
		boolean[][] seen = new boolean[k][3];
		int oo = 987654321;
		for (int i = 0; i < been.length; i++) {
			Arrays.fill(been[i], oo);
		}

		ArrayDeque<state> dq = new ArrayDeque<state>();
		dq.add(new state(idf,0,0));

		while(!dq.isEmpty()){
			state at = dq.pollFirst();

			int dex = at.kdex;
			if(been[dex][at.lit] < at.dist || seen[dex][at.lit]) continue;
			seen[dex][at.lit] = true;
			if(dex == k-2 && at.lit != 1) continue;
			if(dex == k-1 && at.lit != 2) continue;
			//System.out.println(dq + ": before");
			//System.out.println("at: (" + r[dex] + ", " + c[dex] + ") " + at.dist + " kdex: " + dex);

			//System.out.println("at: r" + r[dex] + " c " + c[dex] + " cost: " + been[dex][at.lit] + " lit: " + at.lit);

			for(int rgo = -2; rgo <= 2; rgo++){
				int row = r[dex] + rgo;
				if(row >= 0 && row < byr.length){
					for(int kto : byr[row]){
						if(Math.abs(rgo) == 2){
							int newlit = 0;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}
						}
						else if(at.lit == 1){
							int newlit = rgo == 0 ? 1 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(c[kto] == c[dex] || rgo == 0 && Math.abs(c[kto]-c[dex]) == 1){
							int newlit=0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}

						}
						else{
							int newlit = 1;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}

						}
					}
				}
			}

			for(int cgo = -2; cgo <= 2; cgo++){
				int col = c[dex]+cgo;
				if(col >= 0 && col < byc.length){
					for(int kto: byc[col]){
						if(Math.abs(cgo) == 2){
							int newlit = 0;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}
						}
						else if(at.lit == 2){
							int newlit = cgo == 0 ? 2 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(r[kto] == r[dex] || cgo == 0 && Math.abs(r[kto]-r[dex]) == 1){
							int newlit = 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else{
							int newlit = 2;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}
						}
					}
				}
			}

			//System.out.println(dq);
			//System.out.println();

		}

		int ans = -1;
		for (int i = 2; i <= 2; i++) {
			if(been[k-1][i] != oo){
				if(ans == -1) ans = been[k-1][i];
				else ans = Math.min(ans, been[k-1][i]);
			}
		}

		int ans2 = -1;
		for (int i = 1; i <= 1; i++) {
			if(been[k-2][i] != oo){
				if(ans2 == -1) ans2 = been[k-2][i];
				else ans2 = Math.min(ans2, been[k-2][i]);
			}
		}

		int min = Math.min(ans, ans2);

		if(idl != -1){
			min = Math.min(min, been[idl][0]);
		}

		System.out.println(min);

		qwe.close();
	}

	static class state{
		int kdex;
		int lit;
		int dist;

		public state(int kdex, int lit, int dist) {
			this.kdex = kdex;
			this.lit = lit;
			this.dist = dist;
		}

		@Override
		public String toString() {
			return "state [kdex=" + kdex + ", lit=" + lit + ", dist=" + dist + "]";
		}

	}

}


import java.util.*;
/*

12 12 3
1 1
10 3
8 11

 */
public class D420 {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int k=  qwe.nextInt();
		k+=2;
		int[] r = new int[k];
		int[] c = new int[k];

		ArrayList<Integer>[] byr = new ArrayList[n+2];
		for (int i = 0; i < byr.length; i++) {
			byr[i] = new ArrayList<Integer>();
		}
		ArrayList<Integer>[] byc = new ArrayList[m+2];
		for (int i = 0; i < byc.length; i++) {
			byc[i] = new ArrayList<Integer>();
		}

		int idf = 0;
		int idl = -1;
		for (int i = 0; i < c.length; i++) {
			if(i == c.length-1){
				r[i] = n+1;
				c[i] = m;
			}
			else if(i == c.length-2){
				r[i] = n;
				c[i] = m+1;
			}
			else{
				r[i] = qwe.nextInt();
				c[i] = qwe.nextInt();
				if(r[i] == 1 && c[i] == 1) idf = i;
				if(r[i] == n && c[i] == m) idl = i;
			}

			byr[r[i]].add(i);
			byc[c[i]].add(i);
		}

		int[][] been = new int[k][3];
		boolean[][] seen = new boolean[k][3];
		int oo = 987654321;
		for (int i = 0; i < been.length; i++) {
			Arrays.fill(been[i], oo);
		}

		ArrayDeque<state> dq = new ArrayDeque<state>();
		dq.add(new state(idf,0,0));

		while(!dq.isEmpty()){
			state at = dq.pollFirst();

			int dex = at.kdex;
			if(been[dex][at.lit] < at.dist || seen[dex][at.lit]) continue;
			seen[dex][at.lit] = true;
			if(dex == k-2 && at.lit != 1) continue;
			if(dex == k-1 && at.lit != 2) continue;
			//System.out.println(dq + ": before");
			//System.out.println("at: (" + r[dex] + ", " + c[dex] + ") " + at.dist + " kdex: " + dex);

			//System.out.println("at: r" + r[dex] + " c " + c[dex] + " cost: " + been[dex][at.lit] + " lit: " + at.lit);

			for(int rgo = -2; rgo <= 2; rgo++){
				int row = r[dex] + rgo;
				if(row >= 0 && row < byr.length){
					for(int kto : byr[row]){
						if(Math.abs(rgo) == 2){
							int newlit = 0;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}
						}
						else if(at.lit == 1){
							int newlit = rgo == 0 ? 1 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(c[kto] == c[dex] || rgo == 0 && Math.abs(c[kto]-c[dex]) == 1){
							int newlit=0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}

						}
						else{
							int newlit = 1;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}

						}
					}
				}
			}

			for(int cgo = -2; cgo <= 2; cgo++){
				int col = c[dex]+cgo;
				if(col >= 0 && col < byc.length){
					for(int kto: byc[col]){
						if(Math.abs(cgo) == 2){
							int newlit = 0;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}
						}
						else if(at.lit == 2){
							int newlit = cgo == 0 ? 2 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(r[kto] == r[dex] || cgo == 0 && Math.abs(r[kto]-r[dex]) == 1){
							int newlit = 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else{
							int newlit = 2;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}
						}
					}
				}
			}

			//System.out.println(dq);
			//System.out.println();

		}

		int ans = oo;
		ans = Math.min(ans, been[k-1][2]);

		int ans2 = oo;

		ans2 = Math.min(ans2, been[k-2][1]);

		int min = Math.min(ans, ans2);

		if(idl != -1){
			min = Math.min(min, been[idl][0]);
		}

		if(min == oo) min = -1;

		System.out.println(min);

		qwe.close();
	}

	static class state{
		int kdex;
		int lit;
		int dist;

		public state(int kdex, int lit, int dist) {
			this.kdex = kdex;
			this.lit = lit;
			this.dist = dist;
		}

		@Override
		public String toString() {
			return "state [kdex=" + kdex + ", lit=" + lit + ", dist=" + dist + "]";
		}

	}

}


import java.util.*;

public class D {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int k=  qwe.nextInt();
		k++;
		int[] r = new int[k];
		int[] c = new int[k];

		ArrayList<Integer>[] byr = new ArrayList[n+1];
		for (int i = 0; i < byr.length; i++) {
			byr[i] = new ArrayList<Integer>();
		}
		ArrayList<Integer>[] byc = new ArrayList[m+1];
		for (int i = 0; i < byc.length; i++) {
			byc[i] = new ArrayList<Integer>();
		}

		int idf = 0;
		int idl = -1;
		for (int i = 0; i < c.length; i++) {
			if(i == c.length-1){
				r[i] = n;
				c[i] = m;
			}
			else{
				r[i] = qwe.nextInt();
				c[i] = qwe.nextInt();
				if(r[i] == 1 && c[i] == 1) idf = i;
				if(r[i] == n && c[i] == m) idl = i;
			}

			byr[r[i]].add(i);
			byc[c[i]].add(i);
		}

		int[][] been = new int[k][7];
		boolean[][] seen = new boolean[k][7];
		int oo = 987654321;
		for (int i = 0; i < been.length; i++) {
			Arrays.fill(been[i], oo);
		}

		ArrayDeque<state> dq = new ArrayDeque<state>();
		dq.add(new state(idf,0,0));

		while(!dq.isEmpty()){
			state at = dq.pollFirst();

			int dex = at.kdex;

			if(been[dex][at.lit] < at.dist || seen[dex][at.lit]) continue;
			seen[dex][at.lit] = true;

			//System.out.println("at: r" + r[dex] + " c " + c[dex] + " cost: " + been[dex][at.lit] + " lit: " + at.lit);

			for(int rgo = -1; rgo <= 1; rgo++){
				int row = r[dex] + rgo;
				if(row >= 0 && row <= n){
					for(int kto : byr[row]){
						if((c[kto] == c[dex] || rgo == 0 && Math.abs(c[kto]-c[dex]) == 1 || at.lit == 2) && kto != k-1){
							int newlit = getnewlit(r[dex],c[dex],at.lit,r[kto],c[kto]);
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else{

								int ecost = 1;
								if(rgo + 2 == at.lit) ecost = 0;
								if(been[kto][2] > at.dist+ecost){
									if(ecost == 0)
										dq.addFirst(new state(kto,2,at.dist+ecost));
									else
										dq.addLast(new state(kto,2,at.dist+ecost));
									been[kto][2] = at.dist+ecost;
								}
						}
					}
				}
			}

			for(int cgo = -1; cgo <= 1; cgo++){
				int col = c[dex]+cgo;
				if(col >= 0 && col <= m){
					for(int kto: byc[col]){
						if((r[kto] == r[dex] || cgo == 0 && Math.abs(r[kto]-r[dex]) == 1 || at.lit == 5) && kto != k-1){
							int newlit = getnewlit(r[dex],c[dex],at.lit,r[kto],c[kto]);
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								//been[kto][newlit] = at.dist;
							}
						}
						else{
								int ecost = 1;
								if(cgo + 5 == at.lit) ecost = 0;
								if(been[kto][5] > at.dist+ecost){
									if(ecost == 0)
										dq.addFirst(new state(kto,5,at.dist+ecost));
									else dq.addLast(new state(kto,5,at.dist+ecost));

									been[kto][5] = at.dist+ecost;
								}
						}
					}
				}
			}

//			if(at.lit == 0){
//
//				for(int nlit = 1; nlit <= 3; nlit++){
//
//				}
//				//dq.addLast(new );
//
//			}

		}

		int ans = -1;
		int dexl = idl == -1 ? k-1 : idl;
		for (int i = 0; i < 7; i++) {
			if(been[dexl][i] != oo){
				if(ans == -1) ans = been[dexl][i];
				else ans = Math.min(ans, been[dexl][i]);
			}
		}

		System.out.println(ans);

		qwe.close();
	}

	static int getnewlit(int r, int c, int lit, int nr, int nc){
		if(lit == 0) return 0;

		if(lit < 4){
			int dr = r-nr;
			int ret = lit+dr;
			if(ret < 1 || ret > 4) ret = 0;
			return ret;
		}
		else{
			int dc = c-nc;
			int ret = lit+dc;
			if(ret < 4 || ret > 6) ret= 0;
			return ret;
		}

	}

	static class state{
		int kdex;
		int lit;
		int dist;

		public state(int kdex, int lit, int dist) {
			this.kdex = kdex;
			this.lit = lit;
			this.dist = dist;
		}

	}

}


import java.util.*;

public class D420 {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int k=  qwe.nextInt();
		k+=2;
		int[] r = new int[k];
		int[] c = new int[k];

		ArrayList<Integer>[] byr = new ArrayList[n+2];
		for (int i = 0; i < byr.length; i++) {
			byr[i] = new ArrayList<Integer>();
		}
		ArrayList<Integer>[] byc = new ArrayList[m+2];
		for (int i = 0; i < byc.length; i++) {
			byc[i] = new ArrayList<Integer>();
		}

		int idf = 0;
		int idl = -1;
		for (int i = 0; i < c.length; i++) {
			if(i == c.length-1){
				r[i] = n+1;
				c[i] = m;
			}
			else if(i == c.length-2){
				r[i] = n;
				c[i] = m+1;
			}
			else{
				r[i] = qwe.nextInt();
				c[i] = qwe.nextInt();
				if(r[i] == 1 && c[i] == 1) idf = i;
				if(r[i] == n && c[i] == m) idl = i;
			}

			byr[r[i]].add(i);
			byc[c[i]].add(i);
		}

		int[][] been = new int[k][3];
		boolean[][] seen = new boolean[k][3];
		int oo = 987654321;
		for (int i = 0; i < been.length; i++) {
			Arrays.fill(been[i], oo);
		}

		ArrayDeque<state> dq = new ArrayDeque<state>();
		dq.add(new state(idf,0,0));

		while(!dq.isEmpty()){
			state at = dq.pollFirst();

			int dex = at.kdex;
			if(been[dex][at.lit] < at.dist || seen[dex][at.lit]) continue;

			//System.out.println(dq + ": before");
			//System.out.println("at: (" + r[dex] + ", " + c[dex] + ") " + at.dist + " kdex: " + dex);

			seen[dex][at.lit] = true;

			//System.out.println("at: r" + r[dex] + " c " + c[dex] + " cost: " + been[dex][at.lit] + " lit: " + at.lit);

			for(int rgo = -1; rgo <= 1; rgo++){
				int row = r[dex] + rgo;
				if(row >= 0 && row < byr.length){
					for(int kto : byr[row]){
						if(at.lit == 1){
							int newlit = rgo == 0 ? 1 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(c[kto] == c[dex] || rgo == 0 && Math.abs(c[kto]-c[dex]) == 1){
							int newlit=0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}

						}
						else{
							int newlit = 1;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}

						}
					}
				}
			}

			for(int cgo = -1; cgo <= 1; cgo++){
				int col = c[dex]+cgo;
				if(col >= 0 && col <= m){
					for(int kto: byc[col]){
						if(at.lit == 2){
							int newlit = cgo == 0 ? 2 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(r[kto] == r[dex] || cgo == 0 && Math.abs(r[kto]-r[dex]) == 1){
							int newlit = 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else{
							int newlit = 2;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}
						}
					}
				}
			}

			//System.out.println(dq);
			//System.out.println();

		}

		int ans = -1;
		for (int i = 2; i <= 2; i++) {
			if(been[k-1][i] != oo){
				if(ans == -1) ans = been[k-1][i];
				else ans = Math.min(ans, been[k-1][i]);
			}
		}

		int ans2 = -1;
		for (int i = 1; i <= 1; i++) {
			if(been[k-2][i] != oo){
				if(ans2 == -1) ans2 = been[k-2][i];
				else ans2 = Math.min(ans2, been[k-2][i]);
			}
		}

		int min = Math.min(ans, ans2);

		if(idl != -1) min = Math.min(min, been[idl][0]);

		System.out.println(min);

		qwe.close();
	}

//	static int getnewlit(int r, int c, int lit, int nr, int nc){
//		if(lit == 0) return 0;
//
//		if(lit < 4){
//			int dr = r-nr;
//			int ret = lit+dr;
//			if(ret < 1 || ret > 4) ret = 0;
//			return ret;
//		}
//		else{
//			int dc = c-nc;
//			int ret = lit+dc;
//			if(ret < 4 || ret > 6) ret= 0;
//			return ret;
//		}
//
//	}

	static class state{
		int kdex;
		int lit;
		int dist;

		public state(int kdex, int lit, int dist) {
			this.kdex = kdex;
			this.lit = lit;
			this.dist = dist;
		}

		@Override
		public String toString() {
			return "state [kdex=" + kdex + ", lit=" + lit + ", dist=" + dist + "]";
		}

	}

}


import java.util.*;

public class D {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int k=  qwe.nextInt();
		k++;
		int[] r = new int[k];
		int[] c = new int[k];

		ArrayList<Integer>[] byr = new ArrayList[n+1];
		for (int i = 0; i < byr.length; i++) {
			byr[i] = new ArrayList<Integer>();
		}
		ArrayList<Integer>[] byc = new ArrayList[m+1];
		for (int i = 0; i < byc.length; i++) {
			byc[i] = new ArrayList<Integer>();
		}

		int idf = 0;
		int idl = -1;
		for (int i = 0; i < c.length; i++) {
			if(i == c.length-1){
				r[i] = n;
				c[i] = m;
			}
			else{
				r[i] = qwe.nextInt();
				c[i] = qwe.nextInt();
				if(r[i] == 1 && c[i] == 1) idf = i;
				if(r[i] == n && c[i] == m) idl = i;
			}

			byr[r[i]].add(i);
			byc[c[i]].add(i);
		}

		int[][] been = new int[k][7];
		for (int i = 0; i < been.length; i++) {
			Arrays.fill(been[i], -1);
		}

		ArrayDeque<state> dq = new ArrayDeque<state>();
		dq.add(new state(idf,0,0));

		while(!dq.isEmpty()){
			state at = dq.pollFirst();

			int dex = at.kdex;

			if(been[dex][at.lit] != -1) continue;
			been[dex][at.lit] = at.dist;

			//System.out.println("at: r" + r[dex] + " c " + c[dex] + " cost: " + been[dex][at.lit] + " lit: " + at.lit);

			for(int rgo = -1; rgo <= 1; rgo++){
				int row = r[dex] + rgo;
				if(row >= 0 && row <= n){
					for(int kto : byr[row]){
						if((c[kto] == c[dex] || rgo == 0 && Math.abs(c[kto]-c[dex]) == 1 || at.lit == 2) && kto != k-1){
							int newlit = getnewlit(r[dex],c[dex],at.lit,r[kto],c[kto]);
							if(been[kto][newlit] == -1)
								dq.addFirst(new state(kto,newlit,at.dist));
						}
						else{
							if(been[kto][2] == -1){
								int ecost = 1;
								if(rgo + 2 == at.lit) ecost = 0;
								dq.addLast(new state(kto,2,at.dist+ecost));
							}
						}
					}
				}
			}

			for(int cgo = -1; cgo <= 1; cgo++){
				int col = c[dex]+cgo;
				if(col >= 0 && col <= m){
					for(int kto: byc[col]){
						if((r[kto] == r[dex] || cgo == 0 && Math.abs(r[kto]-r[dex]) == 1 || at.lit == 5) && kto != k-1){
							int newlit = getnewlit(r[dex],c[dex],at.lit,r[kto],c[kto]);
							if(been[kto][newlit] == -1)
								dq.addFirst(new state(kto,newlit,at.dist));
						}
						else{
							if(been[kto][5] == -1){
								int ecost = 1;
								if(cgo + 5 == at.lit) ecost = 0;
								dq.addLast(new state(kto,5,at.dist+ecost));
							}
						}
					}
				}
			}

//			if(at.lit == 0){
//
//				for(int nlit = 1; nlit <= 3; nlit++){
//
//				}
//				//dq.addLast(new );
//
//			}

		}

		int ans = -1;
		int dexl = idl == -1 ? k-1 : idl;
		for (int i = 0; i < 7; i++) {
			if(been[dexl][i] != -1){
				if(ans == -1) ans = been[dexl][i];
				else ans = Math.min(ans, been[dexl][i]);
			}
		}

		System.out.println(ans);

		qwe.close();
	}

	static int getnewlit(int r, int c, int lit, int nr, int nc){
		if(lit == 0) return 0;

		if(lit < 4){
			int dr = r-nr;
			int ret = lit+dr;
			if(ret < 1 || ret > 4) ret = 0;
			return ret;
		}
		else{
			int dc = c-nc;
			int ret = lit+dc;
			if(ret < 4 || ret > 6) ret= 0;
			return ret;
		}

	}

	static class state{
		int kdex;
		int lit;
		int dist;

		public state(int kdex, int lit, int dist) {
			this.kdex = kdex;
			this.lit = lit;
			this.dist = dist;
		}

	}

}


import java.util.*;

public class D420 {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int k=  qwe.nextInt();
		k+=2;
		int[] r = new int[k];
		int[] c = new int[k];

		ArrayList<Integer>[] byr = new ArrayList[n+2];
		for (int i = 0; i < byr.length; i++) {
			byr[i] = new ArrayList<Integer>();
		}
		ArrayList<Integer>[] byc = new ArrayList[m+2];
		for (int i = 0; i < byc.length; i++) {
			byc[i] = new ArrayList<Integer>();
		}

		int idf = 0;
		int idl = -1;
		for (int i = 0; i < c.length; i++) {
			if(i == c.length-1){
				r[i] = n+1;
				c[i] = m;
			}
			else if(i == c.length-2){
				r[i] = n;
				c[i] = m+1;
			}
			else{
				r[i] = qwe.nextInt();
				c[i] = qwe.nextInt();
				if(r[i] == 1 && c[i] == 1) idf = i;
				if(r[i] == n && c[i] == m) idl = i;
			}

			byr[r[i]].add(i);
			byc[c[i]].add(i);
		}

		int[][] been = new int[k][3];
		boolean[][] seen = new boolean[k][3];
		int oo = 987654321;
		for (int i = 0; i < been.length; i++) {
			Arrays.fill(been[i], oo);
		}

		ArrayDeque<state> dq = new ArrayDeque<state>();
		dq.add(new state(idf,0,0));

		while(!dq.isEmpty()){
			state at = dq.pollFirst();

			int dex = at.kdex;
			if(been[dex][at.lit] < at.dist || seen[dex][at.lit]) continue;

			//System.out.println(dq + ": before");
			//System.out.println("at: (" + r[dex] + ", " + c[dex] + ") " + at.dist + " kdex: " + dex);

			seen[dex][at.lit] = true;

			//System.out.println("at: r" + r[dex] + " c " + c[dex] + " cost: " + been[dex][at.lit] + " lit: " + at.lit);

			for(int rgo = -1; rgo <= 1; rgo++){
				int row = r[dex] + rgo;
				if(row >= 0 && row < byr.length){
					for(int kto : byr[row]){
						if(at.lit == 1){
							int newlit = rgo == 0 ? 1 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(c[kto] == c[dex] || rgo == 0 && Math.abs(c[kto]-c[dex]) == 1){
							int newlit=0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}

						}
						else{
							int newlit = 1;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}

						}
					}
				}
			}

			for(int cgo = -1; cgo <= 1; cgo++){
				int col = c[dex]+cgo;
				if(col >= 0 && col < byc.length){
					for(int kto: byc[col]){
						if(at.lit == 2){
							int newlit = cgo == 0 ? 2 : 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else if(r[kto] == r[dex] || cgo == 0 && Math.abs(r[kto]-r[dex]) == 1){
							int newlit = 0;
							if(been[kto][newlit] > at.dist)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								been[kto][newlit] = at.dist;
							}
						}
						else{
							int newlit = 2;
							if(been[kto][newlit] > at.dist+1){
								dq.addLast(new state(kto,newlit,at.dist+1));
								been[kto][newlit] = at.dist+1;
							}
						}
					}
				}
			}

			//System.out.println(dq);
			//System.out.println();

		}

		int ans = -1;
		for (int i = 2; i <= 2; i++) {
			if(been[k-1][i] != oo){
				if(ans == -1) ans = been[k-1][i];
				else ans = Math.min(ans, been[k-1][i]);
			}
		}

		int ans2 = -1;
		for (int i = 1; i <= 1; i++) {
			if(been[k-2][i] != oo){
				if(ans2 == -1) ans2 = been[k-2][i];
				else ans2 = Math.min(ans2, been[k-2][i]);
			}
		}

		int min = Math.min(ans, ans2);

		if(idl != -1){
			min = Math.min(min, been[idl][0]);
		}

		System.out.println(min);

		qwe.close();
	}

	static class state{
		int kdex;
		int lit;
		int dist;

		public state(int kdex, int lit, int dist) {
			this.kdex = kdex;
			this.lit = lit;
			this.dist = dist;
		}

		@Override
		public String toString() {
			return "state [kdex=" + kdex + ", lit=" + lit + ", dist=" + dist + "]";
		}

	}

}


import java.util.*;

public class D {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int k=  qwe.nextInt();
		k++;
		int[] r = new int[k];
		int[] c = new int[k];

		ArrayList<Integer>[] byr = new ArrayList[n+1];
		for (int i = 0; i < byr.length; i++) {
			byr[i] = new ArrayList<Integer>();
		}
		ArrayList<Integer>[] byc = new ArrayList[m+1];
		for (int i = 0; i < byc.length; i++) {
			byc[i] = new ArrayList<Integer>();
		}

		int idf = 0;
		int idl = -1;
		for (int i = 0; i < c.length; i++) {
			if(i == c.length-1){
				r[i] = n;
				c[i] = m;
			}
			else{
				r[i] = qwe.nextInt();
				c[i] = qwe.nextInt();
				if(r[i] == 1 && c[i] == 1) idf = i;
				if(r[i] == n && c[i] == m) idl = i;
			}

			byr[r[i]].add(i);
			byc[c[i]].add(i);
		}

		int[][] been = new int[k][7];
		for (int i = 0; i < been.length; i++) {
			Arrays.fill(been[i], -1);
		}

		ArrayDeque<state> dq = new ArrayDeque<state>();
		dq.add(new state(idf,0,0));

		while(!dq.isEmpty()){
			state at = dq.pollFirst();

			int dex = at.kdex;

			if(been[dex][at.lit] != -1) continue;
			been[dex][at.lit] = at.dist;

			//System.out.println("at: r" + r[dex] + " c " + c[dex] + " cost: " + been[dex][at.lit] + " lit: " + at.lit);

			for(int rgo = -1; rgo <= 1; rgo++){
				int row = r[dex] + rgo;
				if(row >= 0 && row <= n){
					for(int kto : byr[row]){
						if((c[kto] == c[dex] || rgo == 0 && Math.abs(c[kto]-c[dex]) == 1 || at.lit == 2) && kto != k-1){
							int newlit = getnewlit(r[dex],c[dex],at.lit,r[kto],c[kto]);
							if(been[kto][newlit] == -1)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								//been[kto][newlit] = at.dist;
							}
						}
						else{
							if(been[kto][2] == -1){
								int ecost = 1;
								if(rgo + 2 == at.lit) ecost = 0;
								dq.addLast(new state(kto,2,at.dist+ecost));
							}
						}
					}
				}
			}

			for(int cgo = -1; cgo <= 1; cgo++){
				int col = c[dex]+cgo;
				if(col >= 0 && col <= m){
					for(int kto: byc[col]){
						if((r[kto] == r[dex] || cgo == 0 && Math.abs(r[kto]-r[dex]) == 1 || at.lit == 5) && kto != k-1){
							int newlit = getnewlit(r[dex],c[dex],at.lit,r[kto],c[kto]);
							if(been[kto][newlit] == -1)
							{
								dq.addFirst(new state(kto,newlit,at.dist));
								//been[kto][newlit] = at.dist;
							}
						}
						else{
							if(been[kto][5] == -1){
								int ecost = 1;
								if(cgo + 5 == at.lit) ecost = 0;
								dq.addLast(new state(kto,5,at.dist+ecost));
							}
						}
					}
				}
			}

//			if(at.lit == 0){
//
//				for(int nlit = 1; nlit <= 3; nlit++){
//
//				}
//				//dq.addLast(new );
//
//			}

		}

		int ans = -1;
		int dexl = idl == -1 ? k-1 : idl;
		for (int i = 0; i < 7; i++) {
			if(been[dexl][i] != -1){
				if(ans == -1) ans = been[dexl][i];
				else ans = Math.min(ans, been[dexl][i]);
			}
		}

		System.out.println(ans);

		qwe.close();
	}

	static int getnewlit(int r, int c, int lit, int nr, int nc){
		if(lit == 0) return 0;

		if(lit < 4){
			int dr = r-nr;
			int ret = lit+dr;
			if(ret < 1 || ret > 4) ret = 0;
			return ret;
		}
		else{
			int dc = c-nc;
			int ret = lit+dc;
			if(ret < 4 || ret > 6) ret= 0;
			return ret;
		}

	}

	static class state{
		int kdex;
		int lit;
		int dist;

		public state(int kdex, int lit, int dist) {
			this.kdex = kdex;
			this.lit = lit;
			this.dist = dist;
		}

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static ArrayList<Edge>[] adjList;
    static int n, m, V;
    static boolean[][] arr;
    static int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        n = sc.nextInt();
        m = sc.nextInt();
        V = n * m + n + m;
        int k = sc.nextInt();
        arr = new boolean[n][m];
        adjList = new ArrayList[V];
        for (int i = 0; i < V; i++) adjList[i] = new ArrayList<>();
        while (k-- > 0) {
            arr[sc.nextInt() - 1][sc.nextInt() - 1] = true;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                for (k = 0; k < 4; k++) {
                    int ni = i + dx[k], nj = j + dy[k];
                    if (ni != -1 && nj != -1 && ni != n && nj != m && arr[ni][nj]) {
                        adjList[map(i, j)].add(new Edge(map(ni, nj), 0));
                    }
                }

                // from row to cell
                adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                // from col to cell
                adjList[map(j, -2)].add(new Edge(map(i, j), 0));

                if (arr[i][j]) {
                    adjList[map(i, j)].add(new Edge(i, -1));
                    adjList[map(i, j)].add(new Edge(j, -2));
                    if (i + 1 < n) adjList[map(i, j)].add(new Edge(map(i + 1, -1), 1));
                    if (j + 1 < m) adjList[map(i, j)].add(new Edge(map(j + 1, -2), 1));
                    if (i - 1 != -1) adjList[map(i, j)].add(new Edge(map(i - 1, -1), 1));
                    if (j - 1 != -1) adjList[map(i, j)].add(new Edge(map(j - 1, -2), 1));
                }
            }
        }
        out.println(dijkstra(0, n * m - 1));
        out.flush();
        out.close();
    }

    static class Edge implements Comparable<Edge> {
        int node, cost;
        Edge (int n, int c) {
            node = n;
            cost = c;
        }

        @Override
        public int compareTo(Edge edge) {
            return cost - edge.cost;
        }
    }
    static final int INF = (int)1e9;

    static int dijkstra(int S, int T)
    {
        int[] dist = new int[V];
        Arrays.fill(dist, INF);
        PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
        dist[S] = 0;
        pq.add(new Edge(S, 0));
        while(!pq.isEmpty())
        {
            Edge cur = pq.remove();
            if(cur.node == T) //remove if all nodes are sinks
                return dist[T];
            if(cur.cost > dist[cur.node])
                continue;
            for(Edge nxt : adjList[cur.node])
                if(cur.cost + nxt.cost < dist[nxt.node])
                {
                    dist[nxt.node] = cur.cost + nxt.cost;
                    pq.add(new Edge(nxt.node, dist[nxt.node]));
                }

        }
        return -1;
    }

    static int map (int row, int col) {
        if (col == -1) return n * m + row;
        else if (col == -2) return n * m + n + row;
        return row * m + col;
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static ArrayList<Edge>[] adjList;
    static int n, m, V;
    static boolean[][] arr;
    static int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        n = sc.nextInt();
        m = sc.nextInt();
        V = n * m + n + m;
        int k = sc.nextInt();
        arr = new boolean[n][m];
        adjList = new ArrayList[V];
        for (int i = 0; i < V; i++) adjList[i] = new ArrayList<>();
        while (k-- > 0) {
            arr[sc.nextInt() - 1][sc.nextInt() - 1] = true;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                for (k = 0; k < 4; k++) {
                    int ni = i + dx[k], nj = j + dy[k];
                    if (ni != -1 && nj != -1 && ni != n && nj != m && arr[ni][nj]) {
                        adjList[map(i, j)].add(new Edge(map(ni, nj), 0));
                    }
                }

                // from row to cell
                adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                // from col to cell
                adjList[map(j, -2)].add(new Edge(map(i, j), 0));

                if (arr[i][j]) {
                    adjList[map(i, j)].add(new Edge(map(i, -1), 1));
                    adjList[map(i, j)].add(new Edge(map(j, -2), 1));
                    if (i + 1 < n) adjList[map(i, j)].add(new Edge(map(i + 1, -1), 1));
                    if (j + 1 < m) adjList[map(i, j)].add(new Edge(map(j + 1, -2), 1));
                    if (i - 1 != -1) adjList[map(i, j)].add(new Edge(map(i - 1, -1), 1));
                    if (j - 1 != -1) adjList[map(i, j)].add(new Edge(map(j - 1, -2), 1));
                }
            }
        }
        out.println(dijkstra(0, n * m - 1));
        out.flush();
        out.close();
    }

    static class Edge implements Comparable<Edge> {
        int node, cost;
        Edge (int n, int c) {
            node = n;
            cost = c;
        }

        @Override
        public int compareTo(Edge edge) {
            return cost - edge.cost;
        }
    }
    static final int INF = (int)1e9;

    static int dijkstra(int S, int T)
    {
        int[] dist = new int[V];
        Arrays.fill(dist, INF);
        PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
        dist[S] = 0;
        pq.add(new Edge(S, 0));
        while(!pq.isEmpty())
        {
            Edge cur = pq.remove();
            if(cur.node == T) //remove if all nodes are sinks
                return dist[T];
            if(cur.cost > dist[cur.node])
                continue;
            for(Edge nxt : adjList[cur.node])
                if(cur.cost + nxt.cost < dist[nxt.node])
                {
                    dist[nxt.node] = cur.cost + nxt.cost;
                    pq.add(new Edge(nxt.node, dist[nxt.node]));
                }

        }
        return -1;
    }

    static int map (int row, int col) {
        if (col == -1) return n * m + row;
        else if (col == -2) return n * m + n + row;
        return row * m + col;
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static ArrayList<Edge>[] adjList;
    static int n, m, V;
    static boolean[][] arr;
    static int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        n = sc.nextInt();
        m = sc.nextInt();
        V = n * m + n + m;
        int k = sc.nextInt();
        arr = new boolean[n][m];
        adjList = new ArrayList[(int)1e6];
        while (k-- > 0) {
            arr[sc.nextInt() - 1][sc.nextInt() - 1] = true;
        }
        map(0, 0);
        map(n - 1, m - 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (arr[i][j])
                    for (k = 0; k < 4; k++) {
                        int ni = i + dx[k], nj = j + dy[k];
                        if (ni != -1 && nj != -1 && ni != n && nj != m && arr[ni][nj]) {
                            adjList[map(i, j)].add(new Edge(map(ni, nj), 0));
                        }
                    }

                // from row to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                // from col to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(j, -2)].add(new Edge(map(i, j), 0));

                if (arr[i][j]) {
                    adjList[map(i, j)].add(new Edge(map(i, -1), 1));
                    adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                    adjList[map(i, j)].add(new Edge(map(j, -2), 1));
                    adjList[map(j, -2)].add(new Edge(map(i, j), 0));
                    if (i + 1 < n) {
                        adjList[map(i, j)].add(new Edge(map(i + 1, -1), 1));
                        adjList[map(i + 1, -1)].add(new Edge(map(i, j), 0));
                    }
                    if (j + 1 < m) {
                        adjList[map(i, j)].add(new Edge(map(j + 1, -2), 1));
                        adjList[map(j + 1, -2)].add(new Edge(map(i, j), 0));
                    }
                    if (i - 1 != -1) {
                        adjList[map(i, j)].add(new Edge(map(i - 1, -1), 1));
                        adjList[map(i - 1, -1)].add(new Edge(map(i, j), 0));
                    }
                    if (j - 1 != -1) {
                        adjList[map(i, j)].add(new Edge(map(j - 1, -2), 1));
                        adjList[map(j - 1, -2)].add(new Edge(map(i, j), 0));
                    }
                }
            }
        }
        out.println(dijkstra(map(0, 0), map(n - 1, m - 1)));
        out.flush();
        out.close();
    }

    static class Edge implements Comparable<Edge> {
        int node, cost;
        Edge (int n, int c) {
            node = n;
            cost = c;
        }

        @Override
        public int compareTo(Edge edge) {
            return cost - edge.cost;
        }
    }
    static final int INF = (int)1e9;

    static int dijkstra(int S, int T)
    {
        int[] dist = new int[V];
        Arrays.fill(dist, INF);
        PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
        dist[S] = 0;
        pq.add(new Edge(S, 0));
        while(!pq.isEmpty())
        {
            Edge cur = pq.remove();
            if(cur.node == T) //remove if all nodes are sinks
                return dist[T];
            if(cur.cost > dist[cur.node])
                continue;
            for(Edge nxt : adjList[cur.node])
                if(cur.cost + nxt.cost < dist[nxt.node])
                {
                    dist[nxt.node] = cur.cost + nxt.cost;
                    pq.add(new Edge(nxt.node, dist[nxt.node]));
                }

        }
        return -1;
    }

    static HashMap<Integer,Integer> map = new HashMap<>();
    static int mapIdx = 0;
    static int map (int row, int col) {
        int res;
        if (col == -1) res = n * m + row;
        else if (col == -2) res = n * m + n + row;
        else res = row * m + col;

        if (!map.containsKey(res)) map.put(res, mapIdx++);
        res = map.get(res);
        if (adjList[res] == null) adjList[res] = new ArrayList<>();
        return res;
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }
        public Scanner(FileReader s) {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static ArrayList<Edge>[] adjList;
    static int n, m, V;
    static boolean[][] arr;
    static int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        n = sc.nextInt();
        m = sc.nextInt();
        V = n * m + n + m;
        int k = sc.nextInt();
        arr = new boolean[n][m];
        adjList = new ArrayList[V];
        while (k-- > 0) {
            arr[sc.nextInt() - 1][sc.nextInt() - 1] = true;
        }
        map(0, 0);
        map(n - 1, m - 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (arr[i][j])
                for (k = 0; k < 4; k++) {
                    int ni = i + dx[k], nj = j + dy[k];
                    if (ni != -1 && nj != -1 && ni != n && nj != m && arr[ni][nj]) {
                        adjList[map(i, j)].add(new Edge(map(ni, nj), 0));
                    }
                }

                // from row to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                // from col to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(j, -2)].add(new Edge(map(i, j), 0));

                if (arr[i][j]) {
                    adjList[map(i, j)].add(new Edge(map(i, -1), 1));
                    adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                    adjList[map(i, j)].add(new Edge(map(j, -2), 1));
                    adjList[map(j, -2)].add(new Edge(map(i, j), 0));
                    if (i + 1 < n) {
                        adjList[map(i, j)].add(new Edge(map(i + 1, -1), 1));
                        adjList[map(i + 1, -1)].add(new Edge(map(i, j), 0));
                    }
                    if (j + 1 < m) {
                        adjList[map(i, j)].add(new Edge(map(j + 1, -2), 1));
                        adjList[map(j + 1, -2)].add(new Edge(map(i, j), 0));
                    }
                    if (i - 1 != -1) {
                        adjList[map(i, j)].add(new Edge(map(i - 1, -1), 1));
                        adjList[map(i - 1, -1)].add(new Edge(map(i, j), 0));
                    }
                    if (j - 1 != -1) {
                        adjList[map(i, j)].add(new Edge(map(j - 1, -2), 1));
                        adjList[map(j - 1, -2)].add(new Edge(map(i, j), 0));
                    }
                }
            }
        }
        out.println(dijkstra(0, n * m - 1));
        out.flush();
        out.close();
    }

    static class Edge implements Comparable<Edge> {
        int node, cost;
        Edge (int n, int c) {
            node = n;
            cost = c;
        }

        @Override
        public int compareTo(Edge edge) {
            return cost - edge.cost;
        }
    }
    static final int INF = (int)1e9;

    static int dijkstra(int S, int T)
    {
        int[] dist = new int[V];
        Arrays.fill(dist, INF);
        PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
        dist[S] = 0;
        pq.add(new Edge(S, 0));
        while(!pq.isEmpty())
        {
            Edge cur = pq.remove();
            if(cur.node == T) //remove if all nodes are sinks
                return dist[T];
            if(cur.cost > dist[cur.node])
                continue;
            for(Edge nxt : adjList[cur.node])
                if(cur.cost + nxt.cost < dist[nxt.node])
                {
                    dist[nxt.node] = cur.cost + nxt.cost;
                    pq.add(new Edge(nxt.node, dist[nxt.node]));
                }

        }
        return -1;
    }

    static int map (int row, int col) {
        int res;
        if (col == -1) res = n * m + row;
        else if (col == -2) res = n * m + n + row;
        else res = row * m + col;
        if (adjList[res] == null) adjList[res] = new ArrayList<>();
        return res;
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }
        public Scanner(FileReader s) {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static ArrayList<Edge>[] adjList;
    static int n, m, V;
    static boolean[][] arr;
    static int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        n = sc.nextInt();
        m = sc.nextInt();
        V = n * m + n + m;
        int k = sc.nextInt();
        arr = new boolean[n][m];
        adjList = new ArrayList[V];
        for (int i = 0; i < V; i++) adjList[i] = new ArrayList<>();
        while (k-- > 0) {
            arr[sc.nextInt() - 1][sc.nextInt() - 1] = true;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                for (k = 0; k < 4; k++) {
                    int ni = i + dx[k], nj = j + dy[k];
                    if (ni != -1 && nj != -1 && ni != n && nj != m && arr[ni][nj]) {
                        adjList[map(i, j)].add(new Edge(map(ni, nj), 0));
                    }
                }
                // from row to cell
                adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                // from col to cell
                adjList[map(j, -2)].add(new Edge(map(i, j), 0));
                if (arr[i][j]) {
                    if (i + 1 < n) adjList[map(i, j)].add(new Edge(map(i + 1, -1), 1));
                    if (j + 1 < m) adjList[map(i, j)].add(new Edge(map(j + 1, -2), 1));
                    if (i - 1 != -1) adjList[map(i, j)].add(new Edge(map(i - 1, -1), 1));
                    if (j - 1 != -1) adjList[map(i, j)].add(new Edge(map(j - 1, -2), 1));
                }
            }
        }
        adjList[map(n - 1, -1)].add(new Edge(map(n - 1, m - 1), 0));
        adjList[map(m - 1, -2)].add(new Edge(map(n - 1, m - 1), 0));
        out.println(dijkstra(0, n * m - 1));
        out.flush();
        out.close();
    }

    static class Edge implements Comparable<Edge> {
        int node, cost;
        Edge (int n, int c) {
            node = n;
            cost = c;
        }

        @Override
        public int compareTo(Edge edge) {
            return cost - edge.cost;
        }
    }
    static final int INF = (int)1e9;

    static int dijkstra(int S, int T)
    {
        int[] dist = new int[V];
        Arrays.fill(dist, INF);
        PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
        dist[S] = 0;
        pq.add(new Edge(S, 0));
        while(!pq.isEmpty())
        {
            Edge cur = pq.remove();
            if(cur.node == T) //remove if all nodes are sinks
                return dist[T];
            if(cur.cost > dist[cur.node])
                continue;
            for(Edge nxt : adjList[cur.node])
                if(cur.cost + nxt.cost < dist[nxt.node])
                {
                    dist[nxt.node] = cur.cost + nxt.cost;
                    pq.add(new Edge(nxt.node, dist[nxt.node]));
                }

        }
        return -1;
    }

    static int map (int row, int col) {
        if (col == -1) return n * m + row;
        else if (col == -2) return n * m + n + row;
        return row * m + col;
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static ArrayList<Edge>[] adjList;
    static int n, m, V;
    static boolean[][] arr;
    static int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        n = sc.nextInt();
        m = sc.nextInt();
        V = n * m + n + m;
        int k = sc.nextInt();
        arr = new boolean[n][m];
        adjList = new ArrayList[(int)1e6];
        while (k-- > 0) {
            arr[sc.nextInt() - 1][sc.nextInt() - 1] = true;
        }
        map(0, 0);
        map(n - 1, m - 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (arr[i][j])
                    for (k = 0; k < 4; k++) {
                        int ni = i + dx[k], nj = j + dy[k];
                        if (ni != -1 && nj != -1 && ni != n && nj != m && arr[ni][nj]) {
                            adjList[map(i, j)].add(new Edge(map(ni, nj), 0));
                        }
                    }

                // from row to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                // from col to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(j, -2)].add(new Edge(map(i, j), 0));

                if (arr[i][j]) {
                    adjList[map(i, j)].add(new Edge(map(i, -1), 1));
                    adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                    adjList[map(i, j)].add(new Edge(map(j, -2), 1));
                    adjList[map(j, -2)].add(new Edge(map(i, j), 0));
                    if (i + 1 < n) {
                        adjList[map(i, j)].add(new Edge(map(i + 1, -1), 1));
                        adjList[map(i + 1, -1)].add(new Edge(map(i, j), 0));
                    }
                    if (j + 1 < m) {
                        adjList[map(i, j)].add(new Edge(map(j + 1, -2), 1));
                        adjList[map(j + 1, -2)].add(new Edge(map(i, j), 0));
                    }
                    if (i - 1 != -1) {
                        adjList[map(i, j)].add(new Edge(map(i - 1, -1), 1));
                        adjList[map(i - 1, -1)].add(new Edge(map(i, j), 0));
                    }
                    if (j - 1 != -1) {
                        adjList[map(i, j)].add(new Edge(map(j - 1, -2), 1));
                        adjList[map(j - 1, -2)].add(new Edge(map(i, j), 0));
                    }
                }
            }
        }
        out.println(dijkstra(map(0, 0), map(n - 1, m - 1)));
        out.flush();
        out.close();
    }

    static class Edge implements Comparable<Edge> {
        int node, cost;
        Edge (int n, int c) {
            node = n;
            cost = c;
        }

        @Override
        public int compareTo(Edge edge) {
            return cost - edge.cost;
        }
    }
    static final int INF = (int)1e9;

    static int dijkstra(int S, int T)
    {
        int[] dist = new int[mapIdx + 1];
        Arrays.fill(dist, INF);
        PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
        dist[S] = 0;
        pq.add(new Edge(S, 0));
        while(!pq.isEmpty())
        {
            Edge cur = pq.remove();
            if(cur.node == T) //remove if all nodes are sinks
                return dist[T];
            if(cur.cost > dist[cur.node])
                continue;
            for(Edge nxt : adjList[cur.node])
                if(cur.cost + nxt.cost < dist[nxt.node])
                {
                    dist[nxt.node] = cur.cost + nxt.cost;
                    pq.add(new Edge(nxt.node, dist[nxt.node]));
                }

        }
        return -1;
    }

    static HashMap<Integer,Integer> map = new HashMap<>();
    static int mapIdx = 0;
    static int map (int row, int col) {
        int res;
        if (col == -1) res = n * m + row;
        else if (col == -2) res = n * m + n + row;
        else res = row * m + col;

        if (!map.containsKey(res)) map.put(res, mapIdx++);
        res = map.get(res);
        if (adjList[res] == null) adjList[res] = new ArrayList<>();
        return res;
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }
        public Scanner(FileReader s) {
            br = new BufferedReader(s);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static ArrayList<Edge>[] adjList;
    static int n, m, V;
    static boolean[][] arr;
    static int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        n = sc.nextInt();
        m = sc.nextInt();
        V = n * m + n + m;
        int k = sc.nextInt();
        arr = new boolean[n][m];
        adjList = new ArrayList[V];
        while (k-- > 0) {
            arr[sc.nextInt() - 1][sc.nextInt() - 1] = true;
        }
        map(0, 0);
        map(n - 1, m - 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                for (k = 0; k < 4; k++) {
                    int ni = i + dx[k], nj = j + dy[k];
                    if (ni != -1 && nj != -1 && ni != n && nj != m && arr[ni][nj]) {
                        adjList[map(i, j)].add(new Edge(map(ni, nj), 0));
                    }
                }

                // from row to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                // from col to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(j, -2)].add(new Edge(map(i, j), 0));

                if (arr[i][j]) {
                    adjList[map(i, j)].add(new Edge(map(i, -1), 1));
                    adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                    adjList[map(i, j)].add(new Edge(map(j, -2), 1));
                    adjList[map(j, -2)].add(new Edge(map(i, j), 0));
                    if (i + 1 < n) {
                        adjList[map(i, j)].add(new Edge(map(i + 1, -1), 1));
                        adjList[map(i + 1, -1)].add(new Edge(map(i, j), 0));
                    }
                    if (j + 1 < m) {
                        adjList[map(i, j)].add(new Edge(map(j + 1, -2), 1));
                        adjList[map(j + 1, -2)].add(new Edge(map(i, j), 0));
                    }
                    if (i - 1 != -1) {
                        adjList[map(i, j)].add(new Edge(map(i - 1, -1), 1));
                        adjList[map(i - 1, -1)].add(new Edge(map(i, j), 0));
                    }
                    if (j - 1 != -1) {
                        adjList[map(i, j)].add(new Edge(map(j - 1, -2), 1));
                        adjList[map(j - 1, -2)].add(new Edge(map(i, j), 0));
                    }
                }
            }
        }
        out.println(dijkstra(0, n * m - 1));
        out.flush();
        out.close();
    }

    static class Edge implements Comparable<Edge> {
        int node, cost;
        Edge (int n, int c) {
            node = n;
            cost = c;
        }

        @Override
        public int compareTo(Edge edge) {
            return cost - edge.cost;
        }
    }
    static final int INF = (int)1e9;

    static int dijkstra(int S, int T)
    {
        int[] dist = new int[V];
        Arrays.fill(dist, INF);
        PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
        dist[S] = 0;
        pq.add(new Edge(S, 0));
        while(!pq.isEmpty())
        {
            Edge cur = pq.remove();
            if(cur.node == T) //remove if all nodes are sinks
                return dist[T];
            if(cur.cost > dist[cur.node])
                continue;
            for(Edge nxt : adjList[cur.node])
                if(cur.cost + nxt.cost < dist[nxt.node])
                {
                    dist[nxt.node] = cur.cost + nxt.cost;
                    pq.add(new Edge(nxt.node, dist[nxt.node]));
                }

        }
        return -1;
    }

    static int map (int row, int col) {
        int res;
        if (col == -1) res = n * m + row;
        else if (col == -2) res = n * m + n + row;
        else res = row * m + col;
        if (adjList[res] == null) adjList[res] = new ArrayList<>();
        return res;
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class Main {

    static ArrayList<Edge>[] adjList;
    static int n, m, V;
    static boolean[][] arr;
    static int[] dx = {0,0,1,-1}, dy = {1,-1,0,0};

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        n = sc.nextInt();
        m = sc.nextInt();
        V = n * m + n + m;
        int k = sc.nextInt();
        arr = new boolean[n][m];
        adjList = new ArrayList[V];
        while (k-- > 0) {
            arr[sc.nextInt() - 1][sc.nextInt() - 1] = true;
        }
        map(0, 0);
        map(n - 1, m - 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                for (k = 0; k < 4; k++) {
                    int ni = i + dx[k], nj = j + dy[k];
                    if (ni != -1 && nj != -1 && ni != n && nj != m && arr[ni][nj]) {
                        adjList[map(i, j)].add(new Edge(map(ni, nj), 0));
                    }
                }

                // from row to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(i, -1)].add(new Edge(map(i, j), 0));
                // from col to cell
                if (arr[i][j] || (i == n - 1 && j == m - 1)) adjList[map(j, -2)].add(new Edge(map(i, j), 0));

                if (arr[i][j]) {
                    adjList[map(i, j)].add(new Edge(map(i, -1), 1));
                    adjList[map(i, j)].add(new Edge(map(j, -2), 1));
                    if (i + 1 < n) adjList[map(i, j)].add(new Edge(map(i + 1, -1), 1));
                    if (j + 1 < m) adjList[map(i, j)].add(new Edge(map(j + 1, -2), 1));
                    if (i - 1 != -1) adjList[map(i, j)].add(new Edge(map(i - 1, -1), 1));
                    if (j - 1 != -1) adjList[map(i, j)].add(new Edge(map(j - 1, -2), 1));
                }
            }
        }
        out.println(dijkstra(0, n * m - 1));
        out.flush();
        out.close();
    }

    static class Edge implements Comparable<Edge> {
        int node, cost;
        Edge (int n, int c) {
            node = n;
            cost = c;
        }

        @Override
        public int compareTo(Edge edge) {
            return cost - edge.cost;
        }
    }
    static final int INF = (int)1e9;

    static int dijkstra(int S, int T)
    {
        int[] dist = new int[V];
        Arrays.fill(dist, INF);
        PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
        dist[S] = 0;
        pq.add(new Edge(S, 0));
        while(!pq.isEmpty())
        {
            Edge cur = pq.remove();
            if(cur.node == T) //remove if all nodes are sinks
                return dist[T];
            if(cur.cost > dist[cur.node])
                continue;
            for(Edge nxt : adjList[cur.node])
                if(cur.cost + nxt.cost < dist[nxt.node])
                {
                    dist[nxt.node] = cur.cost + nxt.cost;
                    pq.add(new Edge(nxt.node, dist[nxt.node]));
                }

        }
        return -1;
    }

    static int map (int row, int col) {
        int res;
        if (col == -1) res = n * m + row;
        else if (col == -2) res = n * m + n + row;
        else res = row * m + col;
        if (adjList[res] == null) adjList[res] = new ArrayList<>();
        return res;
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}


import java.util.*;
import java.io.*;
public class D {
    void addEdge(int id, Node to, HashMap<Integer, Node> map) {
        Node r = map.get(id);
        Edge e = new Edge(r, to, 1);
        to.edges.add(e);
        e = new Edge(r, to, 0);
        r.edges.add(e);
    }
    void solve(BufferedReader in) throws Exception {
        int[] xx = toInts(in.readLine());
        int n = xx[0], m = xx[1], k = xx[2];
        ArrayList<Integer>[] rows = new ArrayList[n];
        ArrayList<Integer>[] cols = new ArrayList[m];
        HashMap<Integer, Node> map = new HashMap<>();
        for(int i = 0; i<n; i++) {
            rows[i] = new ArrayList<>();
            int id = i + n*m;
            map.put(id, new Node(id));
        }
        for(int i = 0; i<m; i++) {
            cols[i] = new ArrayList<>();
            int id = i + n + n*m;
            map.put(id, new Node(id));
        }
        for(int i = 0; i<k; i++) {
            xx = toInts(in.readLine());
            int a = xx[0]-1, b = xx[1]-1;
            rows[a].add(b);
            cols[b].add(a);
            int idx = a*m + b;
            Node x = new Node(idx);
            map.put(idx, x);
            if(a > 0) addEdge(n*m + a-1, x, map);
            addEdge(n*m + a, x, map);
            if(a < n-1) addEdge(n*m + a + 1, x, map);
            if(b > 0) addEdge(n*m + n + b - 1, x, map);
            addEdge(n*m + n + b, x, map);
            if(b < m-1) addEdge(n*m + n + b + 1, x, map);
        }
        for(int i = 0; i<n; i++) {
            Collections.sort(rows[i]);
            for(int j = 0; j<rows[i].size() - 1; j++) {
                int a = rows[i].get(j), b = rows[i].get(j+1);
                int ida = i*m + a, idb = i*m + b;
                if(a + 1 == b) {
                    Node x = map.get(ida), y = map.get(idb);
                    Edge e = new Edge(x, y, 0);
                    x.edges.add(e);
                    y.edges.add(e);
                }
            }
        }
        for(int i = 0; i<m; i++) {
            Collections.sort(cols[i]);
            for(int j = 0; j<cols[i].size() - 1; j++) {
                int a = cols[i].get(j), b = cols[i].get(j+1);
                int ida = a*m + i, idb = b*m + i;
                if(a + 1 == b) {
                    Node x = map.get(ida), y = map.get(idb);
                    Edge e = new Edge(x, y, 0);
                    x.edges.add(e);
                    y.edges.add(e);
                }
            }
        }
        System.out.println(map.get(0).djikstra(map.get(n*m - 1), map.get(n*m + n-1), map.get(n*m + n + m - 1)));

    }
    int toInt(String s) {return Integer.parseInt(s);}
    int[] toInts(String s) {
        String[] a = s.split(" ");
        int[] o = new int[a.length];
        for(int i = 0; i<a.length; i++) o[i] = toInt(a[i]);
        return o;
    }
    void e(Object o) {
        System.err.println(o);
    }
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        (new D()).solve(in);
    }
}

class Node implements Comparable<Node>{
  LinkedList<Edge> edges = new LinkedList<>();
  int w;
  int id;
  public Node(int id) {
    w = Integer.MAX_VALUE;
    this.id = id;
  }
  public int compareTo(Node n) {
    if(w != n.w) return w - n.w;
    return id - n.id;
  }
  //Asumes all nodes have weight MAXINT.
  public int djikstra(Node x1, Node x2, Node x3) {
    this.w = 0;
    TreeSet<Node> set = new TreeSet<>();
    set.add(this);
    while(!set.isEmpty()) {
      Node curr = set.pollFirst();
      if(x1 == curr) return x1.w;
      if(x2 == curr) return x2.w;
      if(x3 == curr) return x3.w;
      for(Edge e: curr.edges) {
        Node other = e.u == curr? e.v : e.u;
        if(other.w > e.cost + curr.w) {
          set.remove(other);
          other.w = e.cost + curr.w;
          set.add(other);
        }
      }
    }
    return -1;
  }
}
class Edge {
  Node u,v;
  int cost;
  public Edge(Node u, Node v, int c) {
    this.u = u; this.v = v;
    cost = c;
  }
}


import java.io.*;
import java.util.*;

public class D { // noac

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		HashSet<Integer>[] byrow = new HashSet[n];
		for(int i = 0; i < n; i++) byrow[i] = new HashSet<>();
		HashSet<Integer>[] bycol = new HashSet[m];
		for(int i = 0; i < m; i++) bycol[i] = new HashSet<>();
		for(int i = 0; i < k; i++) {
			int x = sc.nextInt()-1;
			int y = sc.nextInt()-1;
//			Coor c = new Coor(x, y);
			byrow[x].add(y);
			bycol[y].add(x);
		}
		int maxv = 1000000000;
		HashMap<Coor, Integer> dist = new HashMap<>();
		for(int x = 0; x < n; x++) {
			for(int y: byrow[x]) {
				dist.put(new Coor(x, y), maxv);
			}
		}
		dist.put(new Coor(0, 0), 0);
		PriorityQueue<Coorw> q = new PriorityQueue<>();
		q.add(new Coorw(0, 0, 0));
		int[][] dir = {{0, 1}, {0, -1}, {1,0}, {-1, 0}};
		while(!q.isEmpty()) {
			Coorw c = q.poll();
			if(c.w > dist.get(new Coor(c.x, c.y))) continue;
			for(int[] d: dir) {
				int x = c.x + d[0], y = c.y + d[1];
				if(x >= 0 && y >= 0 && x < n && y < m && byrow[x].contains(y) && dist.get(new Coor(x, y)) > c.w+0) {
					dist.put(new Coor(x, y), c.w);
					q.add(new Coorw(x, y, c.w));
				}
			}
			for(int dx = -2; dx <= 2; dx++) {
				if(c.x+dx >= 0 && c.x+dx < n) {
					for(int y: byrow[c.x+dx]) {
						if(dist.get(new Coor(c.x+dx, y)) > c.w+1) {
							dist.put(new Coor(c.x+dx, y), c.w+1);
							q.add(new Coorw(c.x+dx, y, c.w+1));
						}
					}
				}
			}
			for(int dy = -2; dy <= 2; dy++) {
				if(c.y+dy >= 0 && c.y+dy < m) {
					for(int x: bycol[c.y+dy]) {
						if(dist.get(new Coor(x, c.y+dy)) > c.w+1) {
							dist.put(new Coor(x, c.y+dy), c.w+1);
							q.add(new Coorw(x, c.y+dy, c.w+1));
						}
					}
				}
			}
		}
		int res = maxv;
		if(dist.containsKey(new Coor(n-1, m-1))){
			res = dist.get(new Coor(n-1, m-1));
		}
		for(int y: byrow[n-1]) {
			res = Math.min(res, dist.get(new Coor(n-1, y))+1);
		}
		for(int y: byrow[n-2]) {
			res = Math.min(res, dist.get(new Coor(n-2, y))+1);
		}
		for(int x: bycol[m-1]) {
			res = Math.min(res, dist.get(new Coor(x, m-1))+1);
		}
		for(int x: bycol[m-2]) {
			res = Math.min(res, dist.get(new Coor(x, m-2))+1);
		}
		if(res == maxv) System.out.println(-1);
		else System.out.println(res);
	}
	static class Coorw implements Comparable<Coorw>{
		int x, y, w;
		public Coorw(int x, int y, int w) {
			this.x = x; this.y = y; this.w = w;
		}
		public int compareTo(Coorw c) {
			return w - c.w;
		}
		public String toString() {
			return x+" "+y+" "+w;
		}
	}
	static class Coor{
		int x, y;
		public Coor(int x, int y) {
			this.x = x; this.y = y;
		}
		@Override
		public int hashCode() {
			return x+y;
		}
		@Override
		public boolean equals(Object o) {
			Coor c = (Coor)o;
			return x == c.x && y == c.y;
		}
		public String toString() {
			return x+" "+y;
		}
	}

	static class FastScanner {
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		public FastScanner() {
			reader = new BufferedReader(new InputStreamReader(System.in), 32768);
			tokenizer = null;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch(IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

}


import java.io.*;
import java.util.*;

public class D {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		HashSet<Integer>[] byrow = new HashSet[n];
		for(int i = 0; i < n; i++) byrow[i] = new HashSet<>();
		HashSet<Integer>[] bycol = new HashSet[m];
		for(int i = 0; i < m; i++) bycol[i] = new HashSet<>();
		for(int i = 0; i < k; i++) {
			int x = sc.nextInt()-1;
			int y = sc.nextInt()-1;
//			Coor c = new Coor(x, y);
			byrow[x].add(y);
			bycol[y].add(x);
		}
		int maxv = 1000000000;
		HashMap<Coor, Integer> dist = new HashMap<>();
		for(int x = 0; x < n; x++) {
			for(int y: byrow[x]) {
				dist.put(new Coor(x, y), maxv);
			}
		}
		dist.put(new Coor(0, 0), 0);
		PriorityQueue<Coorw> q = new PriorityQueue<>();
		q.add(new Coorw(0, 0, 0));
		int[][] dir = {{0, 1}, {0, -1}, {1,0}, {0,1}};
		while(!q.isEmpty()) {
			Coorw c = q.poll();
			if(c.w > dist.get(new Coor(c.x, c.y))) continue;
			for(int[] d: dir) {
				int x = c.x + d[0], y = c.y + d[1];
				if(x >= 0 && y >= 0 && x < n && y < m && byrow[x].contains(y) && dist.get(new Coor(x, y)) > c.w+0) {
					dist.put(new Coor(x, y), c.w);
					q.add(new Coorw(x, y, c.w));
				}
			}
			for(int dx = -2; dx <= 2; dx++) {
				if(c.x+dx >= 0 && c.x+dx < n) {
					for(int y: byrow[c.x+dx]) {
						if(dist.get(new Coor(c.x+dx, y)) > c.w+1) {
							dist.put(new Coor(c.x+dx, y), c.w+1);
							q.add(new Coorw(c.x+dx, y, c.w+1));
						}
					}
				}
			}
			for(int dy = -2; dy <= 2; dy++) {
				if(c.y+dy >= 0 && c.y+dy < m) {
					for(int x: bycol[c.y+dy]) {
						if(dist.get(new Coor(x, c.y+dy)) > c.w+1) {
							dist.put(new Coor(x, c.y+dy), c.w+1);
							q.add(new Coorw(x, c.y+dy, c.w+1));
						}
					}
				}
			}
		}
		int res = maxv;
		if(dist.containsKey(new Coor(n-1, m-1))){
			res = dist.get(new Coor(n-1, m-1));
		}
		for(int y: byrow[n-1]) {
			res = Math.min(res, dist.get(new Coor(n-1, y))+1);
		}
		for(int y: byrow[n-2]) {
			res = Math.min(res, dist.get(new Coor(n-2, y))+1);
		}
		for(int x: bycol[m-1]) {
			res = Math.min(res, dist.get(new Coor(x, m-1))+1);
		}
		for(int x: bycol[m-2]) {
			res = Math.min(res, dist.get(new Coor(x, m-2))+1);
		}
		if(res == maxv) System.out.println(-1);
		else System.out.println(res);
	}
	static class Coorw implements Comparable<Coorw>{
		int x, y, w;
		public Coorw(int x, int y, int w) {
			this.x = x; this.y = y; this.w = w;
		}
		public int compareTo(Coorw c) {
			return w - c.w;
		}
		public String toString() {
			return x+" "+y+" "+w;
		}
	}
	static class Coor{
		int x, y;
		public Coor(int x, int y) {
			this.x = x; this.y = y;
		}
		@Override
		public int hashCode() {
			return x+y;
		}
		@Override
		public boolean equals(Object o) {
			Coor c = (Coor)o;
			return x == c.x && y == c.y;
		}
		public String toString() {
			return x+" "+y;
		}
	}

	static class FastScanner {
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		public FastScanner() {
			reader = new BufferedReader(new InputStreamReader(System.in), 32768);
			tokenizer = null;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch(IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

}


import java.io.*;
import java.util.*;

public class D {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int n = sc.nextInt();
		int m = sc.nextInt();
		int k = sc.nextInt();
		HashSet<Integer>[] byrow = new HashSet[n];
		for(int i = 0; i < n; i++) byrow[i] = new HashSet<>();
		HashSet<Integer>[] bycol = new HashSet[m];
		for(int i = 0; i < m; i++) bycol[i] = new HashSet<>();
		for(int i = 0; i < k; i++) {
			int x = sc.nextInt()-1;
			int y = sc.nextInt()-1;
//			Coor c = new Coor(x, y);
			byrow[x].add(y);
			bycol[y].add(x);
		}
		int maxv = 1000000000;
		HashMap<Coor, Integer> dist = new HashMap<>();
		for(int x = 0; x < n; x++) {
			for(int y: byrow[x]) {
				dist.put(new Coor(x, y), maxv);
			}
		}
		dist.put(new Coor(0, 0), 0);
		PriorityQueue<Coorw> q = new PriorityQueue<>();
		q.add(new Coorw(0, 0, 0));
		int[][] dir = {{0, 1}, {0, -1}, {1,0}, {0,1}};
		while(!q.isEmpty()) {
			Coorw c = q.poll();
			for(int[] d: dir) {
				int x = c.x + d[0], y = c.y + d[1];
				if(x >= 0 && y >= 0 && x < n && y < m && byrow[x].contains(y) && dist.get(new Coor(x, y)) > c.w+0) {
					dist.put(new Coor(x, y), c.w);
					q.add(new Coorw(x, y, c.w));
				}
			}
			for(int dx = -2; dx <= 2; dx++) {
				if(c.x+dx >= 0 && c.x+dx < n) {
					for(int y: byrow[c.x+dx]) {
						if(dist.get(new Coor(c.x+dx, y)) > c.w+1) {
							dist.put(new Coor(c.x+dx, y), c.w+1);
							q.add(new Coorw(c.x+dx, y, c.w+1));
						}
					}
				}
			}
			for(int dy = -2; dy <= 2; dy++) {
				if(c.y+dy >= 0 && c.y+dy < m) {
					for(int x: bycol[c.y+dy]) {
						if(dist.get(new Coor(x, c.y+dy)) > c.w+1) {
							dist.put(new Coor(x, c.y+dy), c.w+1);
							q.add(new Coorw(x, c.y+dy, c.w+1));
						}
					}
				}
			}
		}
		int res = maxv;
		if(dist.containsKey(new Coor(n-1, m-1))){
			res = dist.get(new Coor(n-1, m-1));
		}
		for(int y: byrow[n-1]) {
			res = Math.min(res, dist.get(new Coor(n-1, y))+1);
		}
		for(int y: byrow[n-2]) {
			res = Math.min(res, dist.get(new Coor(n-2, y))+1);
		}
		for(int x: bycol[m-1]) {
			res = Math.min(res, dist.get(new Coor(x, m-1))+1);
		}
		for(int x: bycol[m-2]) {
			res = Math.min(res, dist.get(new Coor(x, m-2))+1);
		}
		if(res == 1000000000) System.out.println(-1);
		else System.out.println(res);
	}
	static class Coorw implements Comparable<Coorw>{
		int x, y, w;
		public Coorw(int x, int y, int w) {
			this.x = x; this.y = y; this.w = w;
		}
		public int compareTo(Coorw c) {
			return w - c.w;
		}
	}
	static class Coor{
		int x, y;
		public Coor(int x, int y) {
			this.x = x; this.y = y;
		}
		@Override
		public int hashCode() {
			return x+y;
		}
		@Override
		public boolean equals(Object o) {
			Coor c = (Coor)o;
			return x == c.x && y == c.y;
		}
	}

	static class FastScanner {
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		public FastScanner() {
			reader = new BufferedReader(new InputStreamReader(System.in), 32768);
			tokenizer = null;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch(IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

}