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

/* // Ordered Set
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)
*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define f0(i,a,b) for(int i=a;i<b;i++)
#define f1(i,a,b) for(int i=a;i<=b;i++)
#define f2(i,a,b) for(int i=a;i>b;i--)
#define f3(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define fi first
#define se second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define modInv(a) binExp(a,mod-2)
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)

int h,w;
char maze[1001][1001];
int dist[10001][1001];
bool vis[1001][1001];

void bfs(int,int);

signed main()
{
    fast

    #ifndef ONLINE_JUDGE
        freopen("inputf.in","r",stdin);
        freopen("outputf.in","w",stdout);
    #endif

    cin>>h>>w;

    int ch,cw,dh,dw;
    cin>>ch>>cw>>dh>>dw;

    f0(i,0,h) f0(j,0,w) cin>>maze[i][j],dist[i][j] = mod, vis[i][j] = 0;

    bfs(ch-1,cw-1);
    // f0(i,0,h)
    // {
    //     f0(j,0,w)
    //     {
    //         cout<<dist[i][j]<<' ';
    //     }
    //     cout<<'\n';
    // }

    cout<<(dist[dh-1][dw-1] == mod?-1:dist[dh-1][dw-1])<<'\n';
}

void bfs(int a, int b)
{
    dist[a][b] = 0;
    multiset<pair<int,pii> > pq;
    pq.insert({0, {a,b}});
    while(!pq.empty())
    {
        auto x = *pq.begin();
        pq.erase(pq.begin());

        int r = x.se.fi;
        int c = x.se.se;
        //cout<<x.fi<<' '<<r<<' '<<c<<'\n';

        if(vis[r][c]) continue;

        vis[r][c] = 1;

        if(r>0 && maze[r-1][c]=='.')
        {
            if(dist[r-1][c] > dist[r][c])
            {
                dist[r-1][c] = dist[r][c];
                pq.insert({dist[r][c], {r-1,c}});
            }
            
        }
        if(r<h-1 && maze[r+1][c]=='.')
        {
            if(dist[r+1][c] > dist[r][c])
            {
                dist[r+1][c] = dist[r][c];
                pq.insert({dist[r][c], {r+1,c}});
            }
            
        }
        if(c>0 && maze[r][c-1]=='.')
        {
            if(dist[r][c-1] > dist[r][c])
            {
                dist[r][c-1] = dist[r][c];
                pq.insert({dist[r][c], {r,c-1}});
            }
            
        }
        if(c<w-1 && maze[r][c+1]=='.')
        {
            if(dist[r][c+1] > dist[r][c])
            {
                dist[r][c+1] = dist[r][c];
                pq.insert({dist[r][c], {r,c+1}});
            }
        }

        f1(i,r-2,r+2)
        {
            f1(j,c-2,c+2)
            {
                if(i == r && j == c) continue;
                if(i >= 0 && i < h && j >= 0 && j < w)
                {
                    if(dist[i][j] > dist[r][c]+1 && maze[i][j] == '.')
                    {
                        dist[i][j] = dist[r][c]+1;
                        pq.insert({dist[r][c]+1, {i,j}});
                    }
                }
            }
        }
    }
}

Measured memory_bytes: 14032