H, W = gets.chomp.split.map(&:to_i)
Ch, Cw = gets.chomp.split.map(&:to_i)
Dh, Dw = gets.chomp.split.map(&:to_i)
S = []
H.times{ |i|
  S.push gets.chomp.each_char.to_a
}

$id_max = -1
$area_by_pos = {}
$area_by_id = {}

def area_search i, j, id=nil
  return if S[i][j] == "#"
  return if $area_by_pos[[i, j]]
  unless id
    $id_max += 1
    id = $id_max
  end
  $area_by_pos[[i, j]] = id
  $area_by_id[id] ||= []
  $area_by_id[id].push([i,j])
  if i > 1
    area_search i-1, j, id
  end
  if i < H - 1
    area_search i+1, j, id
  end
  if j > 1
    area_search i, j-1, id
  end
  if j < W - 1
    area_search i, j+1, id
  end
end

H.times{ |i|
  W.times{ |j|
    area_search i, j
  }
}

#p $area_by_pos
#p $area_by_id
#p ""

$area_to_area = []

def warpable from_area_id, to_area_id
  from_pos_list = $area_by_id[from_area_id]
  to_pos_list   = $area_by_id[to_area_id]
  from_pos_list.each{ |from_pos|
    to_pos_list.each{ |to_pos|
      if (from_pos[0] - to_pos[0]).abs <= 2 && (from_pos[1] - to_pos[1]).abs <= 2
        #p from_pos, to_pos
        #p ""
        return true
      end
    }
  }
  return false
end

$area_by_id.each_key{ |from_area_id|
  $area_by_id.each_key{ |to_area_id|
    next if from_area_id == to_area_id
    if warpable from_area_id, to_area_id
      $area_to_area.push([from_area_id, to_area_id])
    end
  }
}

#p $area_to_area
#p ""

start_area = $area_by_pos[[Ch-1,Cw-1]]
goal_area = $area_by_pos[[Dh-1,Dw-1]]

#p start_area, goal_area

def dijkstra(graph, start)
  h = graph.map {|x| [x[0], x[1].keys]}.to_h

  shortest = Hash.new(Float::INFINITY)
  pred = {}
  done = h.keys.map {|node| [node, false]}.to_h

  shortest[start] = 0

  loop do
    u = nil
    h.each_key.reject {|node| done[node]}.each do |node|
      u = node if !u or shortest[node] < shortest[u]
    end
    break unless u
    done[u] = true

    h[u].each do |v|
      if (a = shortest[u] + graph[u][v]) < shortest[v]
        shortest[v] = a
        pred[v] = u
      end
    end
  end

  [shortest, pred]
end

def make_graph area_to_area
  graph = {}
  $area_to_area.each{ |from_area, to_area|
    graph[from_area] ||= {}
    graph[from_area][to_area] = 1
  }
  graph
end

graph = make_graph $area_to_area
#p graph
#p dijkstra(graph, start_area)
ans = dijkstra(graph, start_area)[0][goal_area]
if ans == Float::INFINITY
  puts "-1"
else
  puts ans
end
