Buggy python submission (verdict: Time Limit Exceeded):
from pprint import pprint
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
maze=[input() for _ in range(h)]

from collections import deque

dist=[[-1]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0 #ワープ回数
while q:
    while q:
        x,y=q.popleft()
        if dist[x][y]>cur:
            continue
        dist[x][y]=cur
        for i in range(-1,2):
            for j in range(-1,2):
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if dist[xx][yy]==-1:
                            if maze[xx][yy]==".":
                                dist[xx][yy]=cur
                                q.append((xx,yy))
                        if dist[xx][yy]>cur:
                            dist[xx][yy]=cur
                            q.append((xx,yy))
                                
        for i in range(-2,3):
            for j in range(-2,3):
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if dist[xx][yy]==-1:
                            if maze[xx][yy]==".":
                                p.append((xx,yy))
    
    if len(q)==0:
        # print(p,q)
        for pp in p:
            if dist[pp[0]][pp[1]]==-1:
                q.append(pp)
        p=[]
        cur+=1
# pprint(dist)
print(dist[dh][dw])
  
    


Fixed submission:
from pprint import pprint
from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
INF=float("inf")
maze=[input() for _ in range(h)]
dist=[[INF]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0
# pprint(dist)
# exit()
while q:
    while q:
        x,y=q.popleft()
        # (x,y)は到達訪問済み
        d=dist[x][y]
        for i in range(-2,3):
            for j in range(-2,3):
                # absの和が1、上下左右
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    # 範囲内の時
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        # 道のとき
                        if maze[xx][yy]==".":
                            # 未到達の時
                            if dist[xx][yy]>d:
                                # 移動前のdist[x][y]と同じコスト
                                dist[xx][yy]=d
                                q.append((xx,yy))
                # ワープ 
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if maze[xx][yy]==".":
                            if dist[xx][yy]>d+1:
                                dist[xx][yy]=d+1
                                p.append((xx,yy))
                                
   
    cur+=1
    q=deque(p)
    p=[]
            

# pprint(dist)
ans=dist[dh][dw]
if ans==INF:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from pprint import pprint
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
maze=[input() for _ in range(h)]

from collections import deque

dist=[[-1]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0 #ワープ回数
while q:
    # print("now cur:",cur)
    # print(q)
    while q:
        x,y=q.popleft()
        dist[x][y]=cur
        for i in range(-1,2):
            for j in range(-1,2):
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if dist[xx][yy]==-1:
                            if maze[xx][yy]==".":
                                dist[xx][yy]=cur
                                q.append((xx,yy))
                        if dist[xx][yy]>cur:
                            dist[xx][yy]=cur
                            q.append((xx,yy))
                                
        for i in range(-2,3):
            for j in range(-2,3):
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if dist[xx][yy]==-1:
                            if maze[xx][yy]==".":
                                p.append((xx,yy))
    
    if len(q)==0:
        # print(p,q)
        for pp in p:
            if dist[pp[0]][pp[1]]==-1:
                q.append(pp)
        p=[]
        cur+=1
# pprint(dist)
print(dist[dh][dw])
  
    


Fixed submission:
from pprint import pprint
from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
INF=float("inf")
maze=[input() for _ in range(h)]
dist=[[INF]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0
# pprint(dist)
# exit()
while q:
    while q:
        x,y=q.popleft()
        # (x,y)は到達訪問済み
        d=dist[x][y]
        for i in range(-2,3):
            for j in range(-2,3):
                # absの和が1、上下左右
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    # 範囲内の時
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        # 道のとき
                        if maze[xx][yy]==".":
                            # 未到達の時
                            if dist[xx][yy]>d:
                                # 移動前のdist[x][y]と同じコスト
                                dist[xx][yy]=d
                                q.append((xx,yy))
                # ワープ 
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if maze[xx][yy]==".":
                            if dist[xx][yy]>d+1:
                                dist[xx][yy]=d+1
                                p.append((xx,yy))
                                
   
    cur+=1
    q=deque(p)
    p=[]
            

# pprint(dist)
ans=dist[dh][dw]
if ans==INF:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!
from pprint import pprint
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
s=[input() for _ in range(h)]

from collections import deque
visited=[[0]*w for _ in range(h)]
q=deque()
p=deque()
q.append((ch,cw))
cur=0
while q:
    while q:
        # print("q",q)
        # print("p",p)
        x,y=q.popleft()
        visited[x][y]=1

        for i,j in [(-1,0),(1,0),(0,1),(0,-1)]:
            xx=x+i
            yy=y+j
            if xx<0 or h<=xx:
                continue
            if yy<0 or w<=yy:
                continue
            
            if visited[xx][yy]==0 and s[xx][yy]==".":
                visited[xx][yy]=1
                q.append((xx,yy))

        for i in range(-2,3):
            for j in range(-2,3):
                if abs(i)+abs(j)<=1:
                    continue
                xx=x+i
                yy=y+j
                if xx<0 or h<=xx:
                    continue
                if yy<0 or w<=yy:
                    continue
                
                if visited[xx][yy]==0 and s[xx][yy]==".":
                    p.append((xx,yy))

    # print(q,p,visited)
    if visited[dh][dw]:
        print(cur)
        exit()
    
    cur+=1
    q=p
    p=deque()
print(-1)
  
    

Fixed submission:
from pprint import pprint
from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
INF=float("inf")
maze=[input() for _ in range(h)]
dist=[[INF]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0
# pprint(dist)
# exit()
while q:
    while q:
        x,y=q.popleft()
        # (x,y)は到達訪問済み
        d=dist[x][y]
        for i in range(-2,3):
            for j in range(-2,3):
                # absの和が1、上下左右
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    # 範囲内の時
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        # 道のとき
                        if maze[xx][yy]==".":
                            # 未到達の時
                            if dist[xx][yy]>d:
                                # 移動前のdist[x][y]と同じコスト
                                dist[xx][yy]=d
                                q.append((xx,yy))
                # ワープ 
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if maze[xx][yy]==".":
                            if dist[xx][yy]>d+1:
                                dist[xx][yy]=d+1
                                p.append((xx,yy))
                                
   
    cur+=1
    q=deque(p)
    p=[]
            

# pprint(dist)
ans=dist[dh][dw]
if ans==INF:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import deque
def LI():
    return list(map(int,input().split()))

h,w=LI()
ch,cw=LI()
# ch-=1
# cw-=1
dh,dw=LI()
# dh-=1
# dw-=1

maze=[list("#"*(w+2))]+[list("#"+input()+"#") for i in range(h)]+[list("#"*(w+2))]
# S=[input() for _ in range(h)]

visited=[[False]*(w+2) for _ in range(h+2)]

q=deque()
q.append((ch*10**4+cw))
p=deque()
while q:
    num=q.popleft()
    x,y=num//10**4,num%10**4
    visited[x][y]=True
    for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
        xn,yn=x+i,y+j
        if maze[xn][yn]=="." and visited[xn][yn]==False:
            visited[xn][yn]=True
            q.append((xn*10**4+yn))
    for i in range(-2,3,1):
        for j in range(-2,3,1):
            xm,ym=x+i,y+j
            try:
                if visited[xm][ym]==False:
                    if maze[xm][ym]==".":
                        p.append((xm*10**4+ym))
            except:pass
if visited[dh][dw]==True:
    print(0)
    exit()

for pp in p:
    x,y=pp//10**4,pp%10**4
    if visited[x][y]==False:
        q.append(pp)
if len(q)==0:
    print(-1)
    exit()
for ttt in range(1,100000):
    while q:
        num=q.popleft()
    	x,y=num//10**4,num%10**4
        visited[x][y]=True
        for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
            xn,yn=x+i,y+j
            if maze[xn][yn]=="." and visited[xn][yn]==False:
                visited[xn][yn]=True
                q.append((xn*10**4+yn))
        for i in range(-2,3,1):
            for j in range(-2,3,1):
                xm,ym=x+i,y+j
                try:
                    if visited[xm][ym]==False:
                        if maze[xm][ym]==".":
                            p.append((xm*10**4+ym))
                except:pass
    
    if visited[dh][dw]==True:
        print(ttt)
        exit()
    for pp in p:
    x,y=pp//10**4,pp%10**4
    if visited[x][y]==False:
        q.append(pp)
    if len(q)==0:
        print(-1)
        exit()
    


Fixed submission:
from pprint import pprint
from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
INF=float("inf")
maze=[input() for _ in range(h)]
dist=[[INF]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0
# pprint(dist)
# exit()
while q:
    while q:
        x,y=q.popleft()
        # (x,y)は到達訪問済み
        d=dist[x][y]
        for i in range(-2,3):
            for j in range(-2,3):
                # absの和が1、上下左右
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    # 範囲内の時
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        # 道のとき
                        if maze[xx][yy]==".":
                            # 未到達の時
                            if dist[xx][yy]>d:
                                # 移動前のdist[x][y]と同じコスト
                                dist[xx][yy]=d
                                q.append((xx,yy))
                # ワープ 
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if maze[xx][yy]==".":
                            if dist[xx][yy]>d+1:
                                dist[xx][yy]=d+1
                                p.append((xx,yy))
                                
   
    cur+=1
    q=deque(p)
    p=[]
            

# pprint(dist)
ans=dist[dh][dw]
if ans==INF:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from pprint import pprint
from collections import deque
def LI():return list(map(int,input().split()))
INF=float("inf")

h,w=LI()
ch,cw=LI()
dh,dw=LI()
maze=[list("#"*(w+2))]+[list("#"+input()+"#") for i in range(h)]+[list("#"*(w+2))]

visited=[[False]*(w+2) for _ in range(h+2)]

q=deque()
q.append((ch,cw))
p=deque()
while q:
    x,y=q.popleft()
    visited[x][y]=True
    for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
        xn,yn=x+i,y+j
        if visited[xn][yn]:
          continue
        if maze[xn][yn]==".":
            visited[xn][yn]=True
            q.append((xn,yn))
    for i,j in ([-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],[-1,-2],[-1,-1],[-1,1],[-1,2],[0,-2],[0,2],[1,-2],[1,-1],[1,1],[1,2],[2,-2],[2,-1],[2,0],[2,1],[2,2]):
      xm,ym=x+i,y+j
      try:
        if visited[xm][ym]:
          continue
        if maze[xm][ym]=="#":
          continue
        p.append((xm,ym))
      except:pass
if visited[dh][dw]:
    print(0)
    exit()

for (px,py) in p:
    if visited[px][py]==False:
        q.append((px,py))
if len(q)==0:
    print(-1)
    exit()
for ttt in range(1,100000):
    while q:
        x,y=q.popleft()
        visited[x][y]=True
        if visited[dh][dw]:
            print(ttt)
            exit()
        for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
            xn,yn=x+i,y+j
            if visited[xn][yn]:
              continue
            if maze[xn][yn]==".":
                visited[xn][yn]=True
                q.append((xn,yn))
        for i,j in ([-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],[-1,-2],[-1,-1],[-1,1],[-1,2],[0,-2],[0,2],[1,-2],[1,-1],[1,1],[1,2],[2,-2],[2,-1],[2,0],[2,1],[2,2]):
          xm,ym=x+i,y+j
          try:
            if visited[xm][ym]:
              continue
            if maze[xm][ym]=="#":
              continue
            p.append((xm,ym))
          except:pass
    
    if visited[dh][dw]==True:
        print(ttt)
        exit()
        
    for (px,py) in p:
        if visited[px][py]==False:
            q.append((px,py))
    if len(q)==0:
        print(-1)
        exit()
    

Fixed submission:
from pprint import pprint
from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
INF=float("inf")
maze=[input() for _ in range(h)]
dist=[[INF]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0
# pprint(dist)
# exit()
while q:
    while q:
        x,y=q.popleft()
        # (x,y)は到達訪問済み
        d=dist[x][y]
        for i in range(-2,3):
            for j in range(-2,3):
                # absの和が1、上下左右
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    # 範囲内の時
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        # 道のとき
                        if maze[xx][yy]==".":
                            # 未到達の時
                            if dist[xx][yy]>d:
                                # 移動前のdist[x][y]と同じコスト
                                dist[xx][yy]=d
                                q.append((xx,yy))
                # ワープ 
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if maze[xx][yy]==".":
                            if dist[xx][yy]>d+1:
                                dist[xx][yy]=d+1
                                p.append((xx,yy))
                                
   
    cur+=1
    q=deque(p)
    p=[]
            

# pprint(dist)
ans=dist[dh][dw]
if ans==INF:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from pprint import pprint
from collections import deque
def LI():
    return list(map(int,input().split()))
INF=float("inf")

h,w=LI()
ch,cw=LI()
# ch-=1
# cw-=1
dh,dw=LI()
# dh-=1
# dw-=1

maze=[list("#"*(w+2))]+[list("#"+input()+"#") for i in range(h)]+[list("#"*(w+2))]
# S=[input() for _ in range(h)]

visited=[[False]*(w+2) for _ in range(h+2)]

q=deque()
q.append((ch,cw))
p=deque()
while q:
    x,y=q.popleft()
    visited[x][y]=True
    for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
        xn,yn=x+i,y+j
        if maze[xn][yn]=="." and visited[xn][yn]==False:
            visited[xn][yn]=True
            q.append((xn,yn))
    for i in range(-2,3,1):
        for j in range(-2,3,1):
            xm,ym=x+i,y+j
            try:
                if visited[xm][ym]==False:
                    if maze[xm][ym]==".":
                        p.append((xm,ym))
            except:pass
if visited[dh][dw]==True:
    print(0)
    exit()

for pp in p:
    if visited[pp[0]][pp[1]]==False:
        q.append(pp)
if len(q)==0:
    print(-1)
    exit()
for ttt in range(1,100000):
    while q:
        x,y=q.popleft()
        visited[x][y]=True
        for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
            xn,yn=x+i,y+j
            if maze[xn][yn]=="." and visited[xn][yn]==False:
                visited[xn][yn]=True
                q.append((xn,yn))
        for i in range(-2,3,1):
            for j in range(-2,3,1):
                xm,ym=x+i,y+j
                try:
                    if visited[xm][ym]==False:
                        if maze[xm][ym]==".":
                            p.append((xm,ym))
                except:pass
    
    if visited[dh][dw]==True:
        print(ttt)
        exit()
        
    for pp in p:
        if visited[pp[0]][pp[1]]==False:
            q.append(pp)
    if len(q)==0:
        print(-1)
        exit()
    


Fixed submission:
from pprint import pprint
from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
INF=float("inf")
maze=[input() for _ in range(h)]
dist=[[INF]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0
# pprint(dist)
# exit()
while q:
    while q:
        x,y=q.popleft()
        # (x,y)は到達訪問済み
        d=dist[x][y]
        for i in range(-2,3):
            for j in range(-2,3):
                # absの和が1、上下左右
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    # 範囲内の時
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        # 道のとき
                        if maze[xx][yy]==".":
                            # 未到達の時
                            if dist[xx][yy]>d:
                                # 移動前のdist[x][y]と同じコスト
                                dist[xx][yy]=d
                                q.append((xx,yy))
                # ワープ 
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if maze[xx][yy]==".":
                            if dist[xx][yy]>d+1:
                                dist[xx][yy]=d+1
                                p.append((xx,yy))
                                
   
    cur+=1
    q=deque(p)
    p=[]
            

# pprint(dist)
ans=dist[dh][dw]
if ans==INF:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from pprint import pprint
from collections import deque
def LI():
    return list(map(int,input().split()))
INF=float("inf")

h,w=LI()
ch,cw=LI()
# ch-=1
# cw-=1
dh,dw=LI()
# dh-=1
# dw-=1

maze=[list("#"*(w+2))]+[list("#"+input()+"#") for i in range(h)]+[list("#"*(w+2))]
# S=[input() for _ in range(h)]

visited=[[False]*(w+2) for _ in range(h+2)]

q=deque()
q.append((ch,cw))
p=deque()
while q:
    x,y=q.popleft()
    visited[x][y]=True
    for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
        xn,yn=x+i,y+j
        if maze[xn][yn]=="." and visited[xn][yn]==False:
            visited[xn][yn]=True
            q.append((xn,yn))
    for i in range(-2,3,1):
        for j in range(-2,3,1):
            xm,ym=x+i,y+j
            try:
                if visited[xm][ym]==False:
                    if maze[xm][ym]==".":
                        p.append((xm,ym))
            except:pass
if visited[dh][dw]==True:
    print(0)
    exit()

for pp in p:
    if visited[pp[0]][pp[1]]==False:
        q.append(pp)
if len(q)==0:
    print(-1)
    exit()
for ttt in range(1,100000):
    while q:
        x,y=q.popleft()
        visited[x][y]=True
        for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
            xn,yn=x+i,y+j
            if maze[xn][yn]=="." and visited[xn][yn]==False:
                visited[xn][yn]=True
                q.append((xn,yn))
        for i in range(-2,3,1):
            for j in range(-2,3,1):
                xm,ym=x+i,y+j
                try:
                    if visited[xm][ym]==False:
                        if maze[xm][ym]==".":
                            p.append((xm,ym))
                except:pass
    
    if visited[dh][dw]==True:
        print(ttt)
        exit()
        
    for pp in p:
        if visited[pp[0]][pp[1]]==False:
            q.append(pp)
    if len(q)==0:
        print(-1)
        exit()
    

Fixed submission:
from pprint import pprint
from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
INF=float("inf")
maze=[input() for _ in range(h)]
dist=[[INF]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0
# pprint(dist)
# exit()
while q:
    while q:
        x,y=q.popleft()
        # (x,y)は到達訪問済み
        d=dist[x][y]
        for i in range(-2,3):
            for j in range(-2,3):
                # absの和が1、上下左右
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    # 範囲内の時
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        # 道のとき
                        if maze[xx][yy]==".":
                            # 未到達の時
                            if dist[xx][yy]>d:
                                # 移動前のdist[x][y]と同じコスト
                                dist[xx][yy]=d
                                q.append((xx,yy))
                # ワープ 
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if maze[xx][yy]==".":
                            if dist[xx][yy]>d+1:
                                dist[xx][yy]=d+1
                                p.append((xx,yy))
                                
   
    cur+=1
    q=deque(p)
    p=[]
            

# pprint(dist)
ans=dist[dh][dw]
if ans==INF:
    print(-1)
else:
    print(ans)