//設定言語check
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <limits.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <queue>
using namespace std;

int main(){
  int H, W, Ch, Cw, Dh, Dw, ans = 0, phase = 0, checker = 1 ;
  int nowH, nowW ;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw ;
  char S[H][W] ;
  for(int i=0;i<H;i++){
    cin >> S[i] ;
  }
  Ch-- ; Cw-- ; Dh-- ; Dw-- ;
  queue<int> que1H, que1W, que2H, que2W ;
  que1H.push(Ch) ;
  que1W.push(Cw) ;
  S[Ch][Cw] = 'X' ;
  while(checker){
    if(!que1H.empty()){ //移動A
      while(!que1H.empty()){
        nowH = que1H.front() ;
        nowW = que1W.front() ;
        if((nowH == Dh) && (nowW == Dw) ){
          cout << phase << endl ;
          return 0 ;
        }
        if(nowH != 0){
          if(S[nowH-1][nowW] == '.'){
            S[nowH-1][nowW] = 'X' ;
            que1H.push(nowH-1) ;
            que1W.push(nowW) ;
          }
        }
        if(nowH != (H-1)){
          if(S[nowH+1][nowW] == '.'){
            S[nowH+1][nowW] = 'X' ;
            que1H.push(nowH+1) ;
            que1W.push(nowW) ;
          }
        }
        if(nowW != 0){
          if(S[nowH][nowW-1] == '.'){
            S[nowH][nowW-1] = 'X' ;
            que1H.push(nowH) ;
            que1W.push(nowW-1) ;
          }
        }
        if(nowW != (W-1)){
          if(S[nowH][nowW+1] == '.'){
            S[nowH][nowW+1] = 'X' ;
            que1H.push(nowH) ;
            que1W.push(nowW+1) ;
          }
        }
        que2H.push(nowH) ;
        que2W.push(nowW) ;
        que1H.pop() ;
        que1W.pop() ;
      }  
    }
    else if(!que2H.empty()){
      while(!que2H.empty()){
        nowH = que2H.front() ;
        nowW = que2W.front() ;
        if((nowH > 1) && (nowW > 1)){
          if(S[nowH-2][nowW-2] == '.'){
            S[nowH-2][nowW-2] = 'X' ;
            que1H.push(nowH-2 ) ;
            que1W.push(nowW-2 ) ;
          }
        }
        if((nowH > 1) && (nowW > 0)){
          if(S[nowH-2][nowW-1] == '.'){
            S[nowH-2][nowW-1] = 'X' ;
            que1H.push(nowH-2 ) ;
            que1W.push(nowW-1 ) ;
          }
        }
        if((nowH > 1)){
          if(S[nowH-2][nowW] == '.'){
            S[nowH-2][nowW] = 'X' ;
            que1H.push(nowH-2 ) ;
            que1W.push(nowW ) ;
          }
        }
        if((nowH > 1) && (nowW < W-1)){
          if(S[nowH-2][nowW+1] == '.'){
            S[nowH-2][nowW+1] = 'X' ;
            que1H.push(nowH-2 ) ;
            que1W.push(nowW+1 ) ;
          }
        }
        if((nowH > 1) && (nowW < W-2)){
          if(S[nowH-2][nowW+2] == '.'){
            S[nowH-2][nowW+2] = 'X' ;
            que1H.push(nowH-2 ) ;
            que1W.push(nowW+2 ) ;
          }
        }
        if((nowH > 0) && (nowW > 1)){
          if(S[nowH-1][nowW-2] == '.'){
            S[nowH-1][nowW-2] = 'X' ;
            que1H.push(nowH-1 ) ;
            que1W.push(nowW-2 ) ;
          }
        }
        if((nowH > 1) && (nowW > 0)){
          if(S[nowH-1][nowW-1] == '.'){
            S[nowH-1][nowW-1] = 'X' ;
            que1H.push(nowH-1 ) ;
            que1W.push(nowW-1 ) ;
          }
        }
        if((nowH > 0) && (nowW < W-1)){
          if(S[nowH-1][nowW+1] == '.'){
            S[nowH-1][nowW+1] = 'X' ;
            que1H.push(nowH-1 ) ;
            que1W.push(nowW+1 ) ;
          }
        }
        if((nowH > 0) && (nowW < W-2)){
          if(S[nowH-1][nowW+2] == '.'){
            S[nowH-1][nowW+2] = 'X' ;
            que1H.push(nowH-1 ) ;
            que1W.push(nowW+2 ) ;
          }
        }
        if((nowW > 1)){
          if(S[nowH][nowW-2] == '.'){
            S[nowH][nowW-2] = 'X' ;
            que1H.push(nowH ) ;
            que1W.push(nowW-2 ) ;
          }
        }
        if((nowW < W-2)){
          if(S[nowH][nowW+2] == '.'){
            S[nowH][nowW+2] = 'X' ;
            que1H.push(nowH ) ;
            que1W.push(nowW+2 ) ;
          }
        } //
        if((nowH < H-1) && (nowW > 1)){
          if(S[nowH+1][nowW-2] == '.'){
            S[nowH+1][nowW-2] = 'X' ;
            que1H.push(nowH+1 ) ;
            que1W.push(nowW-2 ) ;
          }
        }
        if((nowH < H-1) && (nowW > 0)){
          if(S[nowH+1][nowW-1] == '.'){
            S[nowH+1][nowW-1] = 'X' ;
            que1H.push(nowH+1 ) ;
            que1W.push(nowW-1 ) ;
          }
        }
        if((nowH < H-1) && (nowW < W-1)){
          if(S[nowH+1][nowW+1] == '.'){
            S[nowH+1][nowW+1] = 'X' ;
            que1H.push(nowH+1 ) ;
            que1W.push(nowW+1 ) ;
          }
        }
        if((nowH < H-1) && (nowW < W-2)){
          if(S[nowH+1][nowW+2] == '.'){
            S[nowH+1][nowW+2] = 'X' ;
            que1H.push(nowH+1 ) ;
            que1W.push(nowW+2 ) ;
          }
        }
        if((nowH < H-2) && (nowW > 1)){
          if(S[nowH+2][nowW-2] == '.'){
            S[nowH+2][nowW-2] = 'X' ;
            que1H.push(nowH+2 ) ;
            que1W.push(nowW-2 ) ;
          }
        }
        if((nowH < H-2) && (nowW > 0)){
          if(S[nowH+2][nowW-1] == '.'){
            S[nowH+2][nowW-1] = 'X' ;
            que1H.push(nowH+2 ) ;
            que1W.push(nowW-1 ) ;
          }
        }
        if((nowH < H-2)){
          if(S[nowH+2][nowW] == '.'){
            S[nowH+2][nowW] = 'X' ;
            que1H.push(nowH+2 ) ;
            que1W.push(nowW ) ;
          }
        }
        if((nowH < H-2) && (nowW < W-1)){
          if(S[nowH+2][nowW+1] == '.'){
            S[nowH+2][nowW+1] = 'X' ;
            que1H.push(nowH+2 ) ;
            que1W.push(nowW+1 ) ;
          }
        }
        if((nowH < H-2) && (nowW < W-2)){
          if(S[nowH+2][nowW+2] == '.'){
            S[nowH+2][nowW+2] = 'X' ;
            que1H.push(nowH+2 ) ;
            que1W.push(nowW+2 ) ;
          }
        }
        que2H.pop() ;
        que2W.pop() ;
      }
      phase++ ;
    }
    else{
      checker = 0 ;
    }
  }
  cout << -1 << endl ;
  return 0 ;
}  


Measured memory_bytes: 4572