# 로봇 - BOJ
from collections import deque
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
M, N = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(M)]
visit = [[[0] * 4 for _ in range(N)] for _ in range(M)]
s1, s2, s3 = map(int, input().split())
e1, e2, e3 = map(int, input().split())
q = deque()
q.append([s1 - 1, s2 - 1, s3 - 1])
while q:
x, y, w = q.popleft()
if x == e1 - 1 and y == e2 - 1 and w == e3 - 1:
print(visit[x][y][w])
break
for i in range(1, 4):
nx = x + dx[w] * i
ny = y + dy[w] * i
if nx < 0 or ny < 0 or nx >= M or ny >= N:
break
if a[nx][ny]:
break
if not visit[nx][ny][w]:
q.append([nx, ny, w])
visit[nx][ny][w] = visit[x][y][w] + 1
if w == 0 or w == 1:
w1, w2 = 2, 3
if w == 2 or w == 3:
w1, w2 = 0, 1
if not visit[x][y][w1]:
visit[x][y][w1] = visit[x][y][w] + 1
q.append([x, y, w1])
if not visit[x][y][w2]:
visit[x][y][w2] = visit[x][y][w] + 1
q.append([x, y, w2])