h,w = gets.split(' ').map(&:to_i)
c_h, c_w = gets.split(' ').map(&:to_i)
d_h, d_w = gets.split(' ').map(&:to_i)

c_h = c_h - 1
c_w = c_w - 1
d_h = d_h - 1
d_w = d_w - 1

map = []

h.times do
  map << gets.split('')
end

if c_h > d_h
  map = map.reverse
end

if c_w > d_w
  map = map.each { |row| row.reverse }
end

def print_map map, c_h, c_w, d_h, d_w
  map.each.with_index do |row, i|
    if i == c_h
      row[c_w] = 'C'
    end
    if i == d_h
      row[d_w] = 'D'
    end
    puts "#{i}  #{row.join('')}"
  end
end

road = '.'
wall = '#'
sum = 0


loop do
  if c_w < d_w && map[c_h][c_w + 1] == road
    c_w = c_w + 1
  elsif c_h < d_h && map[c_h + 1] && map[c_h + 1][c_w] == road
    c_h = c_h + 1
  elsif map[c_h + 2] && map[c_h + 2][c_w + 2] == road
    c_h = c_h + 2
    c_w = c_w + 2
    sum += 1
  elsif map[c_h + 1] && map[c_h + 1][c_w + 2] == road
    c_h = c_h + 1
    c_w = c_w + 2
    sum += 1
  elsif map[c_h + 2] && map[c_h + 2][c_w + 1] == road
    c_h = c_h + 2
    c_w = c_w + 1
    sum += 1
  elsif map[c_h + 1] && map[c_h + 1][c_w + 1] == road
    c_h = c_h + 1
    c_w = c_w + 1
    sum += 1

  elsif map[c_h - 1] && map[c_h - 1][c_w + 2] == road
    c_h = c_h - 1
    c_w = c_w + 2
    sum += 1
  elsif map[c_h - 1] && map[c_h - 1][c_w + 1] == road
    c_h = c_h - 1
    c_w = c_w + 1
    sum += 1

  elsif map[c_h - 2] && map[c_h - 2][c_w + 2] == road
    c_h = c_h - 2
    c_w = c_w + 2
    sum += 1
  elsif map[c_h - 1] && map[c_h - 1][c_w + 1] == road
    c_h = c_h - 2
    c_w = c_w + 1
    sum += 1

  elsif map[c_h + 2] && map[c_h + 2][c_w - 1] == road
    c_h = c_h + 2
    c_w = c_w - 1
    sum += 1
  elsif map[c_h + 1] && map[c_h + 1][c_w - 1] == road
    c_h = c_h + 1
    c_w = c_w - 1
    sum += 1

  elsif map[c_h + 2] && map[c_h + 2][c_w - 2] == road
    c_h = c_h + 2
    c_w = c_w - 2
    sum += 1
  elsif map[c_h + 1] && map[c_h + 1][c_w - 2] == road
    c_h = c_h + 1
    c_w = c_w - 2
    sum += 1

  else
    # print_map(map, c_h, c_w, d_h, d_w)
    sum = -1
    break
  end
  break if c_h == d_h && c_w == d_w
end

puts sum