### BUGGY CODE ###

import sys
import collections
import itertools


def resolve(in_):
    H, W = map(int, next(in_).split())
    Ch, Cw = map(int, next(in_).split())
    Dh, Dw = map(int, next(in_).split())

    ch = Ch - 1
    cw = Cw - 1
    dh = Dh - 1
    dw = Dw - 1
    S = tuple(s.strip() for s in in_)
    wall_i = ord(b'#')
    # road_i = ord(b'.')
    used = [[False for h in range(W)] for w in range(H)]

    qu = collections.deque()
    qu.append((ch, cw))
    for ans in itertools.count():
        if not qu:
            return -1
        
        roads = []
        # walk
        while qu:
            h, w = qu.popleft()
            if used[h][w]:
                continue
            
            used[h][w] = True
            roads.append((h, w))
            if dh == h and dw == w:
                return ans

            for h0, w0 in ((h - 1, w), (h + 1, w), (h, w - 1), (h, w + 1)):
                if not (0 <= h0 < H and 0 <= w0 < W):
                    continue
                if S[h0][w0] == wall_i:
                    continue
                qu.append((h0, w0))
        # magic
        for h1, w1 in roads:
            for h2 in range(h1 - 2, h1 + 3):
                for w2 in range(w1 - 2, w1 + 3):
                    if not (0 <= h2 < H and 0 <= w2 < W):
                        continue
                    if used[h2][w2]:
                        continue
                    if S[h2][w2] == wall_i:
                        continue
                    qu.append((h2, w2))

    return ans

def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
import collections
import itertools


def resolve(in_):
    H, W = map(int, next(in_).split())
    Ch, Cw = map(int, next(in_).split())
    Dh, Dw = map(int, next(in_).split())

    ch = Ch - 1
    cw = Cw - 1
    dh = Dh - 1
    dw = Dw - 1
    S = tuple(s.strip() for s in in_)
    wall_i = ord(b'#')
    # road_i = ord(b'.')
    used = [[False for h in range(W)] for w in range(H)]

    qu = collections.deque()
    qu.append((ch, cw))
    for ans in itertools.count():
        if not qu:
            return -1
        
        roads = []
        # walk
        while qu:
            h, w = qu.popleft()
            if used[h][w]:
                continue
            
            used[h][w] = True
            roads.append((h, w))
            if dh == h and dw == w:
                return ans

            for h0, w0 in ((h - 1, w), (h + 1, w), (h, w - 1), (h, w + 1)):
                if not (0 <= h0 < H and 0 <= w0 < W):
                    continue
                if S[h0][w0] == wall_i:
                    continue
                qu.append((h0, w0))
        # magic
        for h1, w1 in roads:
            for h2 in range(h1 - 2, h1 + 3):
                for w2 in range(w1 - 2, w1 + 3):
                    if not (0 <= h2 < H and 0 <= w2 < W):
                        continue
                    if used[h2][w2]:
                        continue
                    if S[h2][w2] == wall_i:
                        continue
                    qu.append((h2, w2))

    return ans

def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()