/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cmath>
#include <iostream>
#include <stack>
#include <queue>
#include <climits>
#include <iomanip>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\n"
#define MOD 998244353

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

const int maxN = 1e6;
vector<int> adj[maxN + 1];
vector<bool> vis_g(maxN + 1, false);
vector<int> dis(maxN + 1, -1);

vector<vector<char>> grid;
vector<vector<bool>> vis;
vector<vector<int>> color;

vector<int> Dr = {1, -1, 0, 0};
vector<int> Dc = {0, 0, -1, 1}; 

int bfs(int u, int v) 
{
    queue<int> q;
    //printf("%d %d\n", u, v);
    vis_g[u] = true;
    q.push(u);
    dis[u] = 0;

    while(q.size())
    {
        int x = q.front();
        q.pop();

        for(int node : adj[x])
        {
            if(!vis_g[node])
            {
                vis_g[node] = true;
                dis[node] = dis[x] + 1;
                q.push(node);
                if(node == v)
                {
                    return dis[v];
                }
            }
        }
    }

    return dis[v];
    
}

void dfs(int r, int c, int R, int C, int col)
{
    vis[r][c] = true;
    color[r][c] = col;

    for(int i = 0; i < 4; i++)
    {
        int rr = r + Dr[i];
        int cc = c + Dc[i];

        if(rr < 0 || cc < 0 || rr >= R || cc >= C) 
        {
            continue;
        }

        if(vis[rr][cc] || grid[rr][cc] == '#') 
        {
            continue;
        }

        dfs(rr, cc, R, C, col);
    }
}

void solve()
{
    int H, W;
    cin >> H >> W;
    
    grid = vector<vector<char>> (H, vector<char> (W));
    vis = vector<vector<bool>> (H, vector<bool> (W, false));
    color = vector<vector<int>> (H, vector<int> (W));

    int cr, cc, dr, dc;
    cin >> cr >> cc >> dr >> dc;


    for(int i = 0; i < H; i++) 
    {
        for(int j = 0; j < W; j++)
        {
            cin >> grid[i][j];
        }
    }

    int c = 1;
    for(int i = 0; i < H; i++) 
    {
        for(int j = 0; j < W; j++)
        {
            if(!vis[i][j] && grid[i][j] != '#')
            {
                dfs(i, j, H, W, c++);
            }
        }
    }

    /*for(int i = 0; i < H; i++)
    {
        for(int j = 0; j < W; j++)
        {
            cout << color[i][j] << " ";
        }
        cout << "\n";
    }*/

    set<pair<int, int>> edges;

    vector<int> drs = {1, 0, -1, 0, 1, 1, -1, -1,  2,  2, 2, 2, 2, -2, -2, -2, -2, -2, 1, 0, -1, 1,  0, -1};
    vector<int> dcs = {0, 1, 0, -1 ,1, -1, 1, -1, -1, -2, 1, 2, 0, -1, -2, 1, 2, 0, 2, 2,  2, -2, -2, -2};

    vector<vector<int>> check(5, vector<int> (5));

    /*for(int i = 0; i < drs.size(); i++)
    {
        //printf("%d %d\n", drs[i] + 2, dcs[i] + 2);
        check[drs[i] + 2][dcs[i] + 2] = 1;
    }*/

    /*for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            cout << check[i][j] << " ";
        }
        cout << "\n";
    }*/

    for(int i = 0; i < H; i++) 
    {
        for(int j = 0; j < W; j++)
        {
            if(grid[i][j] == '#')
            {
                continue;
            }
            for(int k = 0; k < drs.size(); k++)
            {
                int rr = i + drs[k];
                int cc = j + dcs[k];

                if(rr < 0 || cc < 0 || rr >= H || cc >= W) 
                {
                    continue;
                }

                if(color[rr][cc] == color[i][j] || grid[rr][cc] == '#')
                {
                    continue;
                }

                int c1 = color[rr][cc];
                int c2 = color[i][j];

                if(c1 > c2)
                {
                    swap(c1, c2);
                }

                if(edges.find({c1, c2}) == edges.end())
                {
                    adj[c1].push_back(c2);
                    adj[c2].push_back(c1);
                    edges.insert({c1, c2});
                    //cout << c1 << " " << c2 << "\n";
                }
            }
        }
    }

    cout << bfs(color[cr - 1][cc - 1], color[dr - 1][dc - 1]) << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int T = 1;
    //cin >> T;

    while (T--)
    {
        solve();
    }

    return 0;
}





Measured memory_bytes: 49056