// Problem : D - Wizard in Maze
// Contest : AtCoder - AtCoder Beginner Contest 176
// URL : https://atcoder.jp/contests/abc176/tasks/abc176_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

/* DEBUGGING */
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define deb(x...)
#endif

/* MACROS */ 
typedef long long int ll;
 
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef pair<pii , int> ppi;
 
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

typedef map<int,int> mii;

#define  ff          first
#define  ss          second
#define  pb          push_back
#define  IOS         ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define  lb          lower_bound
#define  ub          upper_bound
#define  all(x)      (x).begin(), (x).end()
#define  ps(x,y)     fixed<<setprecision(y)<<x
#define  setbit(x)   __builtin_popcountll(x)
#define  rep(i,a,b)  for(int i=a ; i<b ; ++i)
#define  repr(i,a,b) for(int i=a ; i>=b ; --i)

/* CONSTANTS */ 
#define PI 3.141592653589793
const ll MOD = 1e9 + 7;
const ll INF = 1000000000;
const ll MAX_N = 1e3 + 5;

// #define cerr if(false)cerr
/* CODE STARTS HERE */

int r,c;
int grid[MAX_N][MAX_N] , vis[MAX_N][MAX_N] , dist[MAX_N][MAX_N];

bool check(int x,int y){
    if(x<=0 || y<=0 || x>r || y>c) return false;
    if(grid[x][y]) return false;
    
    return true;
}

void solve(){
    cin >> r >> c;
    int st_x,st_y; cin >> st_x >> st_y;
    int ed_x,ed_y; cin >> ed_x >> ed_y;
    
    char ch;
    rep(i,1,r+1){
        rep(j,1,c+1){
            cin >> ch;
            grid[i][j] = (ch=='#' ? 1 : 0);
            dist[i][j] = INF;
            vis[i][j] = 0;
        }
    }
    
    vi dx = {1,-1,0,0,} , dy = {0,0,-1,1};
    deque< pii > dq;
    int nx,ny;
        
    dq.push_front({st_x , st_y});
    dist[st_x][st_y] = 0;
    
    while(!dq.empty()){
        pii node = dq.front();
        dq.pop_front();
        int x = node.ff , y = node.ss;
        // deb(x , y , dist[x][y]);
        
        if(x==ed_x && y==ed_y){
            cout << dist[x][y];
            return;
        }
        
        if(vis[x][y]) continue;
        vis[x][y] = 1;
        
        rep(i,0,4){
            nx = x + dx[i] , ny = y + dy[i];
            
            if(check(nx , ny) && vis[nx][ny]==0){
                dq.push_front({nx , ny});                
                dist[nx][ny] = dist[x][y];
            }
        }
        
        rep(i,-2,3){
            rep(j,-2,3){
                nx = x + i, ny = y + j;
                
                if(check(nx , ny) && vis[nx][ny]==0 && dist[nx][ny]==INF){
                    dq.push_back({nx , ny});
                    dist[nx][ny] = dist[x][y] + 1;
                }    
            }
        }
    }
    
    cout << "-1";
}

int main(){
    IOS;
    solve();
}

Measured memory_bytes: 15392