/*
 * ----------------------------------------------------------------------------
 *  (: Bismillahir Rahmanir Rahim :) 
 * File: /home/grim/Programming/Atcoder/extra/D_Wizard_in_Maze.cpp
 * Project: /home/grim/Programming/Atcoder/extra
 * Created Date: Friday, September 4th 2020, 10:38:30 am
 * Author: Grim Firefly :)
 * Author Mail : GrimFirefly1@gmail.com
 * -----
 * Last Modified: Fri Sep 04 2020
 * Modified By: Grim Firefly :)
 * -----
 *   ________        .__           ___________.__                  _____ .__          
 *  /  _____/_______ |__|  _____   \_   _____/|__|_______   ____ _/ ____\|  |  ___.__.
 * /   \  ___\_  __ \|  | /     \   |    __)  |  |\_  __ \_/ __ \\   __\ |  | <   |  |
 * \    \_\  \|  | \/|  ||  Y Y  \  |     \   |  | |  | \/\  ___/ |  |   |  |__\___  |
 *  \______  /|__|   |__||__|_|  /  \___  /   |__| |__|    \___  >|__|   |____// ____|
 *         \/                  \/       \/                     \/              \/     
 * 
 *           |\_|\
 *           | a_a\
 *           | | "]
 *       ____| '-\___
 *      /.----.___.-'\
 *     //        _    \
 *    //   .-. (GF)  /|
 *   |'|  /\:  .--  / \
 *  // |-/  \_/____/\/~|
 * |/  \ |  []_|_|_] \ |
 * | \  | \ |___   _\ ]_}
 * | |  '-' /   '.'  |
 * | |     /    /|:  | 
 * | |     |   / |:  /\
 * | |     /  /  |  /  \
 * | |    |  /  /  |    \
 * \ |    |/\/  |/|/\    \
 *  \|\ |\|  |  | / /\/\__\
 *   \ \| | /   | |__
 *        / |   |____)
 *        |_/
 * ----------------------------------------------------------------------------
 */

//Code Start
#include <bits/stdc++.h>
using namespace std;

//constant
#define PI acos(-1.0)
#define SZ 10000
#define mod 1000000007
//typedef
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
typedef unsigned un;
typedef pair<int, int> pii;
typedef vector<int> vi;

//Debug Setting up
#define watch(x) cout << (#x) << "=" << x << endl
#define watch2(x, y) cout << (#x) << "=" << x << " & " << (#y) << "=" << y << endl;
#define watchara(wow)     \
    for (auto x : wow)    \
    {                     \
        cout << x << " "; \
    }                     \
    cout << "\n"

//defining fast_io
#define fast_io                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)

//defining something
#define F first
#define S second

//Main Function Start From Here
vector<string> adj;
vector<int> dist[1001];
int h, w;
int cpi, cpj;
int tpi, tpj;
int xaxis[] = {-1, 0, 1, 0};
int yaxis[] = {0, -1, 0, 1};
int inf = 1e8;
void BFS(pii src)
{
    deque<pii> qq;
    qq.push_back(src);
    //sources is always zero
    dist[src.F][src.S] = 0;
    while (!qq.empty())
    {
        int i, j;
        i = qq.front().F;
        j = qq.front().S;
        qq.pop_front();
        int d = dist[i][j];
        for (int x = -2; x < 3; x++)
        {
            for (int y = -2; y < 3; y++)
            {
                int ni = i + x, nj = j + y;
                if (ni >= h or ni < 0 or nj >= w or nj < 0)
                    continue;
                if (adj[ni][nj] == '#')
                    continue;
                if (dist[ni][nj] <= d+1)
                    continue;
                dist[ni][nj] = d+1;
                qq.push_back({ni, nj});
            }
        }
        for (int k = 0; k < 4; k++)
        {
            int ni = i + xaxis[k];
            int nj = j + yaxis[k];
            if (ni >= h or ni < 0 or nj >= w or nj < 0)
                continue;
            if (adj[ni][nj] == '#')
                continue;
            if (dist[ni][nj] <= d)
                continue;
            dist[ni][nj] = d;
            qq.push_front({ni, nj});
        }
    }
}
void solve()
{
    cin >> h >> w;
    cin >> cpi >> cpj;
    cin >> tpi >> tpj;
    // getchar();

    cpi--, cpj--, tpi--, tpj--;
    string str;
    for (int i = 0; i < h; i++)
    {
        cin >> str;
        adj.push_back(str);
        dist[i] = vector<int>(w, inf);
    }
    BFS({cpi, cpj});
    int ans = dist[tpi][tpj];
    ans = ans == inf ? -1 : ans;
    cout << ans << endl;
}

int main()
{
    fast_io;

    solve();

    return 0;
}

Measured memory_bytes: 8612