package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
)

var DEBUG = true

func main() {
	defer Flush()
	H := readi()
	W := readi()
	Ch := readi() - 1
	Cw := readi() - 1
	Dh := readi() - 1
	Dw := readi() - 1
	S := make([][]byte, H)
	d := make([][]int, H)
	for i := range S {
		S[i] = readb()
		d[i] = make([]int, W)
		for j := range d[i] {
			d[i][j] = INF
		}
	}

	q := NewDeque()
	push := func(y, x, c int) {
		v := [3]int{y, x, c}
		if q.Len() == 0 {
			q.PushFront(v)
			return
		}
		f := q.Front().([3]int)
		if f[2] == c {
			q.PushFront(v)
		} else {
			q.PushBack(v)
		}
	}
	pop := func() (int, int, int) {
		f := q.PopFront().([3]int)
		return f[0], f[1], f[2]
	}

	push(Ch, Cw, 0)
	d[Ch][Cw] = 0

	dy := []int{-1, 0, 1, 0}
	dx := []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
			}
			//dbg("adding", y, x)
			d5y = append(d5y, y)
			d5x = append(d5x, x)
		}
	}

	update := func(y, x, c int) {
		if c < d[y][x] {
			d[y][x] = c
		}
	}

	for 0 < q.Len() {
		//for i := range d {
		//	dbg(d[i])
		//}
		//dbg("====")

		y, x, c := pop()
		//if y == Dh && x == Dw {
		//	break
		//}
		for k := range dy {
			ny := y + dy[k]
			nx := x + dx[k]
			if ny < 0 || H <= ny || nx < 0 || W <= nx || S[ny][nx] == '#' {
				continue
			}
			if d[ny][nx] <= c {
				continue
			}
			update(ny, nx, c)
			push(ny, nx, c)
		}
		nd := c + 1
		for k := range d5y {
			ny := y + d5y[k]
			nx := x + d5x[k]
			if ny < 0 || H <= ny || nx < 0 || W <= nx || S[ny][nx] == '#' {
				continue
			}
			if d[ny][nx] <= nd {
				continue
			}
			update(ny, nx, nd)
			push(ny, nx, nd)
		}
	}

	ans := d[Dh][Dw]
	if ans == INF {
		ans = -1
	}
	println(ans)

}

// Deque represetns Double Ended Queue.
type Deque struct {
	a    []interface{}
	capa int
	size int
	l, r int
	less func(x, y interface{}) bool
}

// NewDeque returns new pointer to a initialized Deque object.
func NewDeque() *Deque {
	initial := 4
	dq := &Deque{}
	dq.a = make([]interface{}, initial)
	dq.capa = initial - 1
	dq.size = 0
	dq.l = dq.capa
	dq.r = 0
	return dq
}

// Expand expands the capacity up to newsize
func (dq *Deque) Expand(newsize int) {
	nsz := newsize + 1
	na := make([]interface{}, nsz)
	dq.ForEach(func(i int, v interface{}) {
		na[i] = v
	})
	dq.a = na
	dq.capa = nsz - 1
	dq.l = dq.capa
	dq.r = dq.size
}

func (dq *Deque) expand() {
	osz := (dq.capa + 1)
	nsz := osz * 2
	if 4096 <= osz {
		nsz = osz + osz/4
	}
	dq.Expand(nsz - 1)
}

// Len() returns the number of enqued element.
func (dq *Deque) Len() int {
	return dq.size
}

// Cap() returns the capacity of the undelying slice.
func (dq *Deque) Cap() int {
	return dq.capa
}

func (dq *Deque) SetLess(cmp func(i, j interface{}) bool) {
	dq.less = cmp
}

// Less() returns whether dq.At(i) < dq.At(j)
func (dq *Deque) Less(i, j int) bool {
	return dq.less(dq.At(i), dq.At(j))
}

// Swap(i, j) swaps the elements i and j
func (dq *Deque) Swap(i, j int) {
	vi := dq.At(i)
	vj := dq.At(j)
	dq.AssignAt(i, vj)
	dq.AssignAt(j, vi)
}

