function exists(areaList, r, c, H, W) {
  const cell = r * H + c
  const resultList = areaList.map(area => area.some(areaCell => areaCell == cell))
  if (resultList.length == 0) {
    return false
  } else {
    const result = resultList.reduce((x, y) => x || y)
    return result
  }
}

function getAreaCells(map, r, c, H, W) {
  let cells = []
  const search = (row, col) => {
    if (row < 0 || H <= row || col < 0 || W <= col) return
    if (map[row][col] == "#") return
    const cell = row * H + col
    if (0 < cells.length && cells.some(x => x == cell)) return
    cells.push(cell)
    for (let i = 0; i < 4; i++) {
      const { newRow, newCol } = newIndex(i, row, col)
      search(newRow, newCol)
    }
  }
  search(r, c)
  return Array.from(new Set(cells))
}

function newIndex(direction, r, c) {
  let newRow, newCol
  if (direction == 0) {
    newRow = r - 1
    newCol = c
  } else if (direction == 1) {
    newRow = r
    newCol = c + 1
  } else if (direction == 2) {
    newRow = r + 1
    newCol = c
  } else if (direction == 3) {
    newRow = r
    newCol = c - 1
  }
  return { newRow, newCol }
}

function getFiveRangeCells(cell, H, W, map) {
  const leftTopRow = Math.floor(cell / H) - 2
  const leftTopCol = cell % W - 2
  let cells = []
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
      const row = leftTopRow + r
      const col = leftTopCol + c
      if (row < 0 || H <= row || col < 0 || W <= col) continue
      if (map[row][col] == "#") continue
      cells.push(row * H + col)
    }
  }
  return cells
}

function getAreaNum(areaList, cell) {
  for (let i = 0; i < areaList.length; i++) {
    if (areaList[i].some(x => x == cell)) {
      return i
    }
  }
  return -1
}

function calcWorpCount(worpAreaListOfArea, fromAreaNum, toAreaNum) {
  if (fromAreaNum == toAreaNum) return 0
  let alreadyTakenArea = [fromAreaNum]
  const search = nowArea => {
    const worpList = worpAreaListOfArea[nowArea]
    if (worpList.some(x => x == toAreaNum)) {
      return 1
    } else {
      alreadyTakenArea.push(nowArea)
      const canWorpList = worpList.filter(x => !alreadyTakenArea.some(xx => xx == x))
      if (canWorpList.length == 0) {
        return -1
      } else {
        const result = canWorpList.map(x => search(x)).filter(x => x != -1)
        return result.reduce((x, y) => Math.min(x, y)) + 1
      }
    }
  }
  return search(fromAreaNum)
}

function Main(input) {
  const kabe = "#"
  const miti = "."

  const lines = input.split("\n");
  const H = parseInt(lines[0].split(" ")[0])
  const W = parseInt(lines[0].split(" ")[1])
  const ch = parseInt(lines[1].split(" ")[0])
  const cw = parseInt(lines[1].split(" ")[1])
  const dh = parseInt(lines[2].split(" ")[0])
  const dw = parseInt(lines[2].split(" ")[1])
  const mapLines = lines.slice(3)
  const map = mapLines.map(rowText => rowText.split(""))

  let areaList = []
  for (let r = 0; r < H; r++) {
    for (let c = 0; c < W; c++) {
      if (exists(areaList, r, c, H, W)) continue
      if (map[r][c] == "#") continue
      areaList.push(getAreaCells(map, r, c, H, W))
    }
  }
  const worpAreaListOfArea = areaList.map(area => {
    const worpCellListTmp = area.flatMap(cell => getFiveRangeCells(cell, H, W, map))
    const worpCellList = Array.from(new Set(worpCellListTmp))
    const worpAreaListTmp = worpCellList.map(cell => getAreaNum(areaList, cell))
    const worpAreaList = Array.from(new Set(worpAreaListTmp))
    return worpAreaList
  })
  const cCell = (ch - 1) * H + (cw - 1)
  const cArea = getAreaNum(areaList, cCell)
  const dCell = (dh - 1) * H + (dw - 1)
  const dArea = getAreaNum(areaList, dCell)
  const worpCount = calcWorpCount(worpAreaListOfArea, cArea, dArea)
  console.log(worpCount)
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));