#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vpll vector < pair<long long,long long> >
#define fill(a,val)  memset(a,val, sizeof (a))
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define all(cont) cont.begin(), cont.end()
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(max(a,d),max(b,c))
#define MAX   1e9
#define MIN  -1e9
typedef map<int,int> mpi;
typedef set<int> seti;
typedef multiset<int> mseti;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define rep(i, n)    for(long long int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(long long int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(long long int i = a; i >= (n); --i)
#define repn(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define trav(a, x) for(auto& a : x)
#define dbg(x) cout << #x << " = " << x << '\n'
#define dbg2(x,y) cout<<#x<<" = "<<x<<" & "<<#y<<" = "<<y<<endl
#define inp(a,start,len) for(long long int i = start;i < len;i++) cin>>a[i]
struct Interval{ll start,end;};
bool compare(Interval i1, Interval i2) {return (i1.start < i2.start);}

vpll adj[1000005];
ll dist[1000005];
bool visited[1000005];

ll check(ll i, ll j, ll k,ll p){
	ll dist = abs(i-k)+abs(j-p);
	if(dist == 1)
		return 0;
	else
		return 1;
}


void dijkstra(ll source, ll des,ll n) {
    multiset<pair<int,int>> s;  // using as min-priority queue (multiset is sorted by pair.first with duplicate values)
    s.insert({ 0, source });
    for(int i = 0; i < 1000004; i++)
        dist[i] = MAX;
    dist[source] = 0;
    
    while(!s.empty()) {
        pair<int,int> m = *s.begin();
        s.erase(s.begin());
        
        int u = m.second;
        if(visited[u]) continue;
        
        visited[u] = true;
        
        for(pair<int,int> e : adj[u]) {
            int v = e.first, w = e.second;
            if(dist[u] + w < dist[v]) {
                dist[v] = dist[u] + w;
                s.insert({ dist[v], v });
            }
        }
    }

    if(dist[des] == MAX){
    	cout<<-1;
    }
    else
    	cout<<dist[des];
    
}



int main(){
	fio;
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ll T = 1;
	// cin>>T;
	while(T--){
		ll h,w;
		cin>>h>>w;
		ll xs,ys,xd,yd;
		cin>>xs>>ys>>xd>>yd;
		ll mapped[h+1][w+1];
		char arr[h+1][w+1];
		ll curr = 0;
		rep(i,h){
			rep(j,w){
				cin>>arr[i][j];
				mapped[i][j] = curr;
				curr++;
			}
		}
		rep(i,h){
			rep(j,w){
				if(arr[i][j] == '#'){
					continue;
				}
				repA(k,max(0LL,i-2),min(h-1,i+2)){
					repA(p,max(0LL,j-2),min(w-1,j+2)){
						if(arr[k][p] == '#' || (i==k && j==p)){
							continue;
						}
						if(check(i,j,k,p) == 0){
							adj[mapped[i][j]].pb({mapped[k][p],0});
						}
						else if(check(i,j,k,p) == 1){
							adj[mapped[i][j]].pb({mapped[k][p],1});
						}
					}
				}
			}
		}

		ll start = mapped[xs-1][ys-1];
		ll des = mapped[xd-1][yd-1];
		// dbg2(start,des);

		// rep(i,curr){
		// 	rep(j,adj[i].size()){
		// 		cout<<i<<" "<<adj[i][j].f<<" "<<adj[i][j].s<<"\n";
		// 	}
		// }

		dijkstra(start,des,curr);

	}
	return 0;
}


Measured memory_bytes: 176804