// At(i) returns the i-th element (i = 0, 1, ... , Len()-1).
func (dq *Deque) At(i int) interface{} {
	if dq.size <= i {
		panic("Deque: out of range")
	}
	return dq.a[(dq.l+i+1)%len(dq.a)]
}

// AssignAt(i, v) assignes a value v to the i-th element.
func (dq *Deque) AssignAt(i int, v interface{}) {
	if dq.size <= i {
		panic("Deque: out of range")
	}
	dq.a[(dq.l+i+1)%len(dq.a)] = v
}

// Front() returns the first element
func (dq *Deque) Front() interface{} {
	if dq.size == 0 {
		panic("Deque: out of range")
	}
	return dq.a[(dq.l+1)%len(dq.a)]
}

// Back() returns the element at position 0
func (dq *Deque) Back() interface{} {
	if dq.size == 0 {
		panic("Deque: out of range")
	}
	return dq.a[(dq.r+len(dq.a)-1)%len(dq.a)]
}

// ForEach(fn(i, v)) iterates over the dq calling fn with the position i and
// the value v.
func (dq *Deque) ForEach(fn func(i int, v interface{})) {
	j := (dq.l + 1) % len(dq.a)
	for i := 0; i < dq.size; i++ {
		fn(i, dq.a[j])
		j = (j + 1) % len(dq.a)
	}
}

// All() returns a channel that emits the all element.
func (dq *Deque) All() chan interface{} {
	ch := make(chan interface{})
	go func() {
		dq.ForEach(func(_ int, v interface{}) {
			ch <- v
		})
		close(ch)
	}()
	return ch
}

// PushBack() appends v as the last element
func (dq *Deque) PushBack(v interface{}) {
	if dq.size == dq.capa {
		dq.expand()
	}
	dq.size++
	dq.a[dq.r] = v
	dq.r = (dq.r + 1) % len(dq.a)
}

// PUshFront() appends v as the first element
func (dq *Deque) PushFront(v interface{}) {
	if dq.size == dq.capa {
		dq.expand()
	}
	dq.size++
	dq.a[dq.l] = v
	dq.l = (dq.l + len(dq.a) - 1) % len(dq.a)
}

// PopBack() deletes the last element and returns it
func (dq *Deque) PopBack() interface{} {
	if dq.size == 0 {
		panic("pop from 0 length deque")
	}
	dq.size--
	dq.r = (dq.r + len(dq.a) - 1) % len(dq.a)
	return dq.a[dq.r]
}

// PopFront() deletes the first element and returns it
func (dq *Deque) PopFront() interface{} {
	if dq.size == 0 {
		panic("pop from 0 length deque")
	}
	dq.size--
	dq.l = (dq.l + 1) % len(dq.a)
	return dq.a[dq.l]
}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

func readberr() ([]byte, error) {
	b, err := nextToken()
	return b[:len(b):len(b)], err
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

func readserr() (string, error) {
	b, err := readberr()
	return string(b), err
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

func readblnerr() ([]byte, error) {
	b, err := nextLine()
	return b[:len(b):len(b)], err
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

func readslnerr() (string, error) {
	b, err := readblnerr()
	return string(b), err
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readllerr() (int64, error) {
	s, err := readserr()
	if err != nil {
		return 0, fmt.Errorf("reading string: %w", err)
	}

	i, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		return 0, fmt.Errorf("parsing int: %w", err)
	}

	return i, nil
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

func readierr() (int, error) {
	i, err := readllerr()
	return int(i), err
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func readferr() (float64, error) {
	s, err := readserr()
	if err != nil {
		return 0, fmt.Errorf("reading string: %w", err)
	}

	f, err := strconv.ParseFloat(s, 64)
	if err != nil {
		return 0, fmt.Errorf("parsing float: %w", err)
	}

	return f, nil
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func dbgf(f string, args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintf(os.Stderr, f, args...)
}

func dbg(args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func maxll(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}


Measured memory_bytes: 24140