#include <bits/stdc++.h>
#include <list>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using T = tuple<int, int, int>;
constexpr int INF = 1e9+7;
list<T> ls;
int H, W, h, w, d, eh, ew, ds[1005][1005], dhw[][2] = {{0,1},{0,-1},{1,0},{-1,0}};
char s[1005];
int main(){
	scanf("%d%d%d%d%d%d", &H, &W, &h, &w, &eh, &ew);
	h++; w++; eh++; ew++;
	fill(ds[0], ds[1004], -1);
	rep(i,H){
		scanf("\n%s", s);
		rep(j,W) if(s[j] == '.') ds[i+2][j+2] = INF;
	}
	ls.emplace_front(h,w,0);
	while(!ls.empty()){
		tie(h,w,d) = ls.front(); ls.pop_front();
		if(ds[h][w] <= d) continue;
		ds[h][w] = d;
		for(auto [dh, dw]: dhw) ls.emplace_front(h+dh, w+dw, d);
		rep(i,5) rep(j,5) ls.emplace_back(h+i-2,w+j-2,d+1);
	}
	printf("%d\n", ds[eh][ew] == INF ? -1 : ds[eh][ew]);
}