#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC targett("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(ll i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 4e18
#define five 100005;
#define six 1000005;
#define printclock cerr<<"Time"<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
//___________________________________________________________________________________________
const int N = six;
int dis[N];
vector<int> dp[N];
void ini()
{
       fri(i,0,N-1)
       {
              dis[i] = i;
       }
}
int root(int x)
{
       while(x != dis[x])
       {
              dis[x] = dis[dis[x]];
              x = dis[x];
       }
       return x;
}
void unio(int a,int b)
{
       dis[root(a)] = root(b);
}
int n,m;
int getid(int i,int j)
{
       return i*m + j;
}
bool isv(int i,int j)
{
       return (i<n && i >= 0 && j < m && j >= 0);
}
int x[] = {1 , 0 , -1 , 0};
int y[] = {0 , 1 , 0 , -1};
bool vis[N];
int main()
{
       cin>>n>>m;
       char mat[n][m];
       memset(vis,false,sizeof vis);
       int ix,iy,fx,fy;
       cin>>ix>>iy;
       cin>>fx>>fy;
       --ix;--iy;
       --fx;--fy;
       fri(i,0,n)
       {
              fri(j,0,m)
              {
                     cin>>mat[i][j];
              }
       }
       ini();
       fri(i,0,n)
       {
              fri(j,0,m)
              {
                     if(mat[i][j] == '#')continue;
                     fri(k,0,4)
                     {
                            if(isv(i+x[k],j+y[k]))
                            {
                                   if(mat[i+x[k]][j+y[k]] == '.')
                                   {
                                          unio(getid(i,j),getid(i+x[k],j+y[k]));
                                   }
                            }
                     }
              }
       }
       fri(i,0,n)
       {
              fri(j,0,m)
              {
                     if(mat[i][j]=='#')continue;
                     fri(k,-2,3)
                     {
                            fri(l,-2,3)
                            {
                                   int xx = i+k;
                                   int yy = j+l;
                                   if(isv(xx,yy))
                                   {
                                          if(mat[xx][yy]=='.')
                                          {
                                                 if(root(getid(i,j)) != root(getid(xx,yy)))
                                                 {
                                                        dp[root(getid(i,j))].pb(root(getid(xx,yy)));
                                                        dp[root(getid(xx,yy))].pb(root(getid(i,j)));
                                                 }
                                          }
                                   }
                            }
                     }
              }
       }
       int st = root(getid(ix,iy));
       int fn = root(getid(fx,fy));
       //cout<<st<<" "<<fn<<endl;
       if(st == fn)
       {
              cout<<0<<endl;
              return 0;
       }
       queue<pair<int,int>> q;
       q.push({st,0});
       vis[st]  = true;
       while(q.size())
       {
              int cv = q.front().fr;
              int ds = q.front().sc;
              q.pop();
              if(cv == fn)
              {
                     cout<<ds<<endl;
                     return 0;
              }
              for(int i : dp[cv])
              {
                     if(!vis[i])
                     {
                            q.push({i,ds+1});
                            vis[i] = true;
                     }
              }
       }
       cout<<-1<<endl;



}


Measured memory_bytes: 54908