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

#include <iostream>
#include <fstream>

#include <iostream>
#include "bits/stdc++.h"

using namespace std;
typedef unsigned long long ll;
typedef int                     integer;
//#define int                     long long
#define ii                      pair<int,int>
#define vii                     vector<pair<int,int>>
#define vi                      vector<int>
#define vvi                     vector<vector<int>>
#define f(a,b,c)                for(int a=b;a<c;a++)
#define s(x)                    in>>x
#define s2(x,y)                 in>>x>>y
#define s3(x,y,z)               in>>x>>y>>z
#define s4(x,y,z,w)             in>>x>>y>>z>>w
#define p(x)                    out<<x<<endl
#define p2(x,y)                 out<<x<<' '<<y<<endl
#define p3(x,y,z)               out<<x<<' '<<y<<' '<<z<<endl
#define p4(x,y,z,w)             out<<x<<' '<<y<<' '<<z<<' '<<w<<endl
#define pl(x)                   out<<x
#define pll(x)                  out<<x<<' '
#define range(v)                v.begin(),v.end()
#define rst(x)                  memset(x, -1, sizeof(x))
#define UNVISITED               -1
#define VISITED                 0
#define EXPLORED                2

#define EACH(x, a) for (auto& x: a)
template<class T> void read_v(T& x, std::istream& in) {
    in >> (x);
}
template<class A> void read_vec(vector<A>& x, std::istream& in) {
    EACH(a, x)
        read_v(a, in);
}

bool sortpairbysec(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}
template <typename T>
T factmod(T a, T modulus){ int res = 1, i; for (i = 2; i <= a; i++){ res *= i; res%=modulus;} return res; }
template <typename T>
int powmod(T a,T b,int m){ int ans=1; while(b>0){if(b&1){ans*=a;ans%=m;}a*=a;a%=m;b>>=1;} return ans; }
template <typename T>
int power(T a, T b){ int ans=1; while(b>0){if(b&1){ans*=a;}a*=a;b>>=1;}return ans;}

struct ms{
    int x, y;
};
typedef struct ms mss;

struct grid_s{
    char g;
    int min_moves;
};
typedef struct grid_s grid_ms;

bool valid(mss a, const vector<vector<grid_ms>>& grid, int H, int W){
    if (a.x >= H || a.x < 0 || a.y >= W || a.y < 0){
        return false;
    }
    if (grid[a.x][a.y].g=='#'){
        return false;
    }
    return true;
}

const vector<pair<int, int>> walks{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void ssolve(std::istream& in, std::ostream& out){
    int H, W; s2(H, W);
    int Ch, Cw; s2(Ch, Cw);
    Ch--, Cw--;
    int Dh, Dw; s2(Dh, Dw);
    Dh--, Dw--;
    vector<vector<grid_ms>> grid(H, vector<grid_ms>(W, {.g='e', .min_moves=INT_MAX}));
    f(i, 0, H){
        string st; s(st);
        assert((int)st.size()==W);
        f(j, 0, W){
            grid[i][j].g = st[j];
        }
    }
    /* Test on 1000x1000, all '.' for speed comparison, max deque size can be larger than N*W in this worst case
     * atcoder tests don't have this worst case
     * the return on curr.x==Dh&&curr.y==Dw 4.5s -> 1.7s worst case
     * */
    deque<mss> q;
    q.push_front(mss{.x = Ch, .y = Cw});
    grid[Ch][Cw].min_moves = 0;
    int max_q_size = 0;
    while(!q.empty()){
        max_q_size = max(max_q_size, (int)q.size());
        mss curr = q.front();
        q.pop_front();
        if(curr.x==Dh&&curr.y==Dw){
            p(grid[Dh][Dw].min_moves); // bfs so first match is min value
            return;
        }
        for (pair<int, int> pair1: walks){
            int dx = pair1.first; int dy = pair1.second;
            mss next = {.x = curr.x + dx, .y = curr.y + dy};
            if(valid(next, grid, H, W)){
                if(grid[next.x][next.y].min_moves > grid[curr.x][curr.y].min_moves){
                    grid[next.x][next.y].min_moves = grid[curr.x][curr.y].min_moves;
                    q.push_front(next);
                }
            }
        }
        for(int dx = -2; dx <= 2; dx++){
            for(int dy = -2; dy <= 2; dy++){
                if(dx==0&&dy==0){continue;}
                mss next = {.x = curr.x + dx, .y = curr.y + dy};
                if(valid(next, grid, H, W)){
                    if(grid[next.x][next.y].min_moves > grid[curr.x][curr.y].min_moves + 1){
                        grid[next.x][next.y].min_moves = grid[curr.x][curr.y].min_moves + 1;
                        q.push_back(next);
                    }
                }
            }
        }
    }
//    int ans = (grid[Dh][Dw].min_moves==INT_MAX?-1:grid[Dh][Dw].min_moves);
//    p(ans);
//    p2("Max queue size was:", max_q_size);
    p(-1);
    return;
}

class DWizardInMaze {
public:
void solve(std::istream& in, std::ostream& out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr);out.tie(nullptr);
    int t;
    int READ = 0;
    if(READ) s(t);
    else t = 1;
    while(t--){
        ssolve(in, out);
    }
}
};

integer main() {
    DWizardInMaze solver;
    std::istream& in(std::cin);
     std::ostream& out(std::cout);
     solver.solve(in, out);
}

Measured memory_bytes: 11616