// 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>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

//Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int,int>
#define mii map<pii,int>
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define inf 1e10
#define PI acos(-1.0)
#define int long long
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 1005

//Solution

int n,m;
int sx,sy,fx,fy;
char a[N][N];
int comp[N][N];
int cur = 1;
vector<set<int>> g;

bool valid(int x, int y){
    return x >= 0 && x < n && y >= 0 && y < m;
}

int dx[] = {0,1,0,-1,1,1,-1,-1};
int dy[] = {1,0,-1,0,1,-1,1,-1};

void dfs(int x, int y){
    comp[x][y] = cur;
    for(int i=0;i<4;i++){
        int nx = x+dx[i];
        int ny = y+dy[i];
        if(!valid(nx,ny) || a[nx][ny] == '#' || comp[nx][ny] != -1) continue;
        dfs(nx,ny);
    }
}

void dijkstra(ll *dis, int src){
    for(int i=1;i<cur;i++) dis[i] = inf;
    priority_queue<pii,vii,greater<pii>> pq;
    dis[src] = 0;
    pq.push({0,src});
    while(!pq.empty()){
        int u = pq.top().se;
        int d = pq.top().fi;
        pq.pop();
        if(dis[u] != d) continue;
        for(int i : g[u]){
            int v = i, w = 1;
            if(dis[v] > dis[u]+w){
                dis[v] = dis[u]+w;
                pq.push({dis[v],v});
            }
        }
    }
}

void solve(){
    cin>>n>>m;
    cin>>sx>>sy; sx--; sy--;
    cin>>fx>>fy; fx--; fy--;
    for(int i=0;i<n;i++){
        string s; cin>>s;
        for(int j=0;j<m;j++) a[i][j] = s[j];
    }
    memset(comp,-1,sizeof(comp));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(~comp[i][j] || a[i][j] == '#') continue;
            dfs(i,j); cur++;
        }
    }
    g.resize(cur);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(a[i][j] == '#') continue;
            for(int k=i-2;k<=i+2;k++){
                for(int l=j-2;l<=j+2;l++){
                    if(i == k && j == l) continue;
                    if(!valid(k,l) || a[k][l] == '#') continue;
                    if(comp[i][j] == comp[k][l]) continue;
                    g[comp[i][j]].insert(comp[k][l]);
                }
            }
        }
    }
    int d[cur+1];
    dijkstra(d,comp[sx][sy]);
    if(d[comp[fx][fy]] == inf) cout<<-1;
    else cout<<d[comp[fx][fy]];
}

signed main(){
    io;
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int tests;
    //cin>>tests;
    tests = 1;
    while(tests--){
        solve();
    }
    return 0;
}

Measured memory_bytes: 37168