#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pii;
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fo(i,a,b) for(ll i = a; i<=b;i+=1)
#define rf(i,a,b)  for(ll i=a;i>=b;i--)
#define vll vector<ll>
#define sz(a) ll(a.size())
#define all(v) v.begin(),v.end()
#define pb push_back
#define pf push_front 
#define MAXN 100010
#define N 100010
#define MOD 1000000007
#define mod 998244353
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define debug(x) cout << '>' << #x << ':' << x << " ";
#define nl '\n'
#define MS(x,y) fill_n(*x, sizeof x / sizeof **x, y);
#define mem(x,y) memset(x,y,sizeof(x));
#define INF 1000000000000000000
#define IO                 \
	ios::sync_with_stdio(0); \
	cin.tie(0);              \
	cout.tie(0);
#define int long long int

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// int dxx[] = {1, 0, -1, 0, 2, 2, 2, 1, 0}, dyy[] = {0, 1, 0, -1};

int h, w;
vector<vector<char> > arr(1010, vector<char>(1010, '*'));
vector<vector<bool> > vis(1010, vector<bool>(1010, false));
vector<vector<int> > dist(1010, vector<int>(1010, INF));



bool is_valid(int x, int y){
    if(x < h and x >= 0 and y < w and y >= 0 and arr[x][y] == '.'){
        return true;
    }
    return false;
}
bool is_not_valid(int x, int y){
    if(x < h and x >= 0 and y < w and y >= 0 and arr[x][y] == '#'){
        return true;
    }
    return false;
}
int dh, dw;
int bfs(int x, int y){
    // deb("AAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
    deque<pair<int, int> > que;
    // queue<int> d;

    dist[x][y] = 0;
    que.push_back({x, y});
    // d.push(dist[x][y]);
    // deb(dist[dh][dw])
    // deb("AAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
    while(!que.empty()){
        pair<int, int> p = que.front();
        que.pop_front();
        int a = p.first, b = p.second;
        // int distance = d.front();
        // d.pop();
        // if(dist[a][b] < distance)continue;
        bool go = false;
        for(int i = -1; i <= 1; i++){
            for(int j = -1; j <= 1; j++){
                if(abs(i) + abs(j) > 1)continue;
                if(is_valid(a+i, b+j) and dist[a+i][b+j] > dist[a][b]){
                    dist[a+i][b+j] = dist[a][b];
                    que.push_front({a+i, b+j});
                    // d.push(dist[a+i][b+j]);
                }
            }
        }
        for(int i = -2; i <= 2; i++){
            for(int j = -2; j <= 2; j++){
                if(abs(i) + abs(j) <= 1)continue;
                if(is_valid(a+i, b+j) and dist[a+i][b+j] > dist[a][b] + 1){
                    dist[a+i][b+j] = dist[a][b] + 1;
                    que.push_back({a+i, b+j});
                }
            }
        }
    }
    // deb(dist[dh][dw])
    return dist[dh][dw];
}

main()
{
    IO;
    // deb(0)
    cin >> h >> w;

    
    // arr.resize(h);
    // vis.resize(h);
    // dist.resize(h);

    int ch, cw;
    cin >> ch >> cw;
    ch--, cw--;
    
    cin >> dh >> dw;
    dh--, dw--;
    // deb(1)
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            char c;
            cin >> c;
            arr[i][j] = (c);
        }
    }
    int res = bfs(ch, cw);
    if(res == INF){
        cout << -1 << nl;
    }
    else cout << res;
    return 0;
}


Measured memory_bytes: 12976