#include "bits/stdc++.h"
#include <chrono>
#include <random>
#include <cstdio>
#define ll long long int
#define endl '\n'
#define pb push_back
#define mp make_pair
// #define mod 998244353
#define rep(i,n) for(ll i=0;i<n;i++)
#define vi vector <int>
#define vs vector <string>
#define vc vector <char>
#define vl vector <ll>
#define pll pair<ll, ll>
#define pint pair<int, int>
#define S second
#define F first
#define all(c) (c).begin(),(c).end()
using namespace std;
// ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// ll modInverse(ll a){return power(a,mod-2);}
const int N=1000001;
// vector<pair<ll, ll> > adj[N];
// bool vis[N];
ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } 
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
#define pi 3.14159265358979323846264338
using namespace std; 
ll max(ll a, ll b){ if(a>b)return a;return b;}
ll min(ll a, ll b){ if(a < b) return a;return b;}
ll inf = 100000000000000000;
vector <int> adj[N];
string s[1005];
int h, w;
int ch, cw;
int dh, dw;
int c = 0;
bool vis[1005][1005];
int comp[1005][1005];

void dfs(int a, int b)
{
    comp[a][b] = c;
    vis[a][b] = true;

    if(s[a + 1][b] == '.')
    {
        if(vis[a+1][b] == false)
        {
            dfs(a + 1, b);
        }
    }

    if(s[a - 1][b] == '.')
    {
        if(vis[a - 1][b] == false)
        {
            dfs(a - 1, b);
        }
    }

    if(s[a][b + 1] == '.')
    {
        if(vis[a][b + 1] == false)
        {
            dfs(a, b + 1);
        }
    }

    if(s[a][b - 1] == '.')
    {
        if(vis[a][b-1] == false)
        {
            dfs(a, b-1);
        }
    }
    
}


void solve() 
{

    cin>>h>>w;
    
    cin>>ch>>cw;
    
    cin>>dh>>dw;

    rep(i, 1005)
    {
        rep(j, 1005)
        {
            vis[i][j] = false;
        }
    }

    s[0] = "";
    rep(i, 1005)
    {
        s[0].pb('#');
    }

    for(int i = 1; i <= h; i++)
    {
        cin>>s[i];
        s[i] = '#' + s[i];
    }

    for(int i = 1; i <= h; i++)
    {
        s[i].pb('#');
    }

    s[h + 1] = "";
    rep(i, 1005)
    {
        s[h+1].pb('#');
    }

    for(int i = 1; i <= h; i++)
    {
        for(int j = 1; j <= w; j++)
        {
            comp[i][j] = -1;
        }
        // cout<<endl;
    }

    // for(int i = 0; i <= h + 1; i++)
    // {
    //     for(int j = 0; j <= w + 1; j++)
    //     {
    //         cout<<s[i][j];
    //     }
    //     cout<<endl;
    // }
    

    for(int i = 1; i <= h; i++)
    {
        for(int j = 1; j <= w; j++)
        {
            if(vis[i][j] == false && s[i][j] == '.')
            {
                c++;
                dfs(i, j);
            }
        }
    }


    for(int i = 1; i <= h; i++)
    {
        for(int j = 1; j <= w; j++)
        {
            int a = comp[i][j];
            int b;

            for(int x = i - 2; x <= i + 2; x++)
            {
                for(int y = j - 2; y <= j + 2; y++)
                {
                    if(x>=1 && x <=h && y >= 1 && y <= w)
                    {
                        b = comp[x][y];

                        if(a != b && a!=-1 && b!=-1)
                        {
                            adj[a].pb(b);
                            adj[b].pb(a);
                        }
                    }
                }
            }
        }
    }

    queue<int> q;

    ll dist[c + 5];

    rep(i, c + 5)
    {
        dist[i] = LLONG_MAX;
    }

    // for(int i = 1; i <= c; i++)
    // {
    //     for(auto v:adj[i])
    //     {
    //         cout<<v<<" ";
    //     }
    //     cout<<endl;
    // }
    dist[comp[ch][cw]] = 0;

    q.push(comp[ch][cw]);
    // cout<<comp[dh][dw]<<endl;

    while(q.size())
    {
        int p = q.front();
        q.pop();

        for(auto v:adj[p])
        {
            if(dist[v] > dist[p] + 1)
            {
                dist[v] = dist[p] + 1;
                q.push(v);
            }
        }
    }

    if(dist[comp[dh][dw]] == LLONG_MAX)
    {
        cout<<-1<<endl;
    }
    else
    {
        cout<<dist[comp[dh][dw]]<<endl;
    }





}

int main()
{
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T=1;
    // cin>>T;
    int t=0;
    while(t++<T)

    {
        // cout<<"Case #"<<t<<":"<<' ';
        solve();
        // cout<<'\n';
    }

    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}

Measured memory_bytes: 56564