#include <bits/stdc++.h>
#include <vector>
#include<math.h>
#include<string.h>
using namespace std;
#define MAX 1003
#define MOD 998244353
#define INF 1000000000000000000
#define EPS 0.0000000001
#define CHAINS 18
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
char grid[MAX][MAX];
int dist[MAX][MAX];
int dijkstra(pair<int,int> c,pair<int,int> d,int H,int W)
{
    int i,j;
    for(i=0; i<H; i++)
    {
        for(j=0; j<W; j++)
        {
            dist[i][j]=MOD;
        }
    }
    priority_queue<pair<int,pair<int,int> > > pq;
    pair<int,int> node,child;
    pair<int,pair<int,int> > v;
    dist[c.first][c.second]=0;
    child=c;
    pq.push(make_pair(-dist[child.first][child.second],child));
    while(!pq.empty())
    {
        v=pq.top();
        pq.pop();
        node=v.second;
        if(dist[node.first][node.second]!=-v.first) continue;
        //printf("d[%d][%d]=%d\n",node.first,node.second,dist[node.first][node.second]);
        if(node==d) return dist[node.first][node.second];
        if(node.first-1>=0)
        {
            child=node;
            child.first--;
            if(grid[child.first][child.second]=='.'&&dist[child.first][child.second]>dist[node.first][node.second])
            {
                dist[child.first][child.second]=dist[node.first][node.second];
                pq.push(make_pair(-dist[child.first][child.second],child));
            }
        }
        if(node.second-1>=0)
        {
            child=node;
            child.second--;
            if(grid[child.first][child.second]=='.'&&dist[child.first][child.second]>dist[node.first][node.second])
            {
                dist[child.first][child.second]=dist[node.first][node.second];
                pq.push(make_pair(-dist[child.first][child.second],child));
            }
        }
        if(node.first+1<H)
        {
            child=node;
            child.first++;
            if(grid[child.first][child.second]=='.'&&dist[child.first][child.second]>dist[node.first][node.second])
            {
                dist[child.first][child.second]=dist[node.first][node.second];
                pq.push(make_pair(-dist[child.first][child.second],child));
            }
        }
        if(node.second+1<W)
        {
            child=node;
            child.second++;
            if(grid[child.first][child.second]=='.'&&dist[child.first][child.second]>dist[node.first][node.second])
            {
                dist[child.first][child.second]=dist[node.first][node.second];
                pq.push(make_pair(-dist[child.first][child.second],child));
            }
        }
        for(i=0; i<3; i++)
        {
            for(j=0; j<3; j++)
            {
                if(node.first+i<H)
                {
                    if(node.second+j<W)
                    {
                        child=node;
                        child.first+=i;
                        child.second+=j;
                        if(grid[child.first][child.second]=='.'&&dist[child.first][child.second]>dist[node.first][node.second]+1)
                        {
                            dist[child.first][child.second]=dist[node.first][node.second]+1;
                            pq.push(make_pair(-dist[child.first][child.second],child));
                        }
                    }
                    if(node.second-j>=0)
                    {
                        child=node;
                        child.first+=i;
                        child.second-=j;
                        if(grid[child.first][child.second]=='.'&&dist[child.first][child.second]>dist[node.first][node.second]+1)
                        {
                            dist[child.first][child.second]=dist[node.first][node.second]+1;
                            pq.push(make_pair(-dist[child.first][child.second],child));
                        }
                    }
                }
                if(node.first-i>=0)
                {
                    if(node.second+j<W)
                    {
                        child=node;
                        child.first-=i;
                        child.second+=j;
                        if(grid[child.first][child.second]=='.'&&dist[child.first][child.second]>dist[node.first][node.second]+1)
                        {
                            dist[child.first][child.second]=dist[node.first][node.second]+1;
                            pq.push(make_pair(-dist[child.first][child.second],child));
                        }
                    }
                    if(node.second-j>=0)
                    {
                        child=node;
                        child.first-=i;
                        child.second-=j;
                        if(grid[child.first][child.second]=='.'&&dist[child.first][child.second]>dist[node.first][node.second]+1)
                        {
                            dist[child.first][child.second]=dist[node.first][node.second]+1;
                            pq.push(make_pair(-dist[child.first][child.second],child));
                        }
                    }
                }
            }
        }
    }
    return dist[d.first][d.second];
}
int main()
{
    int H,W,Ch,Cw,Dh,Dw,i,j;
    scanf("%d %d",&H,&W);
    scanf("%d %d",&Ch,&Cw);
    scanf("%d %d",&Dh,&Dw);

    for(i=0; i<H; i++)
    {
        scanf(" ");
        for(j=0; j<W; j++)
        {
            scanf("%c",&grid[i][j]);
        }
    }
    int ans=dijkstra(make_pair(Ch-1,Cw-1),make_pair(Dh-1,Dw-1),H,W);
    if(ans>H*W)
    {
        printf("-1");
    }
    else
    {
        printf("%d",ans);
    }
    return 0;
}


Measured memory_bytes: 8804