#include <bits/stdc++.h>
#ifdef __DEBUG__
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[]={1, 0, -1, 0, 1,-1, 1,-1};
int dy[]={0, 1, 0, -1, 1, 1,-1,-1};
const int INT_INF = (int)(2e9);
const ll  LL_INF = (ll)(2e18);

inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const deque<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const set<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

int H,W;
int sx,sy,tx,ty;
vector<string> S;
void input(){
    fast_io();
    cin >> H >> W;
    cin >> sx >> sy;
    cin >> tx >> ty;
    for (int i = 0; i < H; i++)
    {
        string s; cin >> s;
        S.push_back(s);
    }
    sx--, sy--;
    tx--, ty--;
}

int dijkstra(int sx, int sy, int tx, int ty){
    auto cmp = [](pair<int,pii> a, pair<int,pii> b){
        return a.first > b.first;
    };
    priority_queue<pair<int,pii>, vector<pair<int,pii>>, decltype(cmp)> q(cmp);
    vector<vector<int>> dist(H,vector<int>(W,1e6));
    dist[sx][sy] = 0;
    q.emplace(0, make_pair(sx,sy));
    while (!q.empty())
    {
        int x,y,d;
        d = q.top().first;
        x = q.top().second.first;
        y = q.top().second.second;
        q.pop();
        if(x==tx && y==ty) return d;
        if(dist[x][y] < d) continue;
        dist[x][y] = d;
        for (int k = 0; k < 8; k++)
        {
            int nx = x + dx[k];
            int ny = y + dy[k];
            int nd = d;
            if(k>=4) nd++;
            if(nx < 0 || nx >= H) continue;
            if(ny < 0 || ny >= W) continue;
            if(S[nx][ny]=='#') continue;
            if(dist[nx][ny] <= nd) continue;
            dist[nx][ny] = nd;
            q.emplace(nd, make_pair(nx,ny));
        }

        for (int k = 0; k < 8; k++)
        {
            int nx = x + 2*dx[k];
            int ny = y + 2*dy[k];
            int nd = d+1;
            if(nx < 0 || nx >= H) continue;
            if(ny < 0 || ny >= W) continue;
            if(S[nx][ny]=='#') continue;
            if(dist[nx][ny] <= nd) continue;
            dist[nx][ny] = nd;
            q.emplace(nd, make_pair(nx,ny));
        }

        for (int k = 4; k < 8; k++)
        {
            int nx = x + dx[k];
            int ny = y + 2*dy[k];
            int nd = d+1;
            if(nx < 0 || nx >= H) continue;
            if(ny < 0 || ny >= W) continue;
            if(S[nx][ny]=='#') continue;
            if(dist[nx][ny] <= nd) continue;
            dist[nx][ny] = nd;
            q.emplace(nd, make_pair(nx,ny));
        }

        for (int k = 4; k < 8; k++)
        {
            int nx = x + 2*dx[k];
            int ny = y + dy[k];
            int nd = d+1;
            if(nx < 0 || nx >= H) continue;
            if(ny < 0 || ny >= W) continue;
            if(S[nx][ny]=='#') continue;
            if(dist[nx][ny] <= nd) continue;
            dist[nx][ny] = nd;
            q.emplace(nd, make_pair(nx,ny));
        }

    }
    // if(dist[tx][ty]!=(int)1e6) return dist[tx][ty];
    // else return -1;
    return -1;
}

int solve(){
    cout << dijkstra(sx,sy,tx,ty) << endl;
    return 0;
}   

int main()
{
    input();
    solve();
    return 0;
}

Measured memory_bytes: 8388