import heapq import sys input=sys.stdin.readline ds=[[0,1],[1,1],[-1,1],[1,0],[-1,0],[0,-1],[1,-1],[-1,-1]] def daiku(): hq=[] for i in range(H-2): if A[i][0]!=-1: heapq.heappush(hq,[A[i][0],(i,0)]) high=[-1 for _ in range(W*H)] while len(hq)!=0: h,p=heapq.heappop(hq) if high[p[0]*W+p[1]]==-1: high[p[0]*W+p[1]]=h for dx,dy in ds: x,y=p x+=dx y+=dy if 0<=x=10**12: print(-1) else: print(ans)