#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required

using namespace __gnu_pbds; 
using namespace std;

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; 
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
typedef long long  ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll,ll> PLL;
typedef pair<int,int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(int i=x;i<y;i++)
#define frr(i,x,y) for(int i=x-1;i>=y;i--)
#define inf 1e18+1
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int mod=1000000007;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;}
ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
const int N=1005;
vector<string> arr;

vector<pair<PII, int>> adj[N][N];

void dijkstra(int sx,int sy, vector<VL> & d, vector<VL> & p) {
    d.assign(N,VL(N,inf));
    //p.assign(N,VL(N,-1));

    d[sx][sy] = 0;
    set<pair<int, PII>> q;
    q.insert({0, {sx,sy}});
    while (!q.empty()) {
        PII v = q.begin()->second;
        q.erase(q.begin());

        for (auto edge : adj[v.F][v.S]) {
            PII to = edge.first;
            int len = edge.second;

            if (d[v.F][v.S] + len < d[to.F][to.S]) {
                q.erase({d[to.F][to.S], to});
                d[to.F][to.S] = d[v.F][v.S] + len;
                //p[to] = v;
                q.insert({d[to.F][to.S], to});
            }
        }
    }
}
void solve()
{
    ll r,c;
    cin>>r>>c;
    ll sx,sy,tx,ty;
    cin>>sx>>sy>>tx>>ty;
    arr.assign(r,"");
    fr(i,0,r)
        cin>>arr[i];
    fr(i,0,r)
    {
        fr(j,0,c)
        {
            if(i-1>=0&&arr[i-1][j]!='#')
            {
                adj[i][j].pb({{i-1,j},0});
            }
            if(i+1<r&&arr[i+1][j]!='#')
            {
                adj[i][j].pb({{i+1,j},0});
            }
            if(j-1>=0&&arr[i][j-1]!='#')
            {
                adj[i][j].pb({{i,j-1},0});
            }
            if(j+1<c&&arr[i][j+1]!='#')
            {
                adj[i][j].pb({{i,j+1},0});
            }
            ll p=i-2,v=j-2,a=i+2,b=j+2;
            fr(x,p,a+1)
            {
                fr(y,v,b+1)
                {
                    if(x>=0&&x<r&&y>=0&&y<c)
                    {
                        if(x==i&&y==j)
                        {
                            continue;
                        }
                        else
                        {
                            if(x==i||y==j)
                            {
                                if(x==i)
                                {
                                    if(abs(y-j)<=1)
                                        continue;
                                }
                                if(y==j)
                                {
                                    if(abs(x-i)<=1)
                                        continue;
                                }
                            }
                            else
                            {

                            }
                        }
                        if(arr[x][y]=='#')
                            continue;
                        adj[i][j].pb({{x,y},1});
                    }
                }
            }
        }
    }
    vector<VL> d,p;
    dijkstra(sx-1,sy-1,d,p);
    if(d[tx-1][ty-1]==inf)
        cout<<"-1\n";
    else
        cout<<d[tx-1][ty-1]<<endl;
    return;
}
int main()
{
    IOS;
    ll t=1;
    //cin>>t;
    while(t--)
    {
        //cout<<"Case #"<<pp-t<<":\n";
        solve();
    }
    return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
*/
/*
recursion - matrix exponential

*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm

Measured memory_bytes: 238088