//In the name of Allah :)
#include <bits/stdc++.h>
using namespace std;
string to_string(char c) { return string(1,c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return (string)s; }
string to_string(string s) { return s; }
template<class A> string to_string(complex<A> c) { 
	stringstream ss; ss << c; return ss.str(); }
string to_string(vector<bool> v) { 
	string res = "{"; for(int i = 0; i < (int)v.size(); i++) res += char('0'+v[i]);
	res += "}"; return res; }
template<size_t SZ> string to_string(bitset<SZ> b) {
	string res = ""; for(size_t i = 0; i < SZ; i++) res += char('0'+b[i]);
	return res; }
template<class A, class B> string to_string(pair<A,B> p);
template<class T> string to_string(T v) { // containers with begin(), end()
	bool fst = 1; string res = "{";
	for (const auto& x: v) {
		if (!fst) res += ", ";
		fst = 0; res += to_string(x);
	}
	res += "}"; return res;
}
template<class A, class B> string to_string(pair<A,B> p) {
	return "("+to_string(p.first)+", "+to_string(p.second)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
	cerr << to_string(h); if (sizeof...(t)) cerr << ", ";
	DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define wis(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
#else
#define wis(...) 0
#endif
typedef long long ll;
const int MOD = 1e9 + 7, INF = 1e9 + 5;
const ll LONG_INF = 1e18 + 10;
inline ll powmod(ll a, ll b){ll res=1;for(;b;b>>=1,a=a*a%MOD)if(b&1)res=res*a%MOD;return res;}
inline ll power(ll a, ll b){ll res=1;for(;b;b>>=1,a=a*a)if(b&1)res=res*a;return res;}

const int MAXN = 1e3 + 10;
char a[MAXN][MAXN];
bitset <MAXN> mark[MAXN];

const vector <pair<int, int>> moves = {
        {0, 1},
        {1, 0},
        {-1, 0},
        {0, -1}
};
vector <pair<int, int>> smoves;

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        for(int i = -2; i <= 2; i++){
                for(int j = -2; j <= 2; j++){
                        smoves.push_back({i, j});
                }
        }
	int n, m;
        cin >> n >> m;
        pair<int, int> start, end;
        cin >> start.first >> start.second >> end.first >> end.second;
        start.first--, start.second--, end.first--, end.second--;
        for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                        cin >> a[i][j];
                        if(a[i][j] == '#')
                                mark[i][j] = 1;
                }
        }
        bool new_vis = 1;
        vector <pair<int, int>> last_vis({start});
        mark[start.first][start.second] = 1;
        queue <pair<int, int>> q;
        q.push(start);
        while(q.size()){
                int x = q.front().first, y = q.front().second;
                q.pop();
                for(auto move : moves){
                        int newx = x + move.first, newy = y + move.second;
                        if(newx < 0 || newy < 0 || newx >= n || newy >= m || mark[newx][newy]){
                                continue;
                        }
                        q.push({newx, newy});
                        mark[newx][newy] = 1;
                        last_vis.push_back({newx, newy});
                }
        }
        int ans = 0;
        wis(smoves);
        wis(last_vis);
        while(new_vis && mark[end.first][end.second] == 0){
                vector <pair<int, int>> tmp;
                new_vis = 0;
                for(auto i : last_vis){
                        int x = i.first, y = i.second;
                        for(auto move : smoves){
                                int newx = x + move.first, newy = y + move.second;
                                if(newx < 0 || newy < 0 || newx >= n || newy >= m){
                                        continue;
                                }
                                if(mark[newx][newy]){
                                        wis(newx, newy);
                                        continue;
                                }
                                q.push({newx, newy});
                                mark[newx][newy] = 1;
                                tmp.push_back({newx, newy});
                                new_vis = 1;
                        }
                }
                while(q.size()){
                        int x = q.front().first, y = q.front().second;
                        q.pop(); 
                        for(auto move : moves){
                                int newx = x + move.first, newy = y + move.second;
                                if(newx < 0 || newy < 0 || newx >= n || newy >= m || mark[newx][newy]){
                                        continue;
                                }
                                q.push({newx, newy});
                                mark[newx][newy] = 1;
                                tmp.push_back({newx, newy});
                        }
                }
                wis(tmp);
                ans++;
                last_vis = tmp;
        }
        cout << (mark[end.first][end.second] ? ans : -1) << '\n';
}


Measured memory_bytes: 4852