using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
public class MainClass
{
	public const long Giri = 1000000007;
	public const long DpInf = 99999999999999;
	public class Pos
	{
		public int X{get;set;}
		public int Y{get;set;}
	}
	
	public class Node
	{
		public int Index {get;set;}
		public List<int> Edges{get;set;}
	}
	
	public class Far
	{
		public int Index {get;set;}
		public long Distance {get;set;}
	}
	
	public static void Main(string[] args)
	{
		var p = Input().ToInt();
		var h = p[0];
		var w = p[1];
		p = Input().ToInt();
		var cy = p[0]-1;
		var cx = p[1]-1;
		p = Input().ToInt();
		
		var dy = p[0]-1;
		var dx = p[1]-1;
		
		var map = new string[h];
		for(int i = 0; i < h; i++)
		{
			map[i] = Console.ReadLine();
		}

		var area = new int[h,w];
		int areaNext = 1;
		

		
		//var walls = new Stack<Pos>();
		for(int y = 0; y < h; y++)
		{
			for(int x = 0;x < w; x++)
			{
				if(map[y][x] == '#')
				{
					area[y, x] = -1;
					//walls.Push(new Pos(){X=x, Y=y});
					continue;
				}
				
				if(area[y, x] != 0)
					continue;
				
				var queue = new Queue<Pos>();
				queue.Enqueue(new Pos(){X=x, Y=y});
				bool areaWrite = false;
				while(queue.Count > 0)
				{
					var pos = queue.Dequeue();
					if(pos.X < 0 || pos.Y < 0 || pos.X >= w || pos.Y >= h)
						continue;
					if(map[pos.Y][pos.X] == '#')
					{
						area[pos.Y, pos.X] = -1;
						//walls.Push(new Pos(){X=x, Y=y});
						continue;
					}
					if(area[pos.Y, pos.X] != 0)
						continue;
						
					queue.Enqueue(new Pos(){X=pos.X+0, Y=pos.Y+1});
					queue.Enqueue(new Pos(){X=pos.X+0, Y=pos.Y-1});
					queue.Enqueue(new Pos(){X=pos.X+1, Y=pos.Y+0});
					queue.Enqueue(new Pos(){X=pos.X-1, Y=pos.Y+0});
					area[pos.Y, pos.X] = areaNext;
					areaWrite = true;
				}
				if(areaWrite)
					areaNext++;
				
			}
			
		}
		
		//var edges = new bool[areaNext, areaNext];
		var edges = new HashSet<int>[areaNext] ;
		for(int i = 0; i < edges.Length; i++)
			edges[i]=new HashSet<int>();
		/*
		for(int i = 0; i < areaNext; i++)
			for(int j = 0; j < areaNext; j++)
				edges[i, j] = DpInf;
				*/
		/*
		for(int y = 0; y < h; y++)
		{
			for(int x = 0; x < w; x++)
			{
				Console.Write("{0}, ", area[y, x]);
			}
			Console.WriteLine();
			}*/
		/*
		while(walls.Count > 0)
		{
			var pos = walls.Pop();
			var x = pos.X;
			var y = pos.Y;
			var inareas = new HashSet<int>();
			for(int i = -1; i <= 1; i++)
			{
				if(i == 0) continue;
				if(y + i < 0 || y + i >= h) continue;
				for(int j = -1; j <= 1; j++)
				{
					if(j == 0) continue;
					if(x + j < 0 || x + j >= w) continue;
					if(area[y+i, x+j] == -1) continue;
					inareas.Add(area[y+i, x+j]);
				}
			}
					//Console.WriteLine(" > {0}, {1}", x, y);
			
			foreach(int t in inareas)
			{
				foreach(int s in inareas)
				{
					//Console.WriteLine("{0}, {1}", s, t);
				
					edges[s, t] = true;
					edges[t, s] = true;
				}
			}
		}*/
		
		
		for(int y = 0; y < h; y++)
		{
			for(int x = 0; x < w; x++)
			{
				if(area[y, x] <= 0)
					continue;
				//Console.WriteLine(">{0} {1}", x, y);
				for(int i = -2; i <= 2; i++)
				{
					if(y + i < 0 || y + i >= h) continue;
					for(int j = -2; j <= 2; j++)
					{
						if(x + j < 0 || x+j >= w) continue;
						if(area[y+i, x+j] == -1) continue;
						if(area[y, x] == area[y+i, x+j]) continue;
						//edges[area[y, x], area[y+i, x+j]] = true;
						//edges[area[y+i, x+j], area[y, x]] = true;
						edges[area[y, x]].Add( area[y+i, x+j]);
						edges[area[y+i, x+j]].Add(area[y, x]);
					}
				}
			}
		}
		/*
		for(int y = 0; y < areaNext; y++)
		{
			for(int x = 0; x < areaNext; x++)
			{
				Console.Write("{0}, ", edges[y, x]);
			}
			Console.WriteLine();
		}
			*/
		
		//var mat = new long[areaNext, areaNext];
		
		//warshal
		/*
		for(int i = 1; i < areaNext; i++)
		{
			for(int j = 1; j < areaNext; j++)
			{
				for(int k = 1; k < areaNext; k++)
				{
					edges[j, k] = Math.Min(edges[j, k], edges[j, i] + edges[i, k]);
				}
			}
		}
		*/
		
		long goalArea = area[dy,dx];
		
		var c = new Queue<Far>();
		c.Enqueue(new Far(){Index=(int)area[cy,cx], Distance=0});
		var visited = new long[areaNext];
		for(int i = 0 ; i < visited.Length; i++)
			visited[i] = -1;
		while(c.Count > 0)
		{
			Far f = c.Dequeue();
			int cur = f.Index;
			if(visited[cur] != -1 && visited[cur] <= f.Distance)
				continue;
			visited[cur] = f.Distance;
			
			foreach(var b in edges[cur])
			{
				c.Enqueue(new Far(){Index=b, Distance=f.Distance+1});
			}
			
			/*
			for(int i = 1; i < areaNext; i++)
			{
				if(cur != i && edges[cur, i])
				{
					c.Enqueue(new Far(){Index=i, Distance=f.Distance+1});
				}
			}*/
			
		
		}
		/*
		for(int i = 1; i < areaNext; i++)
		{
			for(int j = 1; j < areaNext; j++)
			{
				
			}
		}
		*/
		/*
		for(int y = 0; y < h; y++)
		{
			for(int x = 0; x < w; x++)
			{
				Console.Write("{0}, ", area[y, x]);
			}
			Console.WriteLine();
		}*/
		
		if(area[cy,cx] == goalArea)
		{
			Console.WriteLine(0);
		}
		else
		{
			long ttt = visited[goalArea];
			Console.WriteLine(ttt);
		}
		/*
		else if(edges[area[cy,cx], area[dy,dx]] >= DpInf)
		{
			Console.WriteLine(-1);
		}
		else
		{
			Console.WriteLine(edges[area[cy-1,cx-1], area[dy-1,dx-1]]);
		}
		*/
	}

	
	
	static long GCD(long a, long b)
	{
		var v = new[] { a, b };
		while (v[1] != 0) { v = new[] { v[1], v[0] % v[1] }; }
		return v[0];
	}
	public static string[] Input() { return Console.ReadLine().Split(); }
}

public static class Smarter
{
	public static int ToInt(this string str) { return int.Parse(str); }
	public static long ToLong(this string str) { return long.Parse(str); }
	public static int[] ToInt(this string[] str, int offset = 0)
	{
		var r = new int[str.Length];
		for(int i = 0; i < str.Length; i++) r[i] = str[i].ToInt() + offset;
		return r;
	}
}


Measured memory_bytes: 75100