#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#define aut(r,v) for(auto r:v)
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define pc()  pop_back()
#define ull unsigned long long
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
#define endl '\n'
#define mod 1000000007
#define Sort(a) sort(all(a))
#define ED(a) Sort(a), a.erase(unique(all(a)), a.end())//removing all duplicates
#define max3(a, b, c)   max(a, max(b, c))
#define min3(a, b, c)   min(a, min(b, c))
#define Max(a)       *max_element(all(a))
#define Min(a)       *min_element(all(a))
#define MaxP(a)       max_element(all(a)) - a.begin()
#define MinP(a)        min_element(all(a)) - a.begin()
#define allUpper(a)     transform(all(a), a.begin(), :: toupper)
#define allLower(a)     transform(all(a), a.begin(), :: tolower)
#define rev(a)          reverse(all(a))
#define ub(v,k)          upper_bound(all(v), k) - v.begin()
#define lb(v,k)          lower_bound(all(v), k) - v.begin()
#define adv(a,n)        advance(auto it:a,n)
#define RSort(a)         sort(a.rbegin(),a.rend()) //decending order
#define cnt(v,a)             count(all(v),a)
#define bs(v,a)           binary_search(all(v),a)
#define popcount(mask)                       __builtin_popcount(mask) // count set bit
#define popcountLL(mask)                     __builtin_popcountll(mask) // for long long
#define X real() // useful for working with #include <complex> for computational geometry
#define Y imag()
#define ll long long
#define ss second
#define ff first
#define vl vector<long long>
#define vi vector<int>
#define vb vector<bool>
#define vc vector<char>
#define pii pair<int,int>
#define vpii vector<pii>
#define vvi vector<vi>
#define vs vector<string>
#define trace1(x)                           cerr << #x << ": " << x << endl;
#define trace2(x, y)                        cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                     cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)                  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)               cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)            cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

void printc(vector<char>& result) {
    aut(r, result) cout << r;
    cout << endl;
}
void printl(vector<long long>& result) {
    aut(r, result) cout << r << " ";
    cout << endl;
}
void print(vector<int>& result) {
    aut(r, result) cout << r << " ";
    cout << endl;
}
ll power(ll a, ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
int h, w;
int ix,iy,fx,fy;

int zero_one_bfs(const vector<string>& a){
  vector<vector<int>> dis(h, vector<int>(w, INF));
  vector<vector<bool>> visit(h, vector<bool>(w, false));
  deque<pii> dq;
  dis[ix][iy]=0;
  dq.push_front({ix,iy});
 //trace2(dq.front().ff,dq.front().ss);
  while(!dq.empty()){
  int x=dq.front().ff;
  int y=dq.front().ss;
  //trace2(x,y);
   dq.pop_front();
   if(visit[x][y])continue;
   if(x==fx&&y==fy)return dis[x][y];
   visit[x][y]=true;
   for(int dx=-2;dx<=2;dx++){
     for(int dy=-2;dy<=2;dy++){
       if(dx==0&&dy==0)continue;
       int nx=x+dx;
       int ny=y+dy;
       if(nx<0||ny<0||nx>=h||ny>=w)continue;
       if(visit[nx][ny]||a[nx][ny]=='#')continue;
       int d=0;
       if(abs(dx)+abs(dy)>1)d=1;
       if(dis[nx][ny]>dis[x][y]+d){
         dis[nx][ny]=dis[x][y]+d;
         if(d==0)dq.push_front({nx,ny});
         else dq.push_back({nx,ny});

       }
     }
   }
  }
   return -1;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test;
    test = 1;
  //  cin >> test;
    //preprocess();

    while (test--) {

    cin>>h>>w;

    cin>>ix>>iy>>fx>>fy;
    ix--;
    iy--;
    fx--;
    fy--;
    vs a(h);
    rep(i,h)cin>>a[i];
    //rep(i,h)cout<<a[i];
    cout<<zero_one_bfs(a)<<endl;


  }
};


Measured memory_bytes: 8948