Problem:
Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

1 \leq H,W,K \leq 10^6

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:

From (3, 2), go west one square to (3, 1).

From (3, 1), go north two squares to (1, 1).

From (1, 1), go east two squares to (1, 3).

From (1, 3), go east one square to (1, 4).

From (1, 4), go south two squares to (3, 4).

Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Console;
using static System.Math;
using static MyIO;
using static MyUtil;

public class F { public static void Main() => (new Solver()).Solve(); }

public class Solver
{
	private const long INF = (long)1e18;
	private long[] di = new long[] { -1, 0, 1, 0 };
	private long[] dj = new long[] { 0, -1, 0, 1 };

	private long H;
	private long W;
	private long K;
	private long si;
	private long sj;
	private long ti;
	private long tj;
	private char[,] grid;
	private long[] dist;
	private PriorityQueue<(long, long)> pq;

	public void Solve()
	{
		H = GetLong();
		W = GetLong();
		K = GetLong();
		si = GetLong() - 1;
		sj = GetLong() - 1;
		ti = GetLong() - 1;
		tj = GetLong() - 1;
		grid = new char[H, W];
		for(long i = 0; i < H; i++)
		{
			var c = GetString().ToCharArray();
			for(long j = 0; j < W; j++)
				grid[i, j] = c[j];
		}

		dist = Enumerable.Repeat(INF, (int)(H * W * 4)).ToArray();
		pq = new PriorityQueue<(long, long)>(H * W * 4, (x, y) => x.CompareTo(y));

		for(long v = 0; v < 4; v++)
			push(si, sj, v, 0);

		while(pq.Count > 0)
		{
			var (d, id) = pq.Pop();
			if(dist[id] != d)
				continue;

			long i = id / W / 4;
			long j = (id / 4) % W;
			long v = id % 4;
			for(long nv = 0; nv < 4; nv++)
				push(i, j, nv, modCeil(d));

			long ni = i + di[v];
			long nj = j + dj[v];
			if(0 <= ni && ni < H && 0 <= nj && nj < W)
			{
				if(grid[ni, nj] == '.')
					push(ni, nj, v, d + 1);
			}
		}

		long ans = INF;
		for(long v = 0; v < 4; v++)
			chmin(ref ans, dist[toId(ti, tj, v)]);

		if(ans == INF)
			WriteLine(-1);
		else
		{
			ans = modCeil(ans) / K;
			WriteLine(ans);
		}
	}

	private long toId(long i, long j, long v) => (i * W + j) * 4 + v;

	private void push(long i, long j, long v, long x)
	{
		long id = toId(i, j, v);
		if(dist[id] <= x)
			return;

		dist[id] = x;
		pq.Push((x, id));
	}

	private long modCeil(long x) => ((x + K - 1) / K) * K;	
}

public class PriorityQueue<T>
{
	public long Count { get; private set; } = 0;
	public long MaxSize { get; private set; } = 0;
	private T[] m_heap;
	private Comparison<T> Comp = null;
	
	public PriorityQueue(long maxSize, Comparison<T> comp)
	{
		if(maxSize <= 0)
			throw new Exception();
		MaxSize = maxSize;
		m_heap = new T[maxSize];

		Comp = comp;
	}
	
	public void Push(T x)
	{
		if(Count == MaxSize)
		{
			T[] new_heap = new T[MaxSize << 1];
			Array.Copy(m_heap, new_heap, MaxSize);
			m_heap = new_heap;
			MaxSize <<= 1;
		}
		
		long i = Count++;
		while(i > 0)
		{
			long p = (i - 1)/2;
			if(Comp(m_heap[p], x) <= 0)
				break;
			m_heap[i] = m_heap[p];
			i = p;			
		}
		m_heap[i] = x;		
	}
	
	public T Pop()
	{
		if(Count == 0)
			throw new Exception("Queue is empty.");
		
		T result = m_heap[0];
		T x = m_heap[--Count];
		long i = 0;
		while(i * 2 + 1 < Count)
		{
			long c1 = i * 2 + 1, c2 = i * 2 + 2;
			if(c2 < Count && Comp(m_heap[c2], m_heap[c1]) < 0)
				c1 = c2;
			if(Comp(m_heap[c1], x) >= 0)
				break;
			m_heap[i] = m_heap[c1];
			i = c1;
		}
		m_heap[i] = x;
		
		return result;		
	}
	
	public T Peek() => m_heap[0];
}

public static class MyIO
{
	private static string[] args = new string[0];
	private static int cnt = 0;
	private static IEnumerable<string> getArgs(long N) => Range(0, N).Select(_ => getArg());
	private static string getArg()
	{
		if(cnt == args.Length) { args = Console.ReadLine().Split(); cnt = 0; }
		return args[cnt++];
	}

	public static int GetInt() => int.Parse(getArg());
	public static long GetLong() => long.Parse(getArg());
	public static double GetDouble() => double.Parse(getArg());
	public static decimal GetDecimal() => decimal.Parse(getArg());
	public static string GetString() => getArg();
	public static char GetChar() => getArg()[0];
	public static int[] GetInts(long N) => getArgs(N).Select(int.Parse).ToArray();
	public static long[] GetLongs(long N) => getArgs(N).Select(long.Parse).ToArray();
	public static double[] GetDoubles(long N) => getArgs(N).Select(double.Parse).ToArray();
	public static decimal[] GetDecimals(long N) => getArgs(N).Select(decimal.Parse).ToArray();
	public static string[] GetStrings(long N) => getArgs(N).ToArray();
}

public static class MyUtil
{
	public static void chmax<T>(ref T x, T y) where T : IComparable<T> { if(x.CompareTo(y) < 0) x = y; }
	public static void chmin<T>(ref T x, T y) where T : IComparable<T> { if(x.CompareTo(y) > 0)	x = y; }
	public static void swap<T>(ref T x, ref T y) { T tmp = x; x = y; y = tmp; }
	public static long rightShift(long x, long y) => (x >> (int)y);
	public static long leftShift(long x, long y) => (x << (int)y);
	public static bool contains(long s, long x) => (rightShift(s, x) & 1L) == 1L;
	public static long gcd(long x, long y) => (y == 0) ? x : gcd(y, x % y);
	public static long lcm(long x, long y) => (x / gcd(x,y)) * y;	
	public static IEnumerable<int> Range(long start, long count) => Enumerable.Range((int)start, (int)count);
	public static void Sort(this List<string> source) => source.Sort(StringComparer.Ordinal);
}