/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author rchalamala
 */

/*
ID: rchalam1
LANG: C++14
TASK:
*/
// For train.usaco.org, fill the template above.

// Competitive Coding Template

// GCC OPTIMIAZTION FLAGS
#ifndef LOCAL

#include <x86intrin.h>

#pragma GCC optimize("fast-math,no-stack-protector,Ofast,omit-frame-pointer,unroll-loops,inline")
#pragma GCC option("arch=native,no-zero-upper,tune=native")
#pragma GCC target("abm,bmi2,mmx,popcnt,sse,sse2,sse3,sse4,ssse3")

#endif

#define NDEBUG

#ifndef BASE_HPP
#define BASE_HPP

// Verification:
//

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#include <forward_list>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <mutex>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <thread>
#include <tuple>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

template<typename T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, std::less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
template<typename T> using ordered_multiset = __gnu_pbds::tree<T, __gnu_pbds::null_type, std::less_equal<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

using st = std::size_t;
using ui = std::uint32_t;
using ii = std::int32_t;
using ul = std::uint64_t;
using ll = std::int64_t;
using ld = long double;
using cd = std::complex<long double>;

using pst = std::pair<std::size_t, std::size_t>;
using pui = std::pair<std::uint32_t, std::uint32_t>;
using pii = std::pair<std::int32_t, std::int32_t>;
using pul = std::pair<std::uint64_t, std::uint64_t>;
using pll = std::pair<std::int64_t, std::int64_t>;
using pld = std::pair<long double, long double>;
using pcd = std::pair<std::complex<long double>, std::complex<long double>>;

using vst = std::vector<std::size_t>;
using vui = std::vector<std::uint32_t>;
using vii = std::vector<std::int32_t>;
using vul = std::vector<std::uint64_t>;
using vll = std::vector<std::int64_t>;
using vld = std::vector<long double>;
using vcd = std::vector<std::complex<long double>>;

using vvst = std::vector<std::vector<std::size_t>>;
using vvui = std::vector<std::vector<std::uint32_t>>;
using vvii = std::vector<std::vector<std::int32_t>>;
using vvul = std::vector<std::vector<std::uint64_t>>;
using vvll = std::vector<std::vector<std::int64_t>>;
using vvld = std::vector<std::vector<long double>>;
using vvcd = std::vector<std::vector<std::complex<long double>>>;

using vpst = std::vector<std::pair<std::size_t, std::size_t>>;
using vpui = std::vector<std::pair<std::uint32_t, std::uint32_t>>;
using vpii = std::vector<std::pair<std::int32_t, std::int32_t>>;
using vpul = std::vector<std::pair<std::uint64_t, std::uint64_t>>;
using vpll = std::vector<std::pair<std::int64_t, std::int64_t>>;
using vpld = std::vector<std::pair<long double, long double>>;
using vpcd = std::vector<std::pair<std::complex<long double>, std::complex<long double>>>;

#define FOR(i, a, b) for (auto (i) = (a); (i) < (b); ++(i))
#define F0R(i, a) FOR((i), 0, (a))
#define ROF(i, a, b) for (auto (i) = (a); (i) >= (b); --(i))
#define R0F(i, a) ROF((i), (a), 0)
#define ITR(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define FSRT(set) std::sort(std::begin(set), std::end(set))
#define RSRT(set) std::sort(std::rbegin(set), std::rend(set))
#define CSRT(set, function) std::sort(std::begin(set), std::end(set), (function))
#define UNIQUE(set) std::sort(std::begin(set), std::end(set), (set).erase(std::unique(std::begin(set), std::end(set)), (set).end())
#define CLR(set, element) memset((set), (element), sizeof((set)))

#define ALL(set) std::begin(set), std::end(set)
#define RALL(set) std::rbegin(set), std::rend(set)

#define sc static_cast
#define sz(set) static_cast<std::int32_t>((set).size())

template<typename Arg, typename... Args> Arg min(const Arg& first, const Args& ... rest) { return std::min(first, min(rest...)); }

template<typename Arg, typename... Args> Arg max(const Arg& first, const Args& ... rest) { return std::max(first, min(rest...)); }

template<class T> const T& min(const std::vector<T>& elements) { return *std::min_element(elements.begin(), elements.end()); }

template<class T> const T& max(const std::vector<T>& elements) { return *std::max_element(elements.begin(), elements.end()); }

#define mp std::make_pair
#define mt std::make_tuple
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound

#if __cplusplus >= 201402


namespace std
{
	template<class Fun> class y_combinator_result
	{
		Fun fun_;
	public:
		template<class T> explicit y_combinator_result(T&& fun): fun_(std::forward<T>(fun)) {}

		template<class ...Args> decltype(auto) operator()(Args&& ...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
	};

	template<class Fun> decltype(auto) y_combinator(Fun&& fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
}

#endif

#endif
#ifndef IO_HPP
#define IO_HPP

// Verification:
//













// https://github.com/bqi343/USACO/blob/master/Implementations/content/contest/CppIO.h
namespace std
{

	std::string to_string(const char& element) { return std::string(1, element); }

	std::string to_string(const char *element) { return std::string(element); }

	std::string to_string(const std::string& element) { return element; }

	std::string to_string(const bool& element)
	{
#ifdef LOCAL
		return element ? "true" : "false";
#else
		return std::string{static_cast<char>('0' + element)};
#endif
	}

	template<typename T> std::string to_string(const std::complex<T>& element)
	{
		std::stringstream convert;
		convert << element;
		return convert.str();
	}

	std::string to_string(const std::vector<bool>& elements)
	{
		std::string convert = "{";
		for(const auto& element : elements) convert += static_cast<char>('0' + element);
		return convert + "}";
	}

	template<std::size_t Size> std::string to_string(const std::bitset<Size>& elements)
	{
		std::string convert;
		for(auto i = 0; i < Size; ++i) convert += static_cast<char>('0' + elements[i]);
		return convert;
	}

	template<typename T1, typename T2> std::string to_string(std::pair<T1, T2> element)
	{
#ifdef LOCAL
		return "(" + std::to_string(element.first) + ", " + std::to_string(element.second) + ")";
#else
		return std::to_string(element.ff) + " " + std::to_string(element.ss);
#endif
	}

	template<typename T> std::string to_string(const T& elements)
	{
		std::string convert;
		bool first = true;
#ifdef LOCAL
		convert += "{";
		for(const auto& element : elements)
		{
			if(!first) convert += ", ";
			first = false;
			convert += std::to_string(element);
		}
		convert += "}";
#else
		for(const auto& element : elements)
		{
			if(!first) convert += " ";
			first = false;
			convert += std::to_string(element);
		}
#endif
		return convert;
	}
}

// INPUT
bool inline read(std::istream& in, double& element);

bool inline read(std::istream& in, long double& element);

template<typename Arg, typename... Args> bool inline read(std::istream& in, Arg& first, Args& ... rest);

template<typename T, std::size_t Size> bool inline read(std::istream& in, std::array<T, Size>& elements);

template<typename T1, typename T2> bool inline read(std::istream& in, std::pair<T1, T2>& element);

template<typename T> bool inline read(std::istream& in, T& element);

template<typename T> bool inline read(std::istream& in, std::complex<T>& element);

template<typename T> bool inline read(std::istream& in, std::tuple<T, T, T, T, T, T>& element);

template<typename T> bool inline read(std::istream& in, std::tuple<T, T, T, T, T>& element);

template<typename T> bool inline read(std::istream& in, std::tuple<T, T, T, T>& element);

template<typename T> bool inline read(std::istream& in, std::tuple<T, T, T>& element);

template<typename T> bool inline read(std::istream& in, std::vector<T>& elements);

bool inline read(std::istream& in, double& element)
{
	bool result = true;
	std::string convert;
	result &= read(in, convert);
	element = std::stod(convert);
	return result;
}

bool inline read(std::istream& in, long double& element)
{
	bool result = true;
	std::string convert;
	result &= read(in, convert);
	element = std::stold(convert);
	return result;
}

template<typename Arg, typename... Args> bool inline read(std::istream& in, Arg& first, Args& ... rest)
{
	bool result = true;
	result &= read(in, first);
	result &= read(in, rest...);
	return result;
}

template<typename T, std::size_t Size> bool inline read(std::istream& in, std::array<T, Size>& elements)
{
	bool result = true;
	for(auto& element : elements) result &= read(in, element);
	return result;
}

template<typename T1, typename T2> bool inline read(std::istream& in, std::pair<T1, T2>& element) { return read(in, element.first, element.second); }

template<typename T> bool inline read(std::istream& in, T& element) { return static_cast<bool>(in >> element); }

template<typename T> bool inline read(std::istream& in, std::complex<T>& element)
{
	bool result = true;
	T first, second;
	result &= read(in, first, second);
	element = std::complex<T>(first, second);
	return result;
}

template<typename T> bool inline read(std::istream& in, std::tuple<T, T, T, T, T, T>& element) { return read(in, std::get<0>(element), std::get<1>(element), std::get<2>(element), std::get<3>(element), std::get<4>(element), std::get<5>(element)); }

template<typename T> bool inline read(std::istream& in, std::tuple<T, T, T, T, T>& element) { return read(in, std::get<0>(element), std::get<1>(element), std::get<2>(element), std::get<3>(element), std::get<4>(element)); }

template<typename T> bool inline read(std::istream& in, std::tuple<T, T, T, T>& element) { return read(in, std::get<0>(element), std::get<1>(element), std::get<2>(element), std::get<3>(element)); }

template<typename T> bool inline read(std::istream& in, std::tuple<T, T, T>& element) { return read(in, std::get<0>(element), std::get<1>(element), std::get<2>(element)); }

template<typename T> bool inline read(std::istream& in, std::vector<T>& elements)
{
	bool result = true;
	for(auto& element : elements) result &= read(in, element);
	return result;
}

// OUTPUT
template<typename Arg, typename... Args> void inline print(std::ostream& out, const Arg& first, const Args& ... rest);

template<typename Arg, typename... Args> void inline printn(std::ostream& out, const Arg& first, const Args& ... rest);

template<typename Arg, typename... Args> void inline prints(std::ostream& out, const Arg& first, const Args& ... rest);

template<typename T> void inline print(std::ostream& out, const T& element);

void inline printn(std::ostream& out);

void inline prints(std::ostream& out);

template<typename Arg, typename... Args> void inline print(std::ostream& out, const Arg& first, const Args& ... rest)
{
	print(out, first);
	print(out, rest...);
}

template<typename Arg, typename... Args> void inline printn(std::ostream& out, const Arg& first, const Args& ... rest)
{
	print(out, first);
	if(sizeof...(rest)) prints(out);
	printn(out, rest...);
}

template<typename Arg, typename... Args> void inline prints(std::ostream& out, const Arg& first, const Args& ... rest)
{
	print(out, first);
	if(sizeof...(rest)) print(out, " ");
	prints(out, rest...);
}

template<typename T> void inline print(std::ostream& out, const T& element) { out << std::to_string(element); }

void inline printn(std::ostream& out) { print(out, '\n'); }

void inline prints(std::ostream& out) { print(out, '\n'); }

void setIO()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);
#ifdef LOCAL
	std::cin.exceptions(std::ios::eofbit | std::ios::failbit | std::ios::badbit);
#endif
}

#endif
#ifndef DEBUG_HPP
#define DEBUG_HPP

// Verification:
//




// https://github.com/bqi343/USACO/blob/master/Implementations/content/contest/CppIO.h
template<typename Arg, typename... Args> void debug(const Arg& first, const Args& ... rest);

void debug();

template<typename Arg, typename... Args> void debug(const Arg& first, const Args& ... rest)
{
	std::cerr << std::to_string(first);
	if(sizeof...(rest)) std::cerr << ", ";
	debug(rest...);
}

void debug() { std::cerr << "]" << std::endl; }

#ifdef LOCAL
#define DEBUG(...) std::cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", debug(__VA_ARGS__)
#else
#define DEBUG(...) 0
#endif

#endif

using namespace std;

constexpr long long MAXN __attribute__((unused)) = static_cast<long long>(2 * 1e5 + 1);
constexpr long long INF __attribute__((unused)) = static_cast<long long>(1e18 + 1);
constexpr long double EPS __attribute__((unused)) = static_cast<long double>(1e-7);

//#define CODE_JAM
//#define LIGHT_OJ

class DWizardInMaze
{
	void output_case(std::ostream& out)
	{
		static std::int32_t _testCase = 0;
		print(out, "Case ");
		#ifdef CODE_JAM
		print(out, '#');
		#endif
		print(out, ++_testCase, ": ");
	}

public:
	void solve(std::istream& in, std::ostream& out)
	{
		#if defined(CODE_JAM) || defined(LIGHT_OJ)
		output_case(out);
		#endif
		ll n, m, ch, cw, dh, dw;
		read(in, n, m, ch, cw, dh, dw);
		vector<vector<char>> matrix(n + 2, vector<char>(m + 2, '#'));
		for(ll i = 1; i <= n; ++i)
			for(ll j = 1; j <= m; ++j)
				read(in, matrix[i][j]);
		vvll depths(n + 2, vll(m + 2, INF));
		deque<pii> q;
		depths[ch][cw] = 0;
		q.eb(ch, cw);
		auto valid = [&n, &m, &matrix, &depths](const ll& i, const ll& j) -> bool { return i > 0 && i <= n && j > 0 && j <= m && matrix[i][j] == '.'; };
		vpll offsets{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
		while(!q.empty())
		{
			auto ij = q.front();
			q.pop_front();
			for(auto& offset : offsets)
			{
				auto i = ij.ff + offset.ff, j = ij.ss + offset.ss;
				if(valid(i, j) && depths[i][j] > depths[ij.ff][ij.ss])
				{
					depths[i][j] = depths[ij.ff][ij.ss];
					q.emplace_front(i, j);
				}
			}
			for(ll i = -2; i <= 2; ++i)
				for(ll j = -2; j <= 2; ++j)
					if(valid(ij.ff + i, ij.ss + j) && depths[ij.ff + i][ij.ss + j] > depths[ij.ff][ij.ss])
					{
						depths[ij.ff + i][ij.ss + j] = depths[ij.ff][ij.ss] + 1;
						q.emplace_back(ij.ff + i, ij.ss + j);
					}
		}
		printn(out, depths[dh][dw] == INF ? -1 : depths[dh][dw]);
	}
	// REMOVE COMPILER OPTIMIZATION FLAGS IF RUNTIME ERRORS (CIN.TIE/COUT.TIE as well)
};

int main()
{
	setIO();
	DWizardInMaze solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	std::fflush(stdout);
	return 0;
}

Measured memory_bytes: 13428