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;
using System.Net;
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 char[][] bm;
	static int[][] b;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int h=s[0],w=s[1];
		char c='@';
		var g=sc.Ia;
		bm=new char[h+2][];
		b=new int[h+2][];
		var bo=new bool[h+2][];
		var oa=new int[h+2][][];
		for(int i = 1;i<=h;i++) {
			bm[i]=(c+sc.S+c).ToCharArray();
			b[i]=new int[w+2];
			bo[i]=new bool[w+2];
			oa[i]=new int[w+2][];
			for(int j = 1;j<=w;j++) {oa[i][j]=new int[4];}
		}
		bm[0]=bm[h+1]=new string(c,w+2).ToCharArray();
		b[0]=b[h+1]=new int[w+2];
		var qu=new Qu2<Tuple<int,int>>();
		qu.Enqueue0(1,Tuple.Create(g[0],g[1]));
		b[g[0]][g[1]]=1;
		while(qu.cnt>0){
			var e=qu.Dequeue;
			if(e.d.Item1==g[2]&&e.d.Item2==g[3]){
				Console.WriteLine(e.n-1);
				return;
			}
			if(b[e.d.Item1][e.d.Item2]!=e.n){continue;}
			Fdi(e.d.Item1,e.d.Item2,(y,x,o)=>{
				if(bm[y][x]=='@'){return;}
				if(oa[e.d.Item1][e.d.Item2][o]==0){
					if(b[y][x]==0||b[y][x]==e.n+1){
						if(b[y][x]==0){
							qu.Enqueue1(e.n+1,Tuple.Create(y,x));
							b[y][x]=e.n+1;
						}
						oa[y][x][o]=Max(oa[y][x][o],s[2]-1);
					}
				}
				else{
					if(b[y][x]==0||(b[y][x]==e.n&&!bo[e.d.Item1][e.d.Item2])){
						qu.Enqueue0(e.n,Tuple.Create(y,x));
						b[y][x]=e.n;
						oa[y][x][o]=Max(oa[y][x][o],oa[e.d.Item1][e.d.Item2][o]-1);
					}
					else if(b[y][x]==e.n+1){
						qu.Enqueue0(e.n,Tuple.Create(y,x));
						b[y][x]=e.n;
						oa[y][x]=new int[4];
						oa[y][x][o]=Max(oa[y][x][o],oa[e.d.Item1][e.d.Item2][o]-1);
					}
				}
			});
			bo[e.d.Item1][e.d.Item2]=true;
		}
		Console.WriteLine("{0}",-1);
	}
	static void Fdi(int y,int x,Action<int,int,int> f){for(int i=0;i<4;i++){f(y+di1[i][0],x+di1[i][1],i);}}
}
public class Qu2<T>{
	public class Dt{
		public int n;
		public T d;
		public Dt(int n,T d){this.n=n;this.d=d;}
		public override string ToString()=>"n:"+n.ToString()+" d:"+d.ToString();
	}
	private Queue<Dt> qu0=new Queue<Dt>(),qu1=new Queue<Dt>();
	public void Enqueue(bool b,int n,T d){
		if(b){qu0.Enqueue(new Dt(n,d));}
		else{qu1.Enqueue(new Dt(n+1,d));}
	}
	public void Enqueue0(int n,T d){qu0.Enqueue(new Dt(n,d));}
	public void Enqueue1(int n,T d){qu1.Enqueue(new Dt(n,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;}
}
public class Sct{
	private StreamReader sr;
	public Sct(string t){sr=new StreamReader(t);}
	public int I{get{return int.Parse(sr.ReadLine());}}
	public long L{get{return long.Parse(sr.ReadLine());}}
	public double D{get{return double.Parse(sr.ReadLine());}}
	public string S{get{return sr.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(sr.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(sr.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(sr.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return sr.ReadLine().Split();}}
	public object[] Oa{get{return sr.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+sr.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+sr.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+sr.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+sr.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+sr.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+sr.ReadLine()+(c?" "+d.ToString():"")).Split(),long.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(sr.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,sr.ReadLine().Split());}return a;}
}

Measured memory_bytes: 108020