using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static readonly int[][] di1=new int[][]{new int[]{-1,0},new int[]{1,0},new int[]{0,-1},new int[]{0,1}};
	static string[] bm;
	static int[][] b;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int h=s[0],w=s[1];
		const char c='#';
		var t=sc.Ia;
		var g=sc.Ia;
		int sy=t[0],sx=t[1];
		bm=new string[h+2];
		b=new int[h+2][];
		for(int i = 1;i<=h;i++) {
			bm[i]=c+sc.S+c;
			b[i]=new int[w+2];
		}
		bm[0]=bm[h+1]=new string(c,w+2);
		b[0]=b[h+1]=new int[w+2];
		var qu=new Qu2<ValueTuple<int,int,int>>();
		qu.Enqueue0(ValueTuple.Create(sy,sx,1));
		b[sy][sx]=1;
		while(qu.cnt>0){
			var e=qu.Dequeue;
			if(e.b){
				if(e.d.Item1==g[0]&&e.d.Item2==g[1]){break;}
				var bo=false;
				Fdi(e.d.Item1,e.d.Item2,(y,x)=>{
					if(bm[y][x]=='.'&&b[y][x]==0){
						qu.Enqueue0(ValueTuple.Create(y,x,e.d.Item3));
						b[y][x]=e.d.Item3;
					}
					else if(bm[y][x]==c){bo=true;}
				});
				if(bo){qu.Enqueue1(ValueTuple.Create(e.d.Item1,e.d.Item2,e.d.Item3+1));}
			}
			else{
				for(int i = -2;i<=2;i++) {
					for(int j = -2;j<=2;j++) {
						int y=e.d.Item1+i,x=e.d.Item2+j;
						if(y>0&&x>0&&y<=s[0]&&x<=s[1]&&bm[y][x]=='.'&&b[y][x]==0){
							qu.Enqueue0(ValueTuple.Create(y,x,e.d.Item3));
							b[y][x]=e.d.Item3;
						}
					}
				}
			}
		}
		Console.WriteLine("{0}",b[g[0]][g[1]]-1);
	}
	static void Fdi(int y,int x,Action<int,int> f){for(int i=0;i<4;i++){f(y+di1[i][0],x+di1[i][1]);}}
}
public class Qu2<T>{
	public class Dt{
		public bool b;
		public T d;
		public Dt(bool b,T d){this.b=b;this.d=d;}
		public override string ToString()=>"b:"+b.ToString()+" d:"+d.ToString();
	}
	private Queue<Dt> qu0=new Queue<Dt>(),qu1=new Queue<Dt>();
	public void Enqueue(bool b,T d){
		if(b){qu0.Enqueue(new Dt(true,d));}
		else{qu1.Enqueue(new Dt(false,d));}
	}
	public void Enqueue0(T d){qu0.Enqueue(new Dt(true,d));}
	public void Enqueue1(T d){qu1.Enqueue(new Dt(false,d));}
	public Dt Dequeue{get{return qu0.Count>0?qu0.Dequeue():qu1.Dequeue();}}
	public Dt Peek{get{return qu0.Count>0?qu0.Peek():qu1.Peek();}}
	public int cnt{get{return qu0.Count+qu1.Count;}}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}

Measured memory_bytes: 53508