#include<bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include"custom_header.h"
#else
#define openfile 
#endif
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
#define ll long long
#define ld long double
#define fo(i,v,e) for(v=i;v<e;v++)
#define rfo(i,v,e) for(v=i;v>e;v--)
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
#define vl vector<ll>
#define sz(x) (ll)x.size()
#define vvl vector<vl>
#define vpl vector<pll>
const ll mod = 1000000007;
bool BFS(vector<set<ll>>&adj, int src, int dest, int v,vector<ll>&pred,vector<ll>&dist) 
{ 
    list<int> queue; 
    bool visited[v]; 
    for (int i = 0; i < v; i++) { 
        visited[i] = false; 
        dist[i] = INT_MAX; 
        pred[i] = -1; 
    } 
    visited[src] = true; 
    dist[src] = 0; 
    queue.push_back(src); 
   while (!queue.empty()) { 
        int u = queue.front(); 
        queue.pop_front(); 
        for (auto x:adj[u]) { 
            if (visited[x] == false) { 
                visited[x] = true; 
                dist[x] = dist[u] + 1; 
                pred[x] = u; 
                queue.push_back(x); 
                if (x == dest) 
                    return true; 
            } 
        } 
    }
    return false; 
} 
  
ll printShortestDistance(vector<set<ll>>&adj,int s, int dest, int v) 
{ 
   vl pred(v),dist(v); 
    if (BFS(adj, s, dest, v, pred, dist) == false) { 
        return -1; 
    } 
    vector<int> path; 
    int crawl = dest; 
    path.push_back(crawl); 
    while (pred[crawl] != -1) { 
        path.push_back(pred[crawl]); 
        crawl = pred[crawl]; 
    } 
   return dist[dest]; 
} 
bool valid(ll x,ll y,ll n,ll m)
{
    return (x>=0 and x<n and y>=0 and y<m);
}
void dfs(ll x,ll y,vvl &vis,vvl &cc,ll cn,ll n,ll m,vector<string>&mat)
{
    if(!valid(x,y,n,m) or vis[x][y])
    {
        return ;    
    }
    cc[x][y] = cn;
    vis[x][y] = 1;
    if(mat[x][y]=='#')
    {
        cc[x][y] = -1;
        return ;
    }
    dfs(x+1,y,vis,cc,cn,n,m,mat);
    dfs(x-1,y,vis,cc,cn,n,m,mat);
    dfs(x,y+1,vis,cc,cn,n,m,mat);
    dfs(x,y-1,vis,cc,cn,n,m,mat);
}
void solve()
{
    ll n,m,i,j,c[2],d[2],k,l;cin>>n>>m;
    cin>>c[0]>>c[1];c[0]--;c[1]--;
    cin>>d[0]>>d[1];d[0]--;d[1]--;
    vector<string>mat(n);
    vvl vis(n,vl(m,0));
    vvl cc(n,vl(m,-1));
    fo(0,i,n)
    {
        cin>>mat[i];
    }
    ll cn = 0;
    fo(0,i,n)
    {
        fo(0,j,m)
        {
            if(!vis[i][j])
            {
                if(mat[i][j]=='#')
                {
                    vis[i][j] = 1;
                    cc[i][j] = -1;
                }
                else
                {
                    dfs(i,j,vis,cc,cn,n,m,mat);
                    cn++;
                }
            }
        }
    }
    if(cc[c[0]][c[1]]==cc[d[0]][d[1]])
    {
        cout<<0<<endl;
        return;
    }
    vector<set<ll>> adj(cn);
    fo(0,i,n)
    {
        fo(0,j,m)
        {
            if(cc[i][j]!=-1)
            {
                fo(-2,k,3)
                {
                    fo(-2,l,3)
                    {
                        if(valid(i+k,j+l,n,m) and cc[i+k][j+l]!=-1 and cc[i+k][j+l]!=cc[i][j])
                        {
                            adj[cc[i+k][j+l]].insert(cc[i][j]);
                            adj[cc[i][j]].insert(cc[i+k][j+l]);
                        }
                    }
                }
            }
        }
    }
   cout<<printShortestDistance(adj,cc[c[0]][c[1]],cc[d[0]][d[1]],cn)<<endl;
}
int main()
{
    //openfile;fastio;
    ll T = 1; 
    // cin>>T;
    while(T--)
    {solve();}
}

Measured memory_bytes: 48184