import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static ArrayList<Integer> result = new ArrayList<>();
public static void solution(int n, int m, int k, int[][] tone){
int[][] qipan = new int[n][m];
for(int i=0; i<k; i++)
qipan[tone[i][0]][tone[i][1]] = 1;
dfs(qipan, new boolean[n][m], 0, 0, 0, n, m);
int min = Integer.MAX_VALUE;
for(int i=0; i<result.size(); i++)
min = Math.min(min, result.get(i));
if(min == Integer.MAX_VALUE)
System.out.println(0);
else
System.out.println(min);
}
public static void dfs(int[][] dp, boolean[][] mark, int i, int j, int path, int n, int m){
if(i == n-1 && j == m-1){
result.add(path);
return;
}
if(i<0 || i>=n || j<0 || j>=m || dp[i][j] == 1 || mark[i][j] == true)
return;
mark[i][j] = true;
dfs(dp, mark, i+1, j, path+1, n, m);
dfs(dp, mark, i-1, j, path+1, n, m);
dfs(dp, mark, i, j+1, path+1, n, m);
dfs(dp, mark, i, j-1, path+1, n, m);
mark[i][j] = false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int k = scanner.nextInt();
int[][] tone = new int[k][2];
for(int i=0; i<k; i++)
for(int j=0; j<2; j++)
tone[i][j] = scanner.nextInt();
solution(n, m, k, tone);
}
}
棋盘最短路径。dfs找出所有路径,输出最小值