H,W = gets.split.map(&:to_i)
SY,SX = gets.split.map { _1.to_i - 1 }
GY,GX = gets.split.map { _1.to_i - 1 }
S = readlines.map { _1.chomp.chars }

MV = [[1,0],[-1,0],[0,1],[0,-1]]
WP = [*-2..2].product([*-2..2]) - [*MV, [0,0]]

dist = Array.new(H) { [-1]*W }
que = []
que2 = []
que.push([SY,SX])
dist[SY][SX] = 0
while !que.empty?
  
  y,x = que.pop
  d = dist[y][x]

  MV.each do |dy,dx|
    yy,xx = y+dy,x+dx
    next if yy < 0 || xx < 0 || H <= yy || W <= xx
    next if S[yy][xx] == "#"
    next if 0 <= dist[yy][xx] && dist[yy][xx] <= d
    dist[yy][xx] = d
    que.push([yy,xx])
  end
  
  WP.each do |dy,dx|
    yy,xx = y+dy,x+dx
    next if yy < 0 || xx < 0 || H <= yy || W <= xx
    next if S[yy][xx] == "#"
    next if 0 <= dist[yy][xx] && dist[yy][xx] <= d + 1
    dist[yy][xx] = d + 1
    que2.push([yy,xx])
  end
  
  if que.empty?
    break if dist[GY][GX] != -1
    que = que2
    que2 = []
  end
end

puts dist[GY][GX]
