#include<bits/stdc++.h>
using namespace std;
#define ld long double
#define lli long long int
#define vi vector<int>
#define vlli vector<lli>
#define vvi vector<vi >
#define str string
#define vs vector<str>
#define vb vector<bool>
#define slli set<lli>
#define mii map<int, int>
#define mlli map<int, lli>
#define msi map<str, int>
#define mp make_pair
#define pii pair<int, int>
#define pll pair<lli, lli>
#define vpii vector<pii >
#define vpll vector<pll >
#define pb push_back
#define loop(i, s, l, j) for(int i = s; i < l; i += j)
#define rloop(i, s, l, j) for(int i = s; i >= l; i -= j)
#define rep(i, n) loop(i,0,n,1)
#define rrep(i, n) rloop(i,n-1,0,1) 
#define fastio() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ft first
#define sd second
#define all(a) a.begin(),a.end()
#define rev(a) reverse(all(a))
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define mod 1000000007
#define inf 100000000
#define limit 200005
#define minval -100000000000000000
#define maxval 1000000000000000
/**********************************************************************************************************************************************/
int dx4[4] = {0, -1, 0, 1};
int dy4[4] = {1, 0, -1, 0};
int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int Power(int base,int exp){int ans=1;  while(exp > 0){if(exp&1)ans=(1LL*ans*base)%mod; exp=exp>>1; base=(1LL*base*base)%mod;}  return ans;}
/**********************************************************************************************************************************************/ 
void input()
{
    int n, m, cx, cy, desx, desy;
    cin >> n >> m >> cx >> cy >> desx >> desy;

    vs grid(n);
    rep(i,n)
        cin >> grid[i];
    cx--, cy--, desx--, desy--;

    int d[n][m];
    rep(i,n)
        rep(j,m)
            d[i][j] = 50000;

    int mark[n][m];
    rep(i,n)
        rep(j,m)
            mark[i][j] = 0;

    set<pair<int, pair<int, int>>> s;

    s.insert(mp(0, mp(cx,cy)));
    while(!s.empty())
    {
        int x = s.begin()->sd.ft;
        int y = s.begin()->sd.sd;
        int dxy = s.begin()->ft;

        s.erase(s.begin());

        if(mark[x][y])
            continue;

        mark[x][y] = 1;
        d[x][y] = dxy;

        if(x == desx && y == desy)
            break;

        rep(k,4)
        {
            int x_ = x+dx4[k];
            int y_ = y+dy4[k];
            if(x_ >= 0 && y_ >= 0 && x_ < n && y_ < m && grid[x_][y_] == '.')
                if(!mark[x_][y_])
                    s.insert(mp(dxy, mp(x_,y_)));
        }
        loop(i,-2,3,1)
            loop(j,-2,3,1)
            {
                int x_ = x+i;
                int y_ = y+j;
                if(x_ >= 0 && y_ >= 0 && x_ < n && y_ < m && grid[x_][y_] == '.')
                    if(!mark[x_][y_])
                        s.insert(mp(1+dxy, mp(x_,y_)));                
            }
    }
    cout << (d[desx][desy]==50000?-1:d[desx][desy]) << '\n';
}
int main()
{
   /*
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   */
    fastio();
    int q = 1;
    //cin >> q;
    while(q--)
        input();
}


Measured memory_bytes: 13388