/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author houghstc
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <vector>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
#include <cstring>
#include <climits>

using namespace std;
using pii = pair<int, int>;
using ll = long long;
using pll = pair<ll, ll>;

const int MOD = 1e9+7;

const int N = 1010;
char grid[N][N];
bool solved[N][N];
int d[N][N];
int h, w, ch, cw, dh, dw;

class DWizardInMaze {
public:
    vector<vector<int>> getAdjDis(int a, int b)
    {
        vector<vector<int>> res(5, vector<int>(5, INT_MAX));
        vector<vector<bool>> sol(5, vector<bool>(5, false));
        res[2][2] = 0;
        sol[2][2] = true;
        queue<pii> qu;
        qu.push({2, 2});
        while(qu.size())
        {
            auto p = qu.front();
            qu.pop();
            int x = p.first, y = p.second;

            int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};
            for(int k = 0; k < 4; ++k)
            {
                int new_x = x + dx[k], new_y = y + dy[k];
                int orig_x = a-2+new_x, orig_y = b-2+new_y;
                //cout << "x: " << new_x << ", y: " << new_y << ", a: " << a << ", b: " << b << ", ox: " << orig_x << ", oy: " << orig_y << ", res: " << res[x][y] << endl;
                if(new_x >= 0 && new_x < 5 && new_y >= 0 && new_y < 5 && orig_x >= 1 && orig_x <= h && orig_y >= 1 && orig_y <= w &&
                    !sol[new_x][new_y] && grid[orig_x][orig_y] == '.')
                {
                    //cout << " ... " << endl;
                    res[new_x][new_y] = 0;
                    sol[new_x][new_y] = true;
                    qu.push({new_x, new_y});
                }
            }
        }
        for(int i = 0; i < 5; ++i)
        {
            for(int j = 0; j < 5; ++j)
            {
                int orig_x = a-2+i, orig_y = b-2+j;
                if(orig_x >= 1 && orig_x <= h && orig_y >= 1 && orig_y <= w && res[i][j] != 0 && grid[orig_x][orig_y] == '.') {
                    //cout << "i: " << i << ", j: " << j << ", x: " << orig_x << ", y: " << orig_y << endl;
                    res[i][j] = 1;
                }
            }
        }
        return res;
    }

    void solve(std::istream& in, std::ostream& out) {

        in >> h >> w >> ch >> cw >> dh >> dw;
        for(int i = 1; i <= h; ++i) {
            for (int j = 1; j <= w; ++j) {
                in >> grid[i][j];
                solved[i][j] = false;
                d[i][j] = 1e9;
            }
        }
        deque<pii> dq;
        dq.push_front({ch, cw});
        d[ch][cw] = 0;
        //solved[ch][cw] = true;

        while(dq.size())
        {
            auto p = dq.front();
            dq.pop_front();
            int x = p.first, y = p.second;

            //cout << x << ", " << y << endl;
            if(p == pii{dh, dw}) {
                out << d[dh][dw] << endl;
                return;
            }

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

            solved[x][y] = true;

            vector<vector<int>> dis = getAdjDis(x, y);

            for(int i = 0; i < 5; ++i)
            {
                for(int j = 0; j < 5; ++j)
                {
                    int new_x = x-2+i, new_y = y-2+j;
                    if(new_x >= 1 && new_x <= h && new_y >= 1 && new_y <= w && !solved[new_x][new_y] && d[new_x][new_y] > d[x][y] + dis[i][j] && grid[new_x][new_y] == '.')
                    {
                        d[new_x][new_y] = d[x][y] + dis[i][j];
                        if(dis[i][j])
                            dq.push_back({new_x, new_y});
                        else
                            dq.push_front({new_x, new_y});
                    }
                }
            }
        }

        out << -1 << endl;
        return;

    }
};


int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    DWizardInMaze solver;
    std::istream& in(std::cin);
    std::ostream& out(std::cout);
    solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 9572