using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	bool InRange(int t, int l, int r){ return l <= t && t < r;}
	public void Solve(){
		
		int Inf = (int) 1e9;
		Func<int, int, int> enc = (y, x) => y * W + x;
		int[][] min = new int[H][];
		bool[][] used = new bool[H][];
		for(int i=0;i<H;i++){
			min[i] = new int[W];
			used[i] = new bool[W];
			for(int j=0;j<W;j++){
				min[i][j] = Inf;
			}
		}
		
		int[] dy = new int[]{0,1,0,-1};
		int[] dx = new int[]{1,0,-1,0};
		
		var SH = new SkewHeap<Pair>();
		SH.Push(new Pair(enc(Ch, Cw), 0));
		min[Ch][Cw] = 0;
		while(SH.Count > 0){
			var p = SH.Top; SH.Pop();
			int y = p.Pos / W;
			int x = p.Pos % W;
			long cost = p.Cost;
			for(int i=-2;i<=2;i++){
				int ny = y + i;
				if(!InRange(ny, 0, H)) continue;
				for(int j=-2;j<=2;j++){
					int nx = x + j;
					if(!InRange(nx, 0, W)) continue;
					if(S[ny][nx] == '#') continue;
					if(min[ny][nx] > min[y][x] + 1){
						min[ny][nx] = min[y][x] + 1;
						SH.Push(new Pair(enc(ny, nx), min[ny][nx]));
					}
				}
			}
			for(int t=0;t<4;t++){
				int ny = y + dy[t];
				int nx = x + dx[t];
				if(!InRange(ny, 0, H)) continue;
				if(!InRange(nx, 0, W)) continue;
				if(S[ny][nx] == '#') continue;
				if(min[ny][nx] > min[y][x]){
					min[ny][nx] = min[y][x];
					SH.Push(new Pair(enc(ny, nx), min[ny][nx]));
				}
			}
		}
		
		int ans = min[Dh][Dw] == Inf ? -1 : min[Dh][Dw];
		Console.WriteLine(ans);
		
		
		
		
	}
	int H,W;
	int Ch, Cw;
	int Dh, Dw;
	String[] S;
	public Sol(){
		var d = ria();
		H = d[0]; W = d[1];
		d = ria();
		Ch = d[0] - 1; Cw = d[1] - 1;
		d = ria();
		Dh = d[0] - 1; Dw = d[1] - 1;
		S = new String[H];
		for(int i=0;i<H;i++) S[i] = rs();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


class Pair : IComparable<Pair> {
	public int Pos;
	public long Cost;
	public Pair(int p, long c) {
		Pos = p; Cost = c;
	}
	public int CompareTo(Pair t) {
		//return this.Cost > t.Cost ? -1 : this.Cost < t.Cost ? 1 : 0;
		return this.Cost > t.Cost ? 1 : this.Cost < t.Cost ? -1 : 0;
	}
}


class SkewHeap<T> where T:IComparable<T>{
	public int Count{
		get{return cnt;}
		private set{cnt=value;}
	}
	
	public SkewHeap(){
		root=null;
		this.Count=0;
	}
	
	public void Push(T v){
		NodeSH<T> p=new NodeSH<T>(v);
		root=NodeSH<T>.Meld(root,p);
		this.Count++;
	}
	
	public void Pop(){
		if(root==null)return;
		root=NodeSH<T>.Meld(root.L,root.R);
		this.Count--;
	}
	
	public T Top{
		get{return root.Val;}
	}
	
	int cnt;
	NodeSH<T> root;
	
	class NodeSH<S> where S : IComparable<S> {
		public NodeSH<S> L, R;
		public S Val;

		public NodeSH(S v){
			Val = v;
			L = null; R = null;
		}
		public static NodeSH<S> Meld(NodeSH<S> a, NodeSH<S> b){
			if(a == null)return b;
			if (b == null) return a;
			if (a.Val.CompareTo(b.Val) > 0) swap(ref a, ref b);
			a.R = Meld(a.R, b);
			swap(ref a.L, ref a.R);
			return a;
		}

		static void swap<U>(ref U x, ref U y) {
			U t = x; x = y; y = t;
		}
	}
}



Measured memory_bytes: 64284