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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

#define PI acos(-1)
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define PB push_back
#define PPB pop_back
#define PF push_front
#define PPF pop_front
#define MP make_pair
#define MT make_tuple
#define f0(i, n) for (i = 0; i < (n); ++i)
#define f1(i, n) for (i = 1; i <= (n); ++i)
#define fab(i, a, b) for (i = (a); i <= (b); ++i)
#define fr(i, b, a) for (i = (b); i >= (a); --i)
#define endl "\n"
#define ln length

using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,
	tree_order_statistics_node_update> indexed_set;
//*/
typedef long long int ll;
#define int long int

typedef vector<ll> vll;
typedef pair<ll, ll> pll;
//typedef tuple<ll, ll, ll> tll

//* utility function
inline bool compare(ll i, ll j)
{
    return i > j;
}
//*/

//* function prototypes
template< typename T>
T modpow(T number, T power, T mod_value);
void seive(ll maximum_number);
//*/

int row, column, k;
//int dx[8] = { 0,   1,  1,  1,  0,  -1, -1, -1};
//int dy[8] = {-1,  -1,  0,  1,  1,   1,  0, -1};
//            N   NE   E  SE   S   SW   W   NW
int dx[4] = { 0,       1,      0,      -1    };
int dy[4] = {-1,       0,      1,       0    };

const ll maxN = 2e5+10; const ll INF = 1e14;
const ll maxA = 1e6+10; const ll maxB = 320;

bitset<//maxN
    +10>is_prime;
vll prime;

#ifdef DEBUG
#endif // DEBUG
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ###################################################################################################################
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//ll arr[maxN+10];
string grid[maxA+10];
struct node
{
    ll y, x, direction, cost, jump;
    node(ll y=-1, ll x=-1, ll direction=-1, ll cost=1, ll jump=1)
        : y(y), x(x), direction(direction), cost(cost), jump(jump) {}
};
bool operator<(node a, node b)
{
    if (a.cost == b.cost) return a.jump > b.jump;
    return a.cost > b.cost;
}
set<pair<ll, ll >>processed;

signed main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    //seive(maxN);
    ll number_of_test_case = 1, test_case;
    //cin >> number_of_test_case;
    for (test_case = 1; test_case <= number_of_test_case; test_case++)
    {
    	//cout << "Test #" << test_case << ": ";
    	//cout << "Case " << test_case << ": ";
    	ll i, j, m, n, query_type, q; ll a, b, x, y;
        ll x1, x2, y1, y2;

    	cin >> row >> column >> k;
    	cin >> y1 >> x1 >> y2 >> x2;

    	f1(i, row)
    	{
    	    cin >> grid[i];
    	    grid[i] = "@"+grid[i]+"@";
    	}
    	f0(i, column+2) grid[0].PB('@');
    	grid[row+1] = grid[0];


    	priority_queue<node>pq;
    	f0(i, 4)
    	{
    	    ll ny = y1+dy[i], nx = x1+dx[i];
    	    if (grid[ny][nx] == '.')
            {
                pq.push(node(ny, nx, i, 1, 1));
            }
    	}
    	processed.insert(MP(y1, x1));

        ll tot_cost = -1;
    	while (pq.empty() == 0)
        {
            node curr = pq.top(); pq.pop();
            ll x, y, direction, cost, jump;
            tie(y, x, direction, cost, jump) = tie(curr.y, curr.x, curr.direction, curr.cost, curr.jump);

            if (y == y2 && x == x2)
            {
                tot_cost = cost; break;
            }
            if (processed.count(MP(y, x))) continue;
            processed.insert(MP(y, x));

            ll ny, nx, nd, nc, nj;

            // jump 1 to the same direction
            nd = direction;
            ny = y+dy[nd]; nx = x+dx[nd]; nc = cost; nj = jump+1;
            if (nj > k)
            {
                nj = 1; nc++;
            }
            if (grid[ny][nx] == '.')pq.push(node(ny, nx, nd, nc, nj));

            // jump 1 to the left direction
            nd = direction-1; if (nd < 0) nd += 4;
            ny = y+dy[nd]; nx = x+dx[nd]; nc = cost+1; nj = 1;
            if (grid[ny][nx] == '.')pq.push(node(ny, nx, nd, nc, nj));

            // jump 1 to the right direction
            nd = direction+1; if (nd >= 4) nd -= 4;
            ny = y+dy[nd]; nx = x+dx[nd]; nc = cost+1; nj = 1;
            if (grid[ny][nx] == '.')pq.push(node(ny, nx, nd, nc, nj));
        }

        cout << tot_cost << endl;
    }
    return 0;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ###################################################################################################################
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~









template< typename T>
T modpow(T number, T power, T mod_value)
{
    if (power == 0)
        return 1%mod_value;
    ll u = modpow(number,power/2,mod_value);
    u = (u*u)%mod_value;
    if (power%2 == 1)
        u = (u*number)%mod_value;
    return u;
}
void seive(ll maximum_number)
{
    ll i, j;
    is_prime.set(); is_prime.reset(0); is_prime.reset(1);
    for (i = 2; i*i <= maximum_number; i++)
    {
        if (is_prime.test(i) == true)
        {
            prime.PB(i);
            for (j = i*i; j <= maximum_number; j += i)is_prime.reset(j);
        }
    }
    for(; i <= maximum_number; i++)if (is_prime.test(i) == true)prime.PB(i);
}