package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	h := getNextInt(scanner)
	w := getNextInt(scanner)
	ch := getNextInt(scanner) - 1
	cw := getNextInt(scanner) - 1
	dh := getNextInt(scanner) - 1
	dw := getNextInt(scanner) - 1
	ss := make([]string, h)
	for i := 0; i < h; i++ {
		ss[i] = getNextString(scanner)
	}
	g := makeGrid(h, w)
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			g[i][j] = math.MaxInt32
		}
	}
	d := newDeque(int(1e6))
	d.pushFront([3]int{ch, cw, 0})
	dy := [4]int{-1, 0, 0, 1}
	dx := [4]int{0, -1, 1, 0}
	for d.r != d.l {
		p := d.list[d.l]
		d.popFront()
		if g[p[0]][p[1]] <= p[2] {
			continue
		}
		g[p[0]][p[1]] = p[2]
		for i := 0; i < 4; i++ {
			yy := p[0] + dy[i]
			xx := p[1] + dx[i]
			if yy < 0 || yy >= h {
				continue
			}
			if xx < 0 || xx >= w {
				continue
			}
			if ss[yy][xx] == '#' {
				continue
			}
			d.pushFront([3]int{yy, xx, p[2]})
		}
		for i := 0; i < 5; i++ {
			for j := 0; j < 5; j++ {
				if i == 2 && j == 2 {
					continue
				}
				yy := p[0] + i - 2
				xx := p[1] + j - 2
				if yy < 0 || yy >= h {
					continue
				}
				if xx < 0 || xx >= w {
					continue
				}
				if ss[yy][xx] == '#' {
					continue
				}
				d.pushBack([3]int{yy, xx, p[2] + 1})
			}
		}
	}
	if g[dh][dw] == math.MaxInt32 {
		fmt.Fprintln(writer, -1)
		return
	}
	fmt.Fprintln(writer, g[dh][dw])
}
func makeGrid(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

type deque struct {
	list       [][3]int
	l, r, size int
}

func newDeque(size int) deque {
	return deque{
		list: make([][3]int, size),
		l:    0,
		r:    0,
		size: size,
	}
}
func (deq *deque) index(i int) int {
	if i < 0 {
		i += deq.size
	}
	if i >= deq.size {
		i -= deq.size
	}
	return i
}
func (deq *deque) at(i int) [3]int {
	return deq.list[deq.index(deq.l+i)]
}
func (deq *deque) pushBack(x [3]int) {
	deq.list[deq.r] = x
	deq.r = deq.index(deq.r + 1)
}
func (deq *deque) pushFront(x [3]int) {
	deq.l = deq.index(deq.l - 1)
	deq.list[deq.l] = x
}
func (deq *deque) popBack() {
	deq.r = deq.index(deq.r - 1)
}
func (deq *deque) popFront() {
	deq.l = deq.index(deq.l + 1)
}


Measured memory_bytes: 36236