R,C=map(int,input().split()) sx,sy=map(int,input().split()) gx,gy=map(int,input().split()) sx-=1 sy-=1 gx-=1 gy-=1 MAP=[input() for i in range(R)] ANS=[[1<<31]*(C) for i in range(R)] from collections import deque Q=deque() Q.append((sx,sy)) ANS[sx][sy]=0 while Q: x,y=Q.popleft() for w,z in [[x+1,y],[x-1,y],[x,y+1],[x,y-1]]: if 0<=wANS[x][y]+1: ANS[w][z]=ANS[x][y]+1 Q.append((w,z)) print(ANS[gx][gy])