#include <bits/stdc++.h> 
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#include<cmath>
#include<string>
#define pb push_back
#define ll long long
#define ull unsigned long long
#define deb(x) cerr<<#x<<" "<<x<<"\n"
#define debi cerr<<"hey sparky\n"
#define x first
#define y second
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define eps (double)1e-9
#define all(x) x.begin(),x.end()
#define int long long
using namespace std;
using namespace __gnu_pbds; 
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> pdbs;
 
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
//void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
// look for  all edge cases
//search for a pattern


int h,w;
int start_x , start_y;
int end_y,end_x;
const int nax = 1e3+2;
char mat[nax][nax];
bool vis[nax][nax];
//map<int,set<pair<int,int>> > comp;
map<pair<int,int> , int> belongs;
vector<pair<int,int>> dir = {{0, 1} , {0 , -1} , {1 , 0} , {-1 ,0}};


void dfs(int next_x , int next_y , int cnt){
	//debug(next_x , next_y, cnt);
	vis[next_x][next_y] = true;
	//comp[cnt].insert({next_x , next_y});
	belongs[{next_x , next_y}] = cnt;
	for(auto it : dir){
		int new_x = next_x + it.x;
		int new_y = next_y + it.y;
		if(new_x >= 0 && new_x < h && new_y >= 0 && new_y < w && mat[new_x][new_y] == '.'){
			if(vis[new_x][new_y] == false){
				dfs(new_x , new_y , cnt);
			}
		}
	}
	
	
}

vector<int> adj[nax*nax];
vector<int> dis;
vector<bool> new_vis;

signed main() {    
	ios::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	
	
	cin >> h >> w;
	cin >> start_x >> start_y;
	cin >> end_x >> end_y;
	
	for(int i =0 ; i < h ; i++){
		for(int j =0 ; j < w ; j++){
			cin >> mat[i][j];
			vis[i][j] = false;
		}
	}
	
	int cnt = 1;
	for(int i = 0; i < h ; i++){
		for(int j = 0; j < w ; j++){
			if(vis[i][j] == false && mat[i][j] == '.'){
				dfs(i , j , cnt);
				cnt += 1;
			}
		}
	}
	
	//debug(cnt);
	
	set<pair<int,int>> edges;
	
	for(int i = 0; i < h ; i++){
		for(int j = 0; j < w ; j++){
			if(mat[i][j] == '.'){
				for(int k = -2 ; k <= 2; k++){
					for(int p = -2 ; p <= 2; p++){
						int new_x = i+k;
						int new_y = j+p;
						if(new_x >= 0 && new_x < h && new_y >= 0 && new_y < w && mat[new_x][new_y] == '.'){
							if(belongs[{new_x , new_y}] != belongs[{i,j}]){
								edges.insert({belongs[{new_x , new_y}] , belongs[{i,j}]});
							}
						}
					}
				}
			}
		}
	}
	
	
	//debug(edges);
	for(auto it : edges){
		int u = it.x , v = it.y;
		adj[u].pb(v);
		//adj[v].pb(u);
	}
	
	dis.assign(cnt , -1);
	new_vis.assign(cnt , false);
	
	
	queue<int> q;
	int curr = belongs[{start_x-1,  start_y-1}];
	new_vis[curr] = true;
	dis[curr] = 0;
	q.push(curr);
	//debug(curr);
	while(!q.empty()){
		int next = q.front();
		q.pop();
		for(auto it : adj[next]){
			if(new_vis[it] == false){
				dis[it] = dis[next] + 1;
				new_vis[it] = true;
				q.push(it);
			}
		}
	}
	
	
	
	cout<<dis[belongs[{end_x-1 , end_y-1}]];
	
	 
	return 0;
}


Measured memory_bytes: 88732