//Littleboy123 Template 1.7
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

//pragmas

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")t
// #pragma GCC optimization ("unroll-loops")


typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
typedef long double ld;
typedef unsigned long long ull;

const long double pi = acos(-1.0);

#define FOR(a,b,c) for(ll (a)=(b);(a)<(c);++(a))
#define FORE(a,b,c) for(ll (a)=(b);(a)<=(c);++(a))
#define FORN(a,b,c) for(ll (a)=(b);(a)>(c);--(a))
#define FORNE(a,b,c) for(ll (a)=(b);(a)>=(c);--(a))
#define TC(n) cout << "Case #" << (n) << ": "
#define DEBUG(x) cout << "DEBUG " << #x << ' ' << x << '\n';
#define MEM(a,b) memset((a), (b), sizeof((a)))
#define MAX(a,b) (a) = max((a), (b))
#define MIN(a,b) (a) = min((a), (b))
#define nl '\n'
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>

void init(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

void infile(){
    freopen("fenwick.out", "r", stdin);
}

void outfile(){
    freopen("fenwick.out", "w", stdout);
}

ll modex(ll a, ll b, ll mod){
    if(b == 0)
        return 1;
    if(b == 1)
        return a;
    ll tmp = modex(a, b/2LL, mod);
    tmp = (tmp * tmp) % mod;
    if(b & 1LL)
        tmp = (tmp * a) % mod;
    return tmp;
}

//End of template

ll m, n, x, y, dx, dy;
char arr[1005][1005];
ll visited[1005][1005];
int dr[] = {1, 0, -1, 0};
int dc[] = {0, -1, 0, 1};

struct jalan{
    ll cost;
    ll x, y;

    jalan() {}
    jalan(ll cost, ll x, ll y): cost(cost), x(x), y(y) {}
};

struct compar{
    bool operator()(jalan a, jalan b){
        return a.cost > b.cost;
    }
};

int main(){
    init();

    cin >> n >> m >> x >> y >> dx >> dy;
    FOR(i, 0, n){
        FOR(j, 0, m){
            cin >> arr[i][j];
        }
    }
    FOR(i, 0, 1005){
        FOR(j, 0, 1005){
            visited[i][j] = INT_MAX;
        }
    }
    x--;
    y--;
    dx--;
    dy--;
    visited[x][y] = 0;
    priority_queue<jalan, vector<jalan>, compar> pq;
    pq.push(jalan(0, x, y));
    while(pq.empty() == false){
        x = pq.top().x;
        y = pq.top().y;
        pq.pop();
        FOR(i, 0, 4){
            if(x + dr[i] < 0 || x + dr[i] >= n || y + dc[i] < 0 || y + dc[i] >= m)
                continue;
            if(arr[x+dr[i]][y+dc[i]] == '#')
                continue;
            if(visited[x+dr[i]][y+dc[i]] > visited[x][y]){
                visited[x+dr[i]][y+dc[i]] = visited[x][y];
                pq.push(jalan(visited[x][y], x+dr[i], y+dc[i]));
            }
        }
        FOR(i, -2, 3){
            FOR(j, -2, 3){
                if(x + i < 0 || x + i >= n || y + j < 0 || y + j >= m)
                    continue;
                if(arr[x+i][y+j] == '#')
                    continue;
                if(visited[x+i][y+j] > visited[x][y]+1){
                    visited[x+i][y+j] = visited[x][y]+1;
                    pq.push(jalan(visited[x+i][y+j], x+i, y+j));
                }
            }
        }
    }
    if(visited[dx][dy] == INT_MAX){
        visited[dx][dy] = -1;
    }
    cout << visited[dx][dy] << endl;
    return 0;
}


Measured memory_bytes: 12592