### BUGGY CODE ###

import heapq

H, W = [int(x) for x in input().split()]
c_h, c_w = [int(x) - 1 for x in input().split()]  # Convert to 0-origin
d_h, d_w = [int(x) - 1 for x in input().split()]  # Convert to 0-origin
s = [['.' for w in range(W)] for h in range(H)]
for h in range(H):
    s[h] = input()

def in_the_map(x):
    if x[0] < 0 or H <= x[0] or x[1] < 0 or W <= x[1]:
        return False
    return True

visited = [[False for w in range(W)] for h in range(H)]
queue = []
heapq.heappush(queue, (0, (c_h, c_w)))  # Push start point with cost (number of warps) 0

while len(queue) > 0:
    # First, explore neighbours where can be reached by walk (with the same cost as p)
    while len(queue) > 0:
        cost, p = heapq.heappop(queue)

        if visited[p[0]][p[1]]:  # Skip if already visited (the priority queue assures we can visit to the point with the smallest cost)
            continue
        visited[p[0]][p[1]] = True

        if p[0] == d_h and p[1] == d_w:  # Finally reach to the goal
            print(cost)
            break
        # print(f'Visit {p}: {cost}')

        neighbours = ((p[0], p[1] - 1), (p[0], p[1] + 1), (p[0] - 1, p[1]), (p[0] + 1, p[1]))
        for n in neighbours:
            if not in_the_map(n) or visited[n[0]][n[1]]:
                continue
            if s[n[0]][n[1]] == '.':  # not wall
                heapq.heappush(queue, (cost, n))  # For later exploration

    # Then, explore points where can be reached by warp from visited points (with +1 cost from the point)
    for h0 in range(H):
        for w0 in range(W):
            if not visited[h0][w0]:
                continue
            for diff_h in range(-2, 3, 1):
                for diff_w in range(-2, 3, 1):
                    h = h0 + diff_h
                    w = w0 + diff_w
                    if not in_the_map((h, w)) or visited[h][w]:
                        continue
                    if s[h][w] == '.': # not wall
                        heapq.heappush(queue, (cost + 1, (h, w)))

if not visited[d_h][d_w]:  # Cannot reach to the goal
    print(-1)

### FIXED CODE ###

H, W = [int(x) for x in input().split()]
c_h, c_w = [int(x) - 1 for x in input().split()]  # Convert to 0-origin
d_h, d_w = [int(x) - 1 for x in input().split()]  # Convert to 0-origin
s = [['.' for w in range(W)] for h in range(H)]
for h in range(H):
    s[h] = input()

def in_the_map(x):
    if x[0] < 0 or H <= x[0] or x[1] < 0 or W <= x[1]:
        return False
    return True
 
costs = [[-1 for w in range(W)] for h in range(H)]
 
def dfs(h, w, c, q):
    if not in_the_map((h, w)):
        return
    if costs[h][w] >= 0:
        return
    if s[h][w] != '.':
        return
    q.append((h, w))
    costs[h][w] = c 
    dfs(h - 1, w, c, q)
    dfs(h + 1, w, c, q)
    dfs(h, w - 1, c, q)
    dfs(h, w + 1, c, q)

queue = [(c_h, c_w)]
cost = 0
while len(queue) > 0:
    visited = []
    for q in queue:
        dfs(q[0], q[1], cost, visited)
    
    next_queue = []
    for v in visited:
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                hh = v[0] + dh 
                ww = v[1] + dw
                if not in_the_map((hh, ww)):
                    continue
                if costs[hh][ww] >= 0:
                    continue
                if s[hh][ww] != '.':
                    continue
                next_queue.append((hh, ww))

    cost += 1
    queue = next_queue

print(costs[d_h][d_w])