const main = (input) => {
  input = input.trim().split('\n')
  // const [h, w] = input[0].split(' ').map(Number)
  const [ch, cw] = input[1].split(' ').map(Number)
  const [dh, dw] = input[2].split(' ').map(Number)
  const [,,, ...g] = input
  const grid = g.map(v => v.split(''))

  const q = [[ch-1, cw-1, 0]]

  while (q.length) {
    const [h, w, cost] = q.shift()

    if (grid[h][w] === '.') grid[h][w] = cost

    if (grid[h-1] && grid[h-1][w] === '.') q.unshift([h-1, w, cost])
    if (grid[h+1] && grid[h+1][w] === '.') q.unshift([h+1, w, cost])
    if (grid[h][w-1] && grid[h][w-1] === '.') q.unshift([h, w-1, cost])
    if (grid[h][w+1] && grid[h][w+1] === '.') q.unshift([h, w+1, cost])

    for (let hh=h-2; hh<=h+2; hh++) {
      for (let ww=w-2; ww<=w+2; ww++) {
        if (grid[hh] && grid[hh][ww] && grid[hh][ww] === '.') q.push([hh, ww, cost+1])
      }
    }
  }

  return console.log(typeof grid[dh-1][dw-1] === 'number' ? grid[dh-1][dw-1] : -1)
}

process.env.MYTEST ? test() : main(require('fs').readFileSync('/dev/stdin', 'utf8'))