#include<bits/stdc++.h>

#define fast                     ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int                      long long 
#define pb                       push_back
#define pii                      pair<int,int>
#define vi                       vector<int>
#define vii                      vector<pii>
#define mi                       map<int,int>
#define mii                      map<pii,int>
#define all(a)                   (a).begin(),(a).end()
#define F                        first
#define S                        second
#define sz(x)                    (int)x.size()
#define endl                     '\n'
#define MOD                      1000000007
#define loop(i,a,b)              for(int (i)=(a);(i)<(b);(i)++)
#define rloop(i,a,b)             for(int (i)=(a);(i)>=(b);(i)--)
#define Unique(A)                (A).resize(distance((A).begin(),unique(all((A)))))
#define MXe(A)                   *max_element(all(A))
#define MNe(A)                   *min_element(all(A))
#define SP(val,precision)        fixed << setprecision((precision)) << (val)
#define setBits(x)               __builtin_popcountll(x)
#define zroBits(x)               __builtin_ctzll(x)
#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
#define mem(name,val)            memset(name,val,sizeof(name))
#define traceA(A,x)              cerr<<#A<<": ";loop(i,0,x)cerr<<A[i]<<' ';cerr<<endl
#define traceV(A)                cerr<<#A<<": ";for(auto i:A)cerr<<i<<' ';cerr<<endl
#define traceM(A,n,m)            cerr<<#A<<": "<<endl;loop(i,0,n){loop(j,0,m) cerr << A[i][j] << ' ';cerr << endl;}
#define maxheap                  priority_queue<int>
#define minheap                  priority_queue<int,vector<int>,greater<int>>

using namespace std;

const int N = 1e3+77;
char A[N][N] , vis[N][N];
int H,W,sw,sh,eh,ew;

int32_t main(){
    fast;
    clock_t clk = clock();

    // =========s============
    
    cin >> H >> W >> sh >> sw >> eh >> ew ;
    sh--;sw--;eh--;ew--;
    loop(i,0,H) loop(j,0,W) cin >> A[i][j];
    vector<vi> Dis(H,vi(W,-1));
    deque<tuple<int,int,int>> Q;
    Q.emplace_front(sh,sw,0);
    while(!Q.empty()){
        int h,w,d; tie(h,w,d) = Q.front(); Q.pop_front();
        if(h<0 || H <=h || w<0 || W <= w || Dis[h][w] >= 0 || A[h][w]=='#') continue;
        Dis[h][w] = d;
        loop(i,-2,3) loop(j,-2,3){
            int x = i*i + j*j;
            if( x== 1) Q.emplace_front(h+i,w+j,d);
            else if( x > 1) Q.emplace_back(h+i,w+j,d+1);
        }    
    }
    cout << Dis[eh][ew] << endl;

    // =====================

    cerr << endl << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << endl;
    return 0;
}
    

Measured memory_bytes: 17656