#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include<bits/stdc++.h>

using namespace std;
 
#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first                 
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back  
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()           
 
typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll,ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   N     =  2e3+5;
const ll   mod   =  1e9+7;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;
const ll   LEVEL =  log2(N)+1;

ll test = 1, n, m, cw, ch, dw, dh, dist[N][N];
string s[N];

void dijkstra(int x, int y) {
    for(int i = 0; i <= n; i++) for(int j = 0; j <= m+1; j++) dist[i][j] = INF;
    dist[x][y] = 0;
    set<pair<ll,pll>> q;
    q.insert({0, {x, y}});
    while (!q.empty()) {
        pll p = q.begin()->s;
        int x = p.f, y = p.s;
        q.erase(q.begin());
        if (s[x+1][y] != '#' and dist[x][y] < dist[x+1][y]) {
            q.erase({dist[x+1][y], {x+1, y}});
        	dist[x+1][y] = dist[x][y];
            q.insert({dist[x+1][y], {x+1, y}});
        }
        if (s[x-1][y] != '#' and dist[x][y] < dist[x-1][y]) {
            q.erase({dist[x-1][y], {x-1, y}});
        	dist[x-1][y] = dist[x][y];
            q.insert({dist[x-1][y], {x-1, y}});
        }
        if (s[x][y+1] != '#' and dist[x][y] < dist[x][y+1]) {
            q.erase({dist[x][y+1], {x, y+1}});
        	dist[x][y+1] = dist[x][y];
            q.insert({dist[x][y+1], {x, y+1}});
        }
        if (s[x][y-1] != '#' and dist[x][y] < dist[x][y-1]) {
            q.erase({dist[x][y-1], {x, y-1}});
        	dist[x][y-1] = dist[x][y];
            q.insert({dist[x][y-1], {x, y-1}});
        }
        for(int i = x-2; i <= x+2; i++){
        	if(i < 1 || i > n) continue;
        	for(int j = y-2; j <= y+2; j++){
        		if(j < 1 || j > m) continue;
        		if (s[i][j] != '#' and dist[x][y]+1 < dist[i][j]) {
		            q.erase({dist[i][j], {i, j}});
		        	dist[i][j] = dist[x][y]+1;
		            q.insert({dist[i][j], {i, j}});
		        }
        	}
        }
    }
    if(dist[dw][dh] == INF) dist[dw][dh] = -1;
    deb1(dist[dw][dh])
}

void solve(){
	cin >> n >> m >> cw >> ch >> dw >> dh;
	for(int i = 1; i <= n; i++) cin >> s[i], s[i] = '#'+s[i]+'#';
	s[0] = s[n+1] = string(m+2,'#');
	dijkstra(cw,ch);
}

int main(){
	GODSPEED;
	//cin>>test;
	for(int i = 1; i <= test; i++){
		solve();
	}
	#ifndef ONLINE_JUDGE
	cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
	#endif
}

Measured memory_bytes: 17596