//By TheOneYouWant
#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(),x.end()
// #define int unsigned long long int
#define memreset(a) memset(a,0,sizeof(a))
#define testcase(t) int t;cin>>t;while(t--)
#define forstl(i,v) for(auto &i: v)
#define forn(i,e) for(int i=0;i<e;++i)
#define forsn(i,s,e) for(int i=s;i<e;++i)
#define rforn(i,s) for(int i=s;i>=0;--i)
#define rforsn(i,s,e) for(int i=s;i>=e;--i)
#define bitcount(a) __builtin_popcount(a) // set bits (add ll)
#define ln '\n'
#define getcurrtime() cerr<<"Time = "<<((double)clock()/CLOCKS_PER_SEC)<<endl
#define dbgarr(v,s,e) cerr<<#v<<" = "; forsn(i,s,e) cerr<<v[i]<<", "; cerr<<endl
#define inputfile freopen("input.txt", "r", stdin)
#define outputfile freopen("output.txt", "w", stdout)
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef pair<int,p32> p96;
typedef vector<ll> v64;
typedef vector<int> v32; 
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;
const int mul1 = 1003, mul2 = 5, mul3 = 8;
const int MOD3 = 179424673, MOD2 = 982451653, MOD1 = 1190494759;
const ld EPS = 1e-9;
#define INF 0x3f3f3f3f


int h,w;
int ch, cw, dh, dw;
vv64 exists;
vv32 skipyy;

vector<string> maze;


vp32 get_55_pos(int h_i, int w_i)
{

    vp32 points;
    forsn(x, h_i-2, h_i+3)
    {
        if (x < 0 || x >= h) continue;
        forsn(y, w_i-2, w_i+3)
        {
            if (y < 0 || y >= w) continue;

            if (x == h_i && y == w_i) continue;

            if (maze[x][y] == '#') continue;

            points.pb(mp(x, y));
            
        }
    }

    return points;
}

vp32 get_11_pos(int h_i, int w_i)
{

    vp32 points;
    forsn(x, h_i-1, h_i+2)
    {
        if (x < 0 || x >= h) continue;

        if (x == h_i) continue;

        if (maze[x][w_i] == '#') continue;

        points.pb(mp(x, w_i));
        
    }

    forsn(y, w_i-1, w_i+2)
    {
        if (y < 0 || y >= w) continue;

        if (y == w_i) continue;

        if (maze[h_i][y] == '#') continue;

        points.pb(mp(h_i, y));
        
    }

    return points;
}

int main()
{
    cin >> h >> w;
    cin >> ch >> cw;
    cin >> dh >> dw;

    ch--;
    cw--;
    dh--;
    dw--;

    forn(i, h)
    {
        string tmp;
        cin >> tmp;
        maze.pb(tmp);
    }

    // cout << "kuy" << endl;

    forn(i, h)
    {
        //  cout << "yeah";
        v64 tmp_v;
        v32 tmp_s;
        // cout << "yeah^^^";
        forn(j,w)
        {
            tmp_v.pb(INF);
            tmp_s.pb(0);

        }
        exists.pb(tmp_v);
        skipyy.pb(tmp_s);
    }

    // cout << "kuy2" << endl;

    queue<p32> nb;
    exists[ch][cw] = 0;
    nb.push(mp(ch, cw));
    while(!nb.empty())
    {
        // cout << "kuy3" << endl;
        // cout << "yooo" << endl;
        p32 cp = nb.front();
        int h_c = cp.first;
        int w_c = cp.second;

        nb.pop();

        if (skipyy[h_c][w_c]) continue;
        // Search walkable
        queue<p32> nb_n;
        queue<p32> nb_m;
        nb_n.push(cp);
        nb_m.push(cp);

        skipyy[h_c][w_c] = 1;

        while (!nb_n.empty())
        {
            p32 cp = nb_n.front();
            int h_c = cp.first;
            int w_c = cp.second;
            nb_n.pop();
            vp32 nebor1 = get_11_pos(h_c, w_c);
            // cout << "kuy4" << endl;
            forstl(pnb, nebor1)
            {
                int h_nb = pnb.first;
                int w_nb = pnb.second;
                
                if (!skipyy[h_nb][w_nb])
                {
                    exists[h_nb][w_nb] = exists[h_c][w_c];
                    nb_n.push(pnb);
                    nb_m.push(pnb);
                    skipyy[h_nb][w_nb] = 1;
                }
                
            }
        }

        while (!nb_m.empty())
        {
            p32 cp = nb_m.front();
            int h_c = cp.first;
            int w_c = cp.second;
            nb_m.pop();
            vp32 nebor = get_55_pos(h_c, w_c);
            // cout << "kuy5" << endl;
            int tmp = exists[h_c][w_c] + 1;
            forstl(pnb, nebor)
            {
                int h_nb = pnb.first;
                int w_nb = pnb.second;
                if (tmp < exists[h_nb][w_nb])
                {
                    exists[h_nb][w_nb] = tmp;
                    nb.push(pnb);
                }
            }
        }
    }

    if (exists[dh][dw] != INF)
        cout << exists[dh][dw] << endl;
    else
    {
        cout << -1 << endl;
    }
    
    return 0;

}

Measured memory_bytes: 16456