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 = [[SY,SX]]
dist[SY][SX] = 0
mem = Array.new(H) { []*W }
until que.empty?
  break if dist[GY][GX] != -1

  que2 = []
  until que.empty?
    y,x = que.pop
    d = dist[y][x]

    blk = false
    MV.each do |dy,dx|
      yy,xx = y+dy,x+dx
      next if yy < 0 || xx < 0 || H <= yy || W <= xx
      next blk=true if S[yy][xx] == "#"
      next if 0 <= dist[yy][xx] && dist[yy][xx] <= d
      dist[yy][xx] = d
      mem[yy][xx] = true
      que.push([yy,xx])
    end
    que2.push([y,x]) if blk
  end
  
  que2.each do |y,x|
    WP.each do |dy,dx|
      yy,xx = y+dy,x+dx
      next if yy < 0 || xx < 0 || H <= yy || W <= xx
      next if mem[yy][xx]
      next if S[yy][xx] == "#"
      next if 0 <= dist[yy][xx] && dist[yy][xx] <= d + 1
      dist[yy][xx] = d + 1
      que.push([yy,xx])
    end
  end

end

puts dist[GY][GX]
