package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"math"
	"os"
)

var mod = 1000000007
var inf = 1000000007

func main() {
	r := bufio.NewReader(os.Stdin)
	var H, W int
	fmt.Fscan(r, &H)
	fmt.Fscan(r, &W)

	var ch, cw int
	fmt.Fscan(r, &ch)
	fmt.Fscan(r, &cw)
	ch--
	cw--
	var dh, dw int
	fmt.Fscan(r, &dh)
	fmt.Fscan(r, &dw)
	dh--
	dw--

	matrix := make([]string, H)
	for i := 0; i < H; i++ {
		fmt.Fscan(r, &matrix[i])
	}

	// 各ポイントへのコスト
	d := make([][]int, H)
	for i := 0; i < H; i++ {
		c := make([]int, W)
		for j := 0; j < W; j++ {
			c[j] = math.MaxInt64
		}
		d[i] = c
	}
	d[ch][cw] = 0

	// 上下左右への移動
	dx := []int{1, 0, -1, 0}
	dy := []int{0, 1, 0, -1}
	// ワープ移動
	var d5y, d5x []int
	for y := -2; y <= 2; y++ {
		for x := -2; x <= 2; x++ {
			if abs(y)+abs(x) <= 1 {
				continue
			}
			d5y = append(d5y, y)
			d5x = append(d5x, x)
		}
	}

	ph := &pheap{}
	heap.Init(ph)
	heap.Push(ph, ppoint{cw, ch, 0})
	for ph.Len() > 0 {
		p := heap.Pop(ph).(ppoint)
		if p.x == dw && p.y == dh {
			break
		}

		for i := range dx {
			nx := p.x + dx[i]
			ny := p.y + dy[i]
			// 探索対象ならキューに追加
			if nx < 0 || nx >= W || ny < 0 || ny >= H || matrix[ny][nx] == '#' {
				continue
			}
			if d[ny][nx] <= p.c {
				continue
			}
			d[ny][nx] = p.c
			heap.Push(ph, ppoint{nx, ny, p.c})
		}

		nc := p.c + 1
		for i := range d5x {
			nx := p.x + d5x[i]
			ny := p.y + d5y[i]
			// 探索対象ならキューに追加
			if nx < 0 || nx >= W || ny < 0 || ny >= H || matrix[ny][nx] == '#' {
				continue
			}
			if d[ny][nx] <= nc {
				continue
			}
			d[ny][nx] = nc
			heap.Push(ph, ppoint{nx, ny, nc})
		}
	}

	// ゴールポイントのコストを出力
	if d[dh][dw] == math.MaxInt64 {
		fmt.Println(-1)
	} else {
		fmt.Println(d[dh][dw])
	}

}

type ppoint struct {
	x int
	y int
	c int
}

// priority_queue
type pheap []ppoint

