#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define de(c) cout << #c << " = " << c << endl
#define dd(c) cout << #c << " = " << c << " "
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
mt19937 mrand(random_device{}());
const ll mod=100000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(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 gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

const int maxn = 233333;
int T;
int n,m;
int sx,sy;
int ex,ey;
int ans;
int ma[2333][2333];
int dx[24] = {0,0,-1,1,0,0,2,-2,1,1,1,1,-1,-1,-1,-1,2,2,2,2,-2,-2,-2,-2};
int dy[24] = {1,-1,0,0,2,-2,0,0,1,2,-1,-2,1,2,-1,-2,1,2,-1,-2,1,2,-1,-2};
int vis[2333][2333];
int dp[2333][2333];

void  dfs(int x,int y,int cnt) {
    vis[x][y] = 1;
    if (dp[x][y] != -1 && dp[x][y] <= cnt) {
        return;
    }
    else {
        dp[x][y] = cnt;
    }
    if (x == ex && y == ey) {
        if (ans == -1) ans = cnt;
        ans = min(ans, cnt);
        return;
    }
    for (int i = 0; i < 24; i++) {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (xx < 0 || yy < 0 || xx >= n || yy >= m || ma[xx][yy] == 1 || vis[xx][yy] == 1) {
            continue;
        }
        if(i < 4)
            dfs(xx,yy,cnt);
        else
            dfs(xx,yy,cnt+1);
        vis[xx][yy] = 0;
    }
}

void bfs(int x,int y) {
    deque<pair<int, PII> > q;
    pair<int,PII> s;
    s.second.first = sx, s.second.second = sy;
    s.first = 0;
    q.push_front(s);
    while(!q.empty()) {
        auto tmp = q.front(); q.pop_front();
        x = tmp.second.first, y = tmp.second.second;
        int cnt = tmp.first;
        if (dp[x][y] != -1 && dp[x][y] < cnt) {
            continue;
        }
        else {
            dp[x][y] = cnt;
        }
        if (x == ex && y == ey) {
            if (ans == -1) ans = cnt;
            ans = min(ans, cnt);
            if (ans == 0)
                return;
        }
        for (int i = 0; i < 24; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(i < 4) {
                if (xx < 0 || yy < 0 || xx >= n || yy >= m || ma[xx][yy] == 1 || (dp[xx][yy] != -1 && dp[xx][yy] <= cnt)) {
                    continue;
                }
                pair<int,PII> tmp;
                tmp.second.first = xx, tmp.second.second = yy;
                tmp.first = cnt;
                q.push_front(tmp);
            }
            else {
                if (xx < 0 || yy < 0 || xx >= n || yy >= m || ma[xx][yy] == 1 || dp[xx][yy] != -1) {
                    continue;
                }
                pair<int,PII> tmp;
                tmp.second.first = xx, tmp.second.second = yy;
                tmp.first = cnt + 1;
                q.push_back(tmp);
            }
        }
    }
}
int main(int argc, char const *argv[])
{
#ifdef indiewar
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
//    cin >> T;
    T =1;
    while(T--)
    {
        string str;
        cin >> n >> m >> sx >> sy >> ex >>ey;
        sx--,sy--,ex--,ey--;
        for (int i = 0; i < n; i++) {
            cin >> str;
            for (int j = 0; j < m; j++) {
                if (str[j] == '.') {
                    ma[i][j] = 0;
                }
                else {
                    ma[i][j] = 1;
                }
            }
        }

        ans = -1;
        memset(vis,0,sizeof vis);
        memset(dp,-1,sizeof dp);
//        dfs(sx, sy, 0);
        bfs(sx,sy);
        cout << ans << endl;
    }
    return 0;
}

Measured memory_bytes: 54700