#include<bits/stdc++.h>
using namespace std;
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii              pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define PI              3.14159265
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define __              ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define rep(i,a,b) 		for(int i=a;i<b;i++)
#define repb(i,a,b) 	for(int i=a;i>=b;i--)
#define endl			"\n"
#define m(a)		    memset(a,0,sizeof(a))
#define all(x)          begin(x), end(x)

/*
// warning this works with cpp14 only...register depricated in 17
void fastscan(int &x){
bool neg = false;register int c;
x = 0;c = getchar();
if(c=='-'){neg = true;c = getchar();}
for(; (c>47 && c<58); c=getchar()){x = x *10 + c - 48;}
if(neg){x *= -1;}}

*/

void file(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt" , "r" , stdin);
	freopen("out.txt" , "w" , stdout);
	#endif
}

int bs(int a[] , int s , int e, int x){
	if(s<=e){
		int m = (s+e)/2;
		if(a[m]==x)
			return m;
		else if(a[m] > x)
			return bs(a,s,m-1,x);
		else return bs(a,m+1,e,x);
	}
	return -1;
}

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;
#define tr5(v,w,x,y,z) cout<<v<<" "<<w<<" "<<x<<" "<<y<<" "<<z<<endl;
#define tr6(u,v,w,x,y,z) cout<<u<<" "<<v<<" "<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl
#define deb3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl
#define deba(a) for(auto &x:a) cout<<x<<" ";

#define sd(x) cin >> x;
#define sd2(x,y) cin >> x >> y;
#define sd3(x,y,z) cin >> x >> y >> z;
#define sd4(w,x,y,z) cin >> w >> x >> y >> z;

bool is_pal(string s) {return equal(all(s), s.rbegin());}

int fxp(int a, int b ,int p){       //(logn) fast exponentiation
    int res = 1;            		// call int x = fxp(2,5);
    while (b > 0) {
        if (b & 1)
            res = (res * a)%p;
        a = (a * a)%p;
        b >>= 1;
    }
    return (res%p);
}

void solveA(){
	int n,x,t; cin >> n >> x >> t;
	int ans=0;
	ans+=(t*(n/x));
	if(n%x!=0)
		ans+=t;
	cout << ans << endl;
}

void solveB(){
	string s; cin >> s;
	int sc=0;
	for(char c : s){
		int cur = c-'0';
		sc+=cur;
	}
	if(sc%9)
		cout <<"No" << endl;
	else cout << "Yes" << endl;
}

void solveC(){
	int n; cin >> n;
	int a[n]; for(int i=0 ; i<n ; ++i) cin >> a[i];
	int ans=0;
	for(int i=1 ; i<n ; ++i)
	{
		if(a[i-1] > a[i]){
			ans+=(a[i-1]-a[i]);
			a[i] = a[i-1];
		}
	}
	cout << ans << endl;
}


const int N = 10001;
char mat[N][N];

const int dx[4] = {1 , 0 , -1 , 0};
const int dy[4] = {0 , 1 , 0 , -1};

bool isValid(int x , int y , int h , int w){
	if(x >= w || y>=h || h<0 || y<0 || mat[x][y]=='#')
		return false;
	return true;
}

void solve(){
	int h,w;
	cin >> h >> w;
	int sx,sy; cin >> sx >> sy;
	sx--; sy--;
	int ex,ey; cin >> ex >> ey;
	ex--; ey--;

	for(int i=0 ; i<h ; ++i){
		for(int j=0 ; j<w ; ++j){
			cin >> mat[i][j];
		}
	}

	int dis[h][w];
	for(int i=0 ; i<h ; ++i){
		for(int j=0 ; j<w ; ++j){
			dis[i][j]=inf;
		}
	}

	dis[sx][sy]=0;

	deque<int>xs,ys;
	xs.push_front(sx); ys.push_front(sy);
	while(!xs.empty() and !ys.empty()){
		int cx = xs.front(); int cy = ys.front();
		xs.pop_front(); ys.pop_front();
		for(int i=0 ; i<4 ; ++i){
			int nx=cx+dx[i];
			int ny=cy+dy[i];
			if(nx >= h || ny >= w || nx<0 || ny < 0 || mat[nx][ny]=='#')
				continue;

			if(dis[nx][ny] > dis[cx][cy]){
				dis[nx][ny] = dis[cx][cy];
				xs.push_front(nx);
				ys.push_front(ny);
			}
		}

		for(int i=cx-2 ; i<=cx+2 ; ++i){
			for(int j=cy-2 ; j<=cy+2 ; ++j){
				if(i>=h || j>=w || i<0 || j<0 || mat[i][j]=='#')
					continue;

				if(dis[i][j] > dis[cx][cy]+1){
					dis[i][j] = dis[cx][cy]+1;
					xs.push_back(i); ys.push_back(j);
				}
			}
		}
	}

	if(dis[ex][ey] == inf)
		cout << -1 << endl;
	else cout << dis[ex][ey] << endl;
}

int32_t main(){	
	__;
	file();
	solve();
	return 0;
}







Measured memory_bytes: 16452