const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
const line1 = await readline()
let [n,m]=line1.trim().split(' ').map(Number)
const line2 = await readline()
let [start_x,start_y,end_x,end_y] = line2.trim().split(' ').map(Number)
let grid = []
for (let i = 0; i < n;i++){
const linei = await readline()
grid.push(linei.trim().split(''))
}
let result = findway(grid,start_x,start_y,end_x,end_y,n,m)
console.log(result)
}()
function findway (arr,start1,start2,end1,end2,n,m){
if(arr[start1 -1][start2 - 1] == "*" || arr[end1 -1 ][end2 - 1]== "*"){
return -1
}
let direction = [[0,-1],[0,1],[1,0],[-1,0]]
let queue = [[start1 - 1,start2 - 1,0]]
arr[start1 - 1][start2 - 1] = "*"
while(queue.length){
let [x,y,step] = queue.shift()
if(x == end1 - 1 && y==end2 - 1){
return step
}
for(let [dx,dy] of direction){
let nx = x + dx
let ny = y + dy
if(nx >=0 && nx < n && y >=0 && y<m && arr[nx][ny] == "."){
arr[nx][ny] ="*"
queue.push([nx,ny,step+1])
}
}
}
return -1
}