#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repr(i,n) for(int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(),v.end()
typedef long long ll;

void bfs(vector<string> &maze, vector<vector<int> > &moving_count, int i, int j, int H, int W){
    vector<int> dx(4);
    dx[0] = 1;
    dx[1] = 0;
    dx[2] = -1;
    dx[3] = 0;
    vector<int> dy(4);
    dy[0] = 0;
    dy[1] = 1;
    dy[2] = 0;
    dy[3] = -1;
    vector<int> ex(20);
    ex[0] = 1;
    ex[1] = -1;
    ex[2] = -1;
    ex[3] = 1;
    ex[4] = 2;
    ex[5] = 1;
    ex[6] = 0;
    ex[7] = -1;
    ex[8] = -2;
    ex[9] = -2;
    ex[10] = -2;
    ex[11] = -2;
    ex[12] = -2;
    ex[13] = -1;
    ex[14] = 0;
    ex[15] = 1;
    ex[16] = 2;
    ex[17] = 2;
    ex[18] = 2;
    ex[19] = 2;
    vector<int> ey(20);
    ey[0] = 1;
    ey[1] = 1;
    ey[2] = -1;
    ey[3] = -1;
    ey[4] = 2;
    ey[5] = 2;
    ey[6] = 2;
    ey[7] = 2;
    ey[8] = 2;
    ey[9] = 1;
    ey[10] = 0;
    ey[11] = -1;
    ey[12] = -2;
    ey[13] = -2;
    ey[14] = -2;
    ey[15] = -2;
    ey[16] = -2;
    ey[17] = -1;
    ey[18] = 0;
    ey[19] = 1;
    queue<pair<int,int> > que1;
    queue<pair<int,int> > que2;
    que1.push(pair<int,int> (i,j));
    moving_count[i][j] = 0;
    while(true){
        if (que1.size() == 0 && que2.size() == 0){
            break;
        }
        while(que1.size() != 0){
            pair<int,int> p = que1.front();
            que1.pop();
            que2.push(p);
            rep(a,4){
                int nx = p.second + dx[a];
                int ny = p.first + dy[a];
                if (0 <= nx && nx < W && 0 <= ny && ny < H && maze[ny][nx] == '.'){
                    if (moving_count[ny][nx] == 10000000){
                        que1.push(pair<int,int> (ny, nx));
                        que2.push(pair<int,int> (ny, nx));
                    }
                    moving_count[ny][nx] = min(moving_count[ny][nx], moving_count[p.first][p.second]);
                }
            }
        }
        while(que2.size() != 0){
            pair<int,int> p = que2.front();
            que2.pop();
            rep(a,20){
                int nx = p.second + ex[a];
                int ny = p.first + ey[a];
                if (0 <= nx && nx < W && 0 <= ny && ny < H && maze[ny][nx] == '.'){
                    if (moving_count[ny][nx] == 10000000) que1.push(pair<int,int>(ny,nx));
                    moving_count[ny][nx] = min(moving_count[ny][nx], moving_count[p.first][p.second] + 1);
                }
            }
        }
        
        /*rep(k,moving_count.size()){
            rep(l,moving_count[k].size()){
                cout << moving_count[k][l] << " ";
            }
            cout << endl;
        }
        cout << endl;*/
    }
}
int main(){
    int H,W;
    cin >> H >> W;
    int ch, cw;
    cin >> ch >> cw;
    ch--;
    cw--;
    int dh, dw;
    cin >> dh >> dw;
    dh--;
    dw--;
    vector<string> maze(H);
    rep(i,H) cin >> maze[i];
    vector<vector<int> > moving_count(H, vector<int> (W));
    rep(i,H){
        rep(j,W){
            moving_count[i][j] = 10000000;
        }
    }
    moving_count[ch][cw] = 0;
    bfs(maze, moving_count, ch, cw, H, W);
    if (moving_count[dh][dw] == 10000000) cout << -1 << endl;
    else cout << moving_count[dh][dw] << endl;
}

Measured memory_bytes: 9508