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 Java (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.AbstractCollection;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author KharYusuf
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "KharYusuf", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastReader in = new FastReader(inputStream);
            PrintWriter out = new PrintWriter(outputStream);
            FPondSkater solver = new FPondSkater();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class FPondSkater {
        int[] dx = {1, -1, 0, 0};
        int[] dy = {0, 0, 1, -1};

        public void solve(int testNumber, FastReader s, PrintWriter w) {
            int h = s.nextInt(), wi = s.nextInt(), k = s.nextInt();
            int x1 = s.nextInt() - 1, y1 = s.nextInt() - 1, x2 = s.nextInt() - 1, y2 = s.nextInt() - 1;
            char[][] c = new char[h][];
            for (int i = 0; i < h; i++) c[i] = s.next().toCharArray();
            PriorityQueue<pair<pair<Integer, Integer>, dist>> l = new PriorityQueue<>(new Comparator<pair<pair<Integer, Integer>, dist>>() {

                public int compare(pair<pair<Integer, Integer>, dist> o1, pair<pair<Integer, Integer>, dist> o2) {
                    return Integer.compare(o1.y.z, o2.y.z);
                }
            });
            int[][][] dis = new int[h][wi][4];
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < wi; j++) Arrays.fill(dis[i][j], Integer.MAX_VALUE);
            }
            for (int i = 0; i < 4; i++) {
                l.add(new pair<>(new pair<>(x1, y1), new dist(i, k, 0)));
                dis[x1][y1][i] = 0;
            }
            while (!l.isEmpty()) {
                pair<pair<Integer, Integer>, dist> cur = l.poll();
                if (cur.y.z != dis[cur.x.x][cur.x.y][cur.y.x]) continue;
                for (int i = 0; i < 4; i++) {
                    int xx = cur.x.x + dx[i], yy = cur.x.y + dy[i];
                    if (xx >= 0 && xx < h && yy >= 0 && yy < wi && c[xx][yy] != '@') {
                   /* if(xx == 2 && yy == 3 ) {
                        w.println(cur.y.x+" "+cur.y.y+" "+cur.y.z+" "+i);
                    }
                    if(xx == 1 && yy == 3 ) {
                        w.println(cur.y.x+"y "+cur.y.y+" "+cur.y.z+" "+i);
                    }*/
                        if (i == cur.y.x && cur.y.y < k) {
                            if (dis[xx][yy][i] > cur.y.z) {
                                dis[xx][yy][i] = cur.y.z;
                                //w.println(xx+" "+yy);
                                l.add(new pair<>(new pair<>(xx, yy), new dist(i, cur.y.y + 1, dis[xx][yy][i])));
//                            for (int j = 0; j < 4; j++)
//                                if (i != j) l.add(new pair<>(new pair<>(xx, yy), new dist(j, 1, dis[xx][yy] + 1)));
                            }
                        } else {
                            if (dis[xx][yy][i] > cur.y.z + 1) {
                                dis[xx][yy][i] = cur.y.z + 1;
                                l.add(new pair<>(new pair<>(xx, yy), new dist(i, 1, dis[xx][yy][i])));
                            }
                        }
                    }
                }
            }
        /*r (int i = 0; i < h; i++) {
            for (int j = 0; j < wi; j++) for (int x = 0; x < 4; x++) w.print(dis[i][j][x]+" ");
            w.println();
        }*/
            w.println(func.min(dis[x2][y2]) == Integer.MAX_VALUE ? -1 : func.min(dis[x2][y2]));
        }

    }

    static class pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<pair<U, V>> {
        public U x;
        public V y;

        public pair(U x, V y) {
            this.x = x;
            this.y = y;
        }

        public int compareTo(pair<U, V> other) {
            int i = x.compareTo(other.x);
            if (i != 0) return i;
            return y.compareTo(other.y);
        }

        public String toString() {
            return x.toString() + " " + y.toString();
        }

        public boolean equals(Object obj) {
            if (this.getClass() != obj.getClass()) return false;
            pair<U, V> other = (pair<U, V>) obj;
            return x.equals(other.x) && y.equals(other.y);
        }

        public int hashCode() {
            return 31 * x.hashCode() + y.hashCode();
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {

            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {

                curChar = 0;

                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }

            return buf[curChar++];
        }

        public int nextInt() {

            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public String next() {

            int c = read();

            while (isSpaceChar(c))
                c = read();

            StringBuilder res = new StringBuilder();

            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c) {

            if (filter != null)
                return filter.isSpaceChar(c);

            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class func {
        public static int min(int... v) {
            int min = Integer.MAX_VALUE;
            for (int i : v) min = Math.min(min, i);
            return min;
        }

    }

    static class dist implements Comparable<dist> {
        public int x;
        public int y;
        public int z;

        public dist(int xi, int yi, int zi) {
            x = xi;
            y = yi;
            z = zi;
        }

        public int compareTo(dist other) {
            if (this.z > other.z) {
                return 1;
            }
            if (this.z < other.z) {
                return -1;
            }
            return 0;
        }

    }
}