#include<bits/stdc++.h>
using namespace std;
 
#define 	pi 				3.14159265358979
#define 	mod 			1000000007
#define 	ll  			long long int
#define 	rep(i, a, b) 	for(ll i = a ; i < b ; i++)
#define     repn(i, a, b)   for(ll i = a ; i > b ; i--)
#define 	mod1 			1000003
#define     mod2            998244353
#define     inf             2e18
#define 	hi   			"hello"
#define 	nl 				"\n"
#define 	ws 				" "
#define 	ff 				first
#define 	ss				second
#define 	pb 				push_back
#define 	mp				make_pair
#define 	B 				begin()
#define     RB              rbegin()
#define 	E 				end()
#define     UB              upper_bound
#define     LB              lower_bound
#define     present(k, m)   (m.find(k)!=m.end())?1:0
#define 	all(v) 			v.begin(), v.end()
#define 	reset(a, val) 	memset(a, val, sizeof(a))
#define     pll             pair<ll, ll>
#define     vll             vector<ll>
#define     vvll            vector<vll>
#define     vpll            vector<pll>
#define 	vs 				vector<string>
#define     vc              vector<char>
#define     sqr(a)          ((a)*(a))
#define     print(a)        cout << a << '\n'
#define 	print1(a) 		for(auto i : a)cout<<i<<" ";cout<<"\n"
#define 	print2(a) 		for(auto i : a)cout<<i;cout<<"\n"
#define 	print3(a) 		for(auto i : a)cout<<i<<nl;
#define 	boost 			ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
 
// ostringstream str1;
// str1 << n;
// string tmp = str1.str();
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); }
 
bool compare(const pair<string, int>&a, const pair<string, int>&b){ return a.second<b.second; }
 
double logn(double n, double r) { return (n > r - 1) ? 1 + logn(n / r, r) : 0; }
 
 
// vll pwx(22);
// void cal(ll x){ pwx[0] = 1; rep(i, 1, 22)pwx[i] = x*(pwx[i-1]); }
 
/* ################################################################ */

#define space 200005

pll mov[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void solve() { 
    ll n, m, cx, cy, dx, dy;
    cin >> n >> m >> cx >> cy >> dx >> dy;

    vvll dist(n, vll(m, 2e18));
    ll mat[n][m];
    memset(mat, 0, sizeof mat);

    string s;

    rep(i, 0, n) {
        cin >> s;
        rep(j, 0, m) {
            if(s[j] == '.') mat[i][j] = 1;
        }
    }

    cx--, cy--;
    dx--, dy--;

    priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>> pq;

    pq.push({0, {cx, cy}});
    
    dist[cx][cy] = 0;

    ll ans = 0;

    while(!pq.empty()) {
        pll node = pq.top().ss;
        ll d_node = pq.top().ff;
        pq.pop();

        if(d_node != dist[node.ff][node.ss]) continue;

        for(auto z : mov) {
            int x = node.ff + z.ff;
            int y = node.ss + z.ss;

            if(x >= 0 && x < n && y >= 0 && y < m && mat[x][y]) {

                if(dist[x][y] > dist[node.ff][node.ss]) {
                    dist[x][y] = dist[node.ff][node.ss];
                    pq.push({dist[x][y], {x, y}});
                }
            }
        }

        for(int i = -2 ; i <= 2 ; i++) {
            for(int j = -2 ; j <= 2 ; j++) {
                if(abs(i) + abs(j) <= 1) continue;

                int x = node.ff + i;
                int y = node.ss + j;

                if(x >= 0 && x < n  && y >= 0 && y < m && mat[x][y]) {

                    if(dist[x][y] > dist[node.ff][node.ss] + 1) {
                        dist[x][y] = dist[node.ff][node.ss] + 1;
                        pq.push({dist[x][y], {x, y}});
                    }
                }
            }
        }
    }

    if(dist[dx][dy] == 2e18) {
        cout << -1 << nl; return;
    }

    cout << dist[dx][dy] << nl;
    
}

int main(){
    boost;
    // cal(2);

    // For getting input from input.txt file 
    // freopen("running_on_fumes_chapter_1_input.txt", "r", stdin); 
  
    // // Printing the Output to output.txt file 
    // freopen("output.txt", "w", stdout);

    int t = 1, k = 1;
    // cin>>t;
    while(t--){
        // cout<<"Case #"<<k<<": ";
        solve();
        k++;
    }
}

Measured memory_bytes: 19448