// D - Wizard in Maze
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
#define rp(i,s,e) for(int i=(int)(s);i<(int)(e);++i)

pair<int, int> dhw[] = { // <h, w>
        { 1, 0},
{ 0,-1},        { 0, 1},
        {-1, 0},
{ 2,-2},{ 2,-1},{ 2, 0},{ 2, 1},{ 2, 2},
{ 1,-2},{ 1,-1},        { 1, 1},{ 1, 2},
{ 0,-2},                        { 0, 2},
{-1,-2},{-1,-1},        {-1, 1},{-1, 2},
{-2,-2},{-2,-1},{-2, 0},{-2, 1},{-2, 2}
};

int main(){
	int H, W; cin>>H>>W;
	int C[2],D[2]; cin>>C[0]>>C[1]>>D[0]>>D[1];
	C[0]--, C[1]--, D[0]--, D[1]--;
	string S[H]; rp(i, 0, H) cin>>S[i];

	vector<vi> dst(H, vi(W, -1));
	deque<tuple<int, int, int>> dq; // <h, w, gid>
	dq.push_back({C[0], C[1], 0});
	while(dq.size()){
		int h, w, gid; tie(h, w, gid) = dq.front(); dq.pop_front();
		if(h < 0 || H <= h || w < 0 || W <= w
		|| dst[h][w] >= 0 || S[h][w] == '#') continue;
		dst[h][w] = gid;
		rp(i, 0, 4) dq.push_front({h + dhw[i].first, w + dhw[i].second, gid});
		rp(i, 4, 24) dq.push_back({h + dhw[i].first, w + dhw[i].second, gid+1});
	}

	cout<< dst[D[0]][D[1]] <<endl;
}
