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找出所有路径,输出最小值
# 迷宫(91%)
n = int(input().strip())
a = []
label = -1
for i in range(n):
a.append(list(input().strip()))
visited = [[0] * n for i in range(n)]
dirs = [(-1,0),(1,0),(0,-1),(0,1)]
for i in range(n):
for j in range(n):
if a[i][j] == 'S':
visited[i][j] = 1
queue = [(i,j,0)]
while queue:
tmp = queue.pop(0)
for t in dirs:
i_n = (tmp[0] + t[0])%n
j_n = (tmp[1] + t[1])%n
if visited[i_n][j_n] == 0 and a[i_n][j_n] != '#':
visited[i_n][j_n] = 1
queue.append((i_n,j_n,tmp[2]+1))
if a[i_n][j_n]=='E':
label = tmp[2]+1
break
if label!=-1:
break
print(label)
# 字符串(100%)
s = list(input().strip())
stack = []
se = set([')',"<"])
for i in range(len(s)):
if s[i] not in se:
stack.append(s[i])
elif s[i]==')':
tmp = stack.pop(-1)
while tmp != "(":
tmp = stack.pop(-1)
else:
if not stack:
continue
if stack[-1]=='(':
continue
else:
stack.pop(-1)
print(''.join(stack))
# 宝物,O(n2), 超时,9%?直接print(3) 18%???
from operator import itemgetter
n = int(input().strip())
a = []
for i in range(n):
a.append(list(map(int, input().strip().split())))
a.sort(key=itemgetter(0))
dp = [1]*n
max_s = 1
for i in range(len(a)):
maxm = 1
for j in range(i):
if a[i][1] >= a[j][1]:
tmp = dp[j] + 1
else:
tmp = dp[j]
if tmp > maxm:
maxm = tmp
dp[i] = maxm
if dp[i] > max_s:
max_s = dp[i]
print(max_s)
# ()<问题
s = raw_input().strip()
res = ''
i = 0
left, right = 0, 0
while i < len(s):
if s[i] == '(':
left += 1
i += 1
while left > right:
if s[i] == '(':
left += 1
elif s[i] == ')':
right += 1
i += 1
elif s[i] == '<':
if res != '':
res = res[:1]
i += 1
else:
res += s[i]
print(res)
# 宝物买卖, 不确定对不对
N = int(raw_input())
li = []
for _ in range(N):
li.append([int(i) for i in raw_input().split()])
def helper(l1, l2):
m1, m2 = max(l1), max(l2)
if m1 < m2:
return -1
elif m1 > m2:
return 1
m1, m2 = min(l1), min(l2)
if m1 > m2:
return 1
elif m1 == m2:
return 0
return -1
li.sort(cmp=helper)
res = 1
for i in range(1, len(li)):
if li[i][0] >= li[i-1][0] and li[i][1] >= li[i-1][1]:
res += 1
print(res)