#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7;

int mpow(int base, int exp);
typedef  vector<int> vi;
typedef  vector<ll> vll;
typedef  vector<pair<ll,ll>> vpll;
typedef  vector<pair<int, int>> vpii;
typedef  pair<int, int> pii;
typedef  pair<ll, ll> pll;
typedef  unordered_map<ll, ll> umpll;
typedef  unordered_map<int, int> umpii;
typedef  unordered_map<ll, int> umpli;
typedef  unordered_set<int> usi;
typedef  unordered_set<ll> usll;
typedef  set<int> si;
typedef  set<ll> sll;
typedef  set<pair<int, int>> spii;
typedef  set<pair<ll, ll>> spll;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define fo(i,n) for(i = 0;i < n; i++)
#define Fo(i,k,n) for(i = k;k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define deb(x) cout << #x << " = " << x << endl
#define deb2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define deb3(x, y, z) cout << #x << " = " << x << ", " << #y << " = " << y << ", "<< #z << " = " << z << endl

ll randGen(ll lower, ll upper) { return (rand() % (upper - lower + 1)) + lower; }
ll dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
ll dir2[4][2] = {{2, 0}, {0, 2}, {-2, 0}, {0, -2}};
// cout << fixed;
// cout.precision(10);
//******************          Function here          *********************//
   bool isSafe(int x, int y, int n, int m) {
      return (x >= 0 && x < n && y >= 0 && y < m);
   }
   void solve(){
      int n, m, sx, sy, tx, ty;
      cin >> n >> m ;
      cin >> sx >> sy;
      cin >> tx >> ty;
      sx--, sy--, tx--, ty--;
      char a[n][m];
      int vis[n][m];
      memset(vis, -1, sizeof(vis));
      for(int i = 0; i < n ;i++)
         for(int j = 0; j < m; j++) cin >> a[i][j];
      deque<pair<int, int>> q;
      q.push_back({sx, sy});
      vis[sx][sy] = 0;
      while(!q.empty()) {
         pair<int, int> elem = q.front();
         q.pop_front();
         for(int i = 0; i < 4; i++) {
            int x = elem.ff + dir[i][0], y = elem.ss + dir[i][1];
            int x1 = elem.ff + dir2[i][0], y1 = elem.ss + dir2[i][1];
            if(isSafe(x, y, n, m) && (vis[x][y] == -1 || vis[x][y] > vis[elem.ff][elem.ss]) && a[x][y] != '#') {
               q.push_front({x, y});
               // deb2(x, y);
               vis[x][y] = vis[elem.ff][elem.ss];
               // if(x == tx && y == ty) {
               //    cout << vis[x][y] << endl;
               //    return;
               // }
            }
         }
         for(int i = elem.ff - 2; i <= elem.ff + 2; i++ ) {
            for(int j = elem.ss - 2; j <= elem.ss + 2; j++) {
               if(isSafe(i, j, n, m) && a[i][j] == '.' && (vis[i][j] == -1 || vis[i][j] > vis[elem.ff][elem.ss] + 1)) {
                  q.push_back({i, j});
                  vis[i][j] = vis[elem.ff][elem.ss] + 1;
                  // if(i == tx && j == ty) {
                  //    cout << vis[i][j] << endl;
                  //     return;
                  // }
               }
            }
         }
         }
         if(vis[tx][ty] < 0) cout << -1 << endl;
         else if(vis[tx][ty] == 0) cout << 0 << endl;
         else cout << vis[tx][ty] << endl;
      }

//*********************************************************************//

int main()
{
    #ifndef ONLINE_JUDGE
      string inputFile, outputFile;
      inputFile = "input.txt";
      outputFile = "output.txt";
      freopen(inputFile.c_str(), "r", stdin);
      freopen(outputFile.c_str(), "w", stdout);
    #endif

    int t = 1;
    // cin>>t;
    while(t-- >0){
      solve();
    }
    return 0;
}



/**********************   Utility Functions  *****************************/
   int mpow(int base, int exp) {
     base %= MOD;
     int result = 1;
     while (exp > 0) {
       if (exp & 1) result = ((ll)result * base) % MOD;
       base = ((ll)base * base) % MOD;
       exp >>= 1;
     }
     return result;
   }
/***********************************************************************/


Measured memory_bytes: 8476