def map(m)
  return
  $stderr.puts "+ #{m}"
  S.each do |s|
    $stderr.puts "| #{s.join}"
  end
  $stderr.puts
end

H, W = $stdin.gets.split.map {|i| i.to_i }
Ch, Cw = $stdin.gets.split.map {|i| i.to_i }
Dh, Dw = $stdin.gets.split.map {|i| i.to_i }
S = []
0.upto(H - 1) do |h|
  S[h] = $stdin.gets.strip.split(//)
end

def set(h, w, v)
  S[h - 1][w - 1] = v
end

def get(h, w)
  if h < 1 || w < 1 || h > H || w > W
    return nil
  end
  return S[h - 1][w - 1]
end

def step(pos_list, clear_list, ch, cw, c)
  v = get(ch, cw)
  if v != '.'
    return v
  end
  set(ch, cw, c)
  pos_list.push([ch, cw])
  clear_list.push([ch, cw]) if clear_list
  return c
end

def walk(walk_list, ch0, cw0)
  c = get(ch0, cw0)
  pos_list = [[ch0, cw0]]
  while !pos_list.empty?
    ch, cw = pos_list.pop
    offset = walk_list.length - 1
    c1 = step(pos_list, walk_list, ch + 1, cw, c)
    c2 = step(pos_list, walk_list, ch - 1, cw, c)
    c3 = step(pos_list, walk_list, ch, cw + 1, c)
    c4 = step(pos_list, walk_list, ch, cw - 1, c)
    if c == c1 && c == c2 && c == c3 && c == c4
      walk_list.delete_at(offset)
    end
  end
end

def warp(warp_list, ch0, cw0)
  c = get(ch0, cw0) + 1
  -2.upto(2) do |oh|
    -2.upto(2) do |ow|
      next if (oh == 0 && ow.abs <= 1) || (ow == 0 && oh.abs <= 1)
      step(warp_list, nil, ch0 + oh, cw0 + ow, c)
    end
  end
end

if get(Ch, Cw) != '.' || get(Dh, Dw) != '.'
  puts "-1"
  exit
end

c = 0
map "-1"
set(Ch, Cw, c)
walk_list = [[Ch, Cw]]
walk(walk_list, Ch, Cw)
map c
while (dc = get(Dh, Dw)) == '.'
  c += 1
  warp_list = []
  walk_list.each do |pos|
    warp(warp_list, *pos)
  end
  if warp_list.empty?
    dc = -1
    break
  end
  walk_list = []
  warp_list.each do |pos|
    walk_list.push(pos)
    walk(walk_list, *pos)
  end
end
puts dc