func (h pheap) Len() int           { return len(h) }
func (h pheap) Less(i, j int) bool { return h[i].c < h[j].c }
func (h pheap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
func (h *pheap) Push(x interface{}) {
	*h = append(*h, x.(ppoint))
}
func (h *pheap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

// Queue ...
type Queue []point

// pop ...
func (q *Queue) empty() bool {
	return len(*q) == 0
}

// push ...
func (q *Queue) push(i point) {
	*q = append(*q, i)
}

// pop ...
func (q *Queue) pop() (point, bool) {
	if q.empty() {
		return point{}, false
	} else {
		res := (*q)[0]
		*q = (*q)[1:]
		return res, true
	}
}

// permutations
func permutations(arr []int) [][]int {
	var helper func([]int, int)
	res := [][]int{}

	helper = func(arr []int, n int) {
		if n == 1 {
			tmp := make([]int, len(arr))
			copy(tmp, arr)
			res = append(res, tmp)
		} else {
			for i := 0; i < n; i++ {
				helper(arr, n-1)
				if n%2 == 1 {
					tmp := arr[i]
					arr[i] = arr[n-1]
					arr[n-1] = tmp
				} else {
					tmp := arr[0]
					arr[0] = arr[n-1]
					arr[n-1] = tmp
				}
			}
		}
	}
	helper(arr, len(arr))
	return res
}

// Union-Find
type unionFind struct {
	d []int
}

func newUnionFind(n int) *unionFind {
	uf := new(unionFind)
	uf.d = make([]int, n)
	for i := 0; i < n; i++ {
		uf.d[i] = -1
	}
	return uf
}

func (uf *unionFind) find(x int) int {
	if uf.d[x] < 0 {
		return x
	}
	uf.d[x] = uf.find(uf.d[x])
	return uf.d[x]
}

func (uf *unionFind) unite(x, y int) bool {
	rootX := uf.find(x)
	rootY := uf.find(y)
	if rootX == rootY {
		return false
	}

	if uf.d[rootX] < uf.d[rootY] {
		uf.d[rootX] += uf.d[rootY]
		uf.d[rootY] = rootX
	} else {
		uf.d[rootY] += uf.d[rootX]
		uf.d[rootX] = rootY
	}

	return true
}

func (uf *unionFind) same(x, y int) bool {
	return uf.find(x) == uf.find(y)
}

func (uf *unionFind) size(x int) int {
	return -uf.d[uf.find(x)]
}

// mod
func modpow(a, n, mod int) int {
	res := 1
	for n > 0 {
		if n%2 != 0 {
			res = res * a % mod
		}
		a = a * a % mod
		n = n / 2
	}
	return res
}

func modinv(n, mod int) int {
	return modpow(n, mod-2, mod)
}

func modcomb(n, a, mod int) int {
	x := 1
	y := 1
	for i := 0; i < a; i++ {
		x = x * (n - i)
		x %= mod
		y = y * (i + 1)
		y %= mod
	}
	return x * modpow(y, mod-2, mod) % mod
}

func modfactorial(n, mod int) int {
	result := 1
	for i := 1; i <= n; i++ {
		result = (result * i) % mod
	}
	return result
}

// Combination ...
type Combination struct {
	facts, invs []int
	mod         int
}

// NewCombination ...
func NewCombination(n, mod int) Combination {
	return Combination{
		facts: make([]int, n+1),
		invs:  make([]int, n+1),
		mod:   mod,
	}
}

func (cmb *Combination) calc(n, k int) int {
	ret := cmb.facts[n] * cmb.invs[k]
	ret %= cmb.mod
	ret = ret * cmb.invs[n-k]
	ret %= cmb.mod
	return ret
}

func (cmb *Combination) init(n int) {
	cmb.facts[0] = 1
	// 階乗を算出
	for i := 1; i <= n; i++ {
		cmb.facts[i] = cmb.facts[i-1] * i % cmb.mod
	}
	// 逆元を算出
	cmb.invs[n] = modinv(cmb.facts[n], cmb.mod)
	for i := n - 1; i >= 0; i-- {
		cmb.invs[i] = cmb.invs[i+1] * (i + 1) % cmb.mod
	}
}

// Utility
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func primeFactor(x int) map[int]int {
	res := make(map[int]int)
	for i := 2; i*i <= x; i++ {
		for x%i == 0 {
			res[i]++
			x = x / i
		}
	}
	if x != 1 {
		res[x] = 1
	}
	return res
}

func divisor(x int) []int {
	res := make([]int, 0)
	for i := 1; i*i <= x; i++ {
		if x%i == 0 {
			res = append(res, i)
			if i != x/i {
				res = append(res, x/i)
			}
		}
	}
	return res
}

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func lcm(x, y int) int {
	return x / gcd(x, y) * y
}

func reverseString(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

func pow(a, n int) int {
	ret := 1
	for i := 1; i <= n; i++ {
		ret *= a
	}
	return ret
}

// Stack ...
type Stack []int

// pop ...
func (s *Stack) empty() bool {
	return len(*s) == 0
}

// push ...
func (s *Stack) push(i int) {
	*s = append(*s, i)
}

// pop ...
func (s *Stack) pop() (int, bool) {
	if s.empty() {
		return 0, false
	} else {
		index := len(*s) - 1
		res := (*s)[index]
		*s = (*s)[:index]
		return res, true
	}
}

// sortable points
type point struct {
	x int
	y int
}

type points []point

func (p points) Len() int {
	return len(p)
}

func (p points) Less(i, j int) bool {
	return p[i].x < p[j].x
}

func (p points) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}


Measured memory_bytes: 25840