/*
  AUTHOR:SOURABH
  CREATED:21:08:20
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<pll , null_type, less< pll >, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define W while
#define fn(i,x) for(i=0;i<x;i++)
#define fs(i,s,x) for(i=s;i<x;i++)
#define fr(i,x) for(i=x;i>=0;i--)
#define fit(it,s) for(it=s.begin();it!=s.end();it++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define mod 100000000
#define MAX 100005
#define M 32

using namespace std;
using namespace __gnu_pbds;

string ss[1000];
ll dp[1000][1000],s,e,xx,yy,n,m;

void dfs(ll x,ll y,ll p)
{
    dp[x][y]=p;
    if(x+1<n && dp[x+1][y]==-1 && ss[x+1][y]=='.')
    {
        dfs(x+1,y,p);
    }
    if(y+1<m && dp[x][y+1]==-1 && ss[x][y+1]=='.')
    {
        dfs(x,y+1,p);
    }
    if(x-1>=0 && dp[x-1][y]==-1 && ss[x-1][y]=='.')
    {
        dfs(x-1,y,p);
    }
    if(y-1>=0 && dp[x][y-1]==-1 && ss[x][y-1]=='.')
    {
        dfs(x,y-1,p);
    }
}

vector<set<ll> > adj(1000001);
vll dis(1000001,1e18);
bool vis[1000001];

void bfs(ll root)
{
    ll i,j,k,start;
    queue<ll> q;
    q.push(root);
    vis[root]=true;
    dis[root]=0;
    set<ll>::iterator it;
    W(!q.empty())
    {
        start=q.front();
        q.pop();
        //k=adj[start].size();
        fit(it,adj[start])
        {
            j=*it;
            if(!vis[j])
            {
                vis[j]=true;
                dis[j]=dis[start]+1;
                q.push(j);
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    //freopen("input.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    ll t,i,j,p,src,des,x,y;
    t=1;
    //cin>>t;
    W(t--)
    {
        cin>>n>>m;
        cin>>s>>e>>xx>>yy;
        fn(i,n)
        cin>>ss[i];
        s--,e--,xx--,yy--;
        memset(dp,-1,sizeof(dp));
        p=1;
        fn(i,n)
        {
            fn(j,m)
            {
                if(ss[i][j]=='.' && dp[i][j]==-1)
                {
                    dfs(i,j,p);
                    p++;
                }
            }
        }
        src=dp[s][e];
        des=dp[xx][yy];
        /*cout<<src<<" "<<des<<" "<<p<<endl;
        fn(i,n)
        {
            fn(j,m)
            cout<<dp[i][j]<<" ";
            cout<<endl;
        }*/
        fn(x,n)
        {
            fn(y,m)
            {
                if(ss[x][y]=='#')
                    continue;
                ll cx=x-2,cy=y-2;
                fn(i,5)
                {
                    fn(j,5)
                    {
                        if(cx+i>=0 && cx+i<n && cy+j>=0 && cy+j<m && ss[cx+i][cy+j]=='.' && dp[cx+i][cy+j]!=dp[x][y])
                        {
                            adj[dp[x][y]].insert(dp[cx+i][cy+j]);
                            adj[dp[cx+i][cy+j]].insert(dp[x][y]);
                        }
                    }
                }
            }
        }

        bfs(src);
        if(dis[des]==1e18)
            dis[des]=-1;
        cout<<dis[des];
    }
    return 0;
}














Measured memory_bytes: 85496