// Problem : D - Wizard in Maze
// Contest : AtCoder - AtCoder Beginner Contest 176
// URL : https://atcoder.jp/contests/abc176/tasks/abc176_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define vll vector<ll> 
#define all(x) (x).begin() , (x).end()
#define inf 1000000000
#define mod 1000000007

void dbg(){
	cerr << endl;
}
template<typename Head , typename... Tail>
void dbg(Head h , Tail... t){
	cerr << h << " ";
	dbg(t...);
}

#ifdef EMBI_DEBUG
#define debug(...) cerr << "(" << #__VA_ARGS__  << "): ", dbg(__VA_ARGS__)
#else 
#define debug(...)
#endif

const int max_n = 1e5 + 9;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
ll power(ll a , ll b)
{
    ll prod = 1;
    while(b)
    {
        if(b&1)
        prod = (prod*a)%mod;
        a = (a*a)%mod;
        b >>= 1;
    }
    return prod;
}
int n, m;
vector<string> arr;
pair<int,int> start , dest;
void dfs(vector<vector<int>>& visited , int i , int j , int cnt){
	if(visited[i][j])return;
	visited[i][j] = cnt;
	
	if(i-1 >= 0 && arr[i-1][j] == '.')dfs(visited,  i-1 , j , cnt);
	if(i+1 < n && arr[i+1][j] == '.')dfs(visited,  i+1 , j , cnt);
	if(j-1 >= 0 && arr[i][j-1] == '.')dfs(visited,  i , j-1 , cnt);
	if(j+1 < m && arr[i][j+1] == '.')dfs(visited,  i , j+1 , cnt);
}
vector<vector<int>> a;
void bfs(vector<int>& visited1 , int s , int d , int&ans){
	queue<int> q;
	vector<int> dist(visited1.size() , inf);
	dist[s] = 0;
	visited1[s] = 1;
	q.push(s);
	while(!q.empty()){
		int x = q.front();
		q.pop();
		
		for(auto it : a[x]){
			if(visited1[it])continue;
			visited1[it] = 1;
			dist[it] = dist[x] + 1;
			q.push(it);
		}
	}
	
	ans = dist[d];
}
void solve(){
	cin >> n >> m;
	arr.resize(n);
	
	cin >> start.ff >> start.ss >> dest.ff >> dest.ss;
	start.ff-- , start.ss-- , dest.ff-- , dest.ss--;
	
	for(auto &it : arr)cin >> it;
	int cnt = 1;
	vector<vector<int>> visited(n , vector<int>(m ,0));
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < m ; j++){
			if(!visited[i][j] && arr[i][j] == '.'){
				dfs(visited , i , j , cnt);
				cnt++;
			}
		}
	}
	
	set<pair<int,int>> s;
	
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < m ; j++){
			if(arr[i][j] == '.'){
				for(int x = max(i-2 , 0) ; x <= min(i + 2 , n-1) ; x++){
					for(int y = max(j-2 , 0) ; y <= min(j + 2 , m-1) ; y++){
						if(arr[x][y] == '.' && visited[x][y] != visited[i][j]){
							s.insert({visited[x][y] , visited[i][j]});
							s.insert({visited[i][j] , visited[x][y]});
						}
					}
				}
			}
		}
	}
	// cout << cnt << "\n";
	a.resize(cnt);
	for(auto it : s){
		a[it.ff].pb(it.ss);
	}
	
	// for(int i = 1 ; i < cnt ; i++){
		// debug(i);
		// for(auto it : a[i])cerr << it << " ";
		// cerr << '\n';
	// }
	int ans = 0;
	vector<int> visited1(cnt , 0);
	bfs(visited1 , visited[start.ff][start.ss] , visited[dest.ff][dest.ss], ans);
	
	if(visited1[visited[dest.ff][dest.ss]])
	cout << ans << "\n";
	else cout << -1 << "\n";
}
signed main(){
	int t = 1;
	// cin >> t;
	while(t--){
		solve();
	}
}

Measured memory_bytes: 35360