// 20 C
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

typedef map<int,int> mii;
typedef map<char,int> mci;

#define pb push_back
#define mp make_pair
#define itr ::iterator it
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define flush fflush(stdout)

#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define dec(i,a,n) for(ll i=n-1;i>=a;i--)
#define SZ(x) ((int)(x).size())
#define remax(a,b) a=max(a,b)
#define remin(a,b) a=min(a,b)
#define LC(i) 2*(i)+1
#define RC(i) 2*(i)+2
#define MID(i,j) ((i)+(j))/2
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))

#define cini(a) int a; cin >> a;
#define cinii(a, b) int a, b; cin >> a >> b;
#define ciniii(a, b, c) int a, b, c; cin >> a >> b >> c;
#define cinl(a) ll a; cin >> a;
#define cinll(a, b) ll a, b; cin >> a >> b;
#define cinlll(a, b, c) ll a, b, c; cin >> a >> b >> c;
#define cinc(c) char c; cin >> c;
#define cins(s) string s; cin >> s;
#define cinss(s1, s2) string s1, s2; cin >> s1 >> s2;
#define cinvi(v, n) vi v(n, 0); rep(i, 0, n) cin >> v[i];
#define cinvl(v, n) vl v(n, 0); rep(i, 0, n) cin >> v[i];

#define print(v) for (auto it = v.begin(); it != v.end(); ++it) cout << *it << ' '; cout << endl;
#define printm(m) for (auto it = m.begin(); it != m.end(); ++it) cout << it->first<<' '<<it->second << endl; cout << endl;
#define disparr(arr,n) for(ll i=0;i<(n);i++)   cout<<arr[i]<<' ';  cout<<endl;
#define debug(x) cout << x << endl;
#define debug2(x,y) cout << x << " " << y << endl;
#define debug3(x,y,z) cout << x << " " << y << " " << z << endl;


const int MOD = 1000000007;
const ll INF = 1e18;
const ld PI=3.141592653589793238462643383279502884197169399375105820974944;

void dijkstra(vpl adj[], vl &parent, vl &dist, ll n, ll root)
{
	set <ll> s;
	priority_queue <pair<ll,ll>, vector<pair<ll,ll> >, greater<pair<ll, ll>>> Q;
	fill(all(dist),INF);
	fill(all(parent),(-1));
	pair<ll,ll> u;
	dist[root]=0;
	rep(i,0,n)
	{
		Q.push(mp(dist[i],i));
	}
	while(!Q.empty()) //or (s.size()<n) for connected graphs
	{
		u=Q.top();
		Q.pop();
		if(s.count(u.se)>0)
			continue;
		s.insert(u.se);
		for(auto y:adj[u.se])
		{
			if(s.count(y.fi)==0)
			{
				if(dist[y.fi]>dist[u.se]+y.se)
				{
					dist[y.fi]=dist[u.se]+y.se;
					parent[y.fi]=u.se;
					Q.push(mp(dist[y.fi],y.fi));
				}
			}
		}
	}
}

int main()
{
	fastio; //Remove for interactive problems
    int n,m,c1,c2,d1,d2;
    cin >> n >> m;
    cin >> c1 >> c2;
    cin >> d1 >> d2;
    vpl adj[n*m+1];
    c1 -= 1;
    c2 -= 1;
    d1 -= 1;
    d2 -= 1;
    vector<vector<char>> a(n);
    for(int i=0; i<n; i++)
    {
        string s;
        cin >> s;
        for(int j=0; j<m; j++)
        {
            a[i].push_back(s[j]);
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if (a[i][j] == '#')continue;
            for(int k=max(0, i-2); k<min(i+3,n); k++)
            {
                for(int l=max(0, j-2); l<min(j+3,m); l++)
                {
                    if (a[k][l]=='#')continue;
                    if ((k==i && abs(l-j) == 1)||(l==j && abs(k-i) == 1))
                    {
                        adj[i*m+j+1].pb(mp(k*m+l+1, 1));
                    }
                    else {
                        adj[i*m+j+1].pb(mp(k*m+l+1, 6969));
                    }
                }
            }
        }
    }

    vl parent(n*m+1);
    vl dist(n*m+1);
    dijkstra(adj, parent, dist, n*m+1, c1*m+c2+1);
    if(parent[d1*m+d2+1]==-1)
        cout<<"-1";

    else
    {
        cout<<dist[d1*m+d2+1]/6969;
    }

	return 0;
}

Measured memory_bytes: 245752