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

#define ll long long int
ll mod=1e9+7;

ll isvalid(ll x,ll y,ll n,ll m)
{
    if(x>=0 && x<n && y>=0 && y<m)
    {
        return 1;
    }

    return 0;
}

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


int main()
{
    /* Faster input ouput */
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    // cout.tie(NULL);

    /* To read input from a file and output to a file */
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    
    ll t = 1;
    // cin >> t;
    while(t--)
    {        
        ll n,i,j,m;
        cin >> n >> m;
        vector<vector<char>> grid(n,vector<char> (m));

        vector<vector<ll>> vis(n,vector<ll> (m,0));
        vector<vector<ll>> moves(n,vector<ll> (m,INT_MAX));

        pair<ll,ll> src;
        cin >> src.first >> src.second;
        src.first--;
        src.second--;

        ll sx = src.first;
        ll sy = src.second;

        pair<ll,ll> dest;
        cin >> dest.first >> dest.second;
        dest.first--;
        dest.second--;

        ll dsx = dest.first;
        ll dsy = dest.second;

        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin >> grid[i][j];
            }
        }

        queue<pair<ll,ll>> q;

        if(grid[sx][sy] == '#')
        {
            cout << -1 << '\n';
            return 0;
        }

        q.push(src);
        moves[sx][sy] = 0;
        queue<pair<ll,ll>> qtemp;

        while(!q.empty())
        {
            while(!q.empty())
            {
                pair<ll,ll> fr = q.front();
                q.pop();
                qtemp.push(fr);
                ll cx = fr.first;
                ll cy = fr.second;
                vis[fr.first][fr.second] = 1;
                
                for(i=0;i<4;i++)
                {
                    ll nx = cx + dx[i];
                    ll ny = cy + dy[i];

                    if(isvalid(nx,ny,n,m) && vis[nx][ny] == 0 && grid[nx][ny] == '.')
                    {
                        vis[nx][ny] = 1;
                        moves[nx][ny] = min(moves[cx][cy],moves[nx][ny]);
                        q.push(make_pair(nx,ny));
                    }
                }
            }

            // if(vis[dsx][dsy] == 1)
            // {
            //     cout << moves[dsx][dsy] << '\n';
            //     return 0;
            // }

            // while(!qtemp.empty())
            // {
            //     pair<ll,ll> fr = qtemp.front();
            //     cout << fr.first << " " << fr.second << '\n';
            //     qtemp.pop();
            // }

            while(!qtemp.empty())
            {
                pair<ll,ll> fr = qtemp.front();
                qtemp.pop();
                ll cx = fr.first;
                ll cy = fr.second;
                for(i=-2;i<=2;i++)
                {
                    for(j=-2;j<=2;j++)
                    {
                        ll nx = cx + i;
                        ll ny = cy + j;

                        if(isvalid(nx,ny,n,m) && vis[nx][ny]==0 && grid[nx][ny]=='.')
                        {
                            vis[nx][ny] = 1;
                            moves[nx][ny] = moves[cx][cy]+1;
                            // qtemp.push(make_pair(nx,ny));
                            q.push(make_pair(nx,ny));
                        }
                    }
                }
            }

            // while(!q.empty())
            // {
            //     pair<ll,ll> fr = q.front();
            //     cout << fr.first << " " << fr.second << '\n';
            //     q.pop();
            // }

        }


        // for(i=0;i<n;i++)
        // {
        //     for(j=0;j<m;j++)
        //     {
        //         if(moves[i][j] == INT_MAX)
        //         {
        //             cout << -1 << " "; 
        //         }
        //         else
        //         {
        //             cout << moves[i][j] << " ";
        //         }
        //     }
        //     cout << '\n';
        // }

        if(vis[dsx][dsy] == 1)
        {
            cout << moves[dsx][dsy] << '\n';
        }
        else
        {
            cout << -1 << '\n';
        }
        




    }
    
    return 0;
}

Measured memory_bytes: 20468