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 Rust (1.42.0) (judge verdict: Time Limit Exceeded)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [graph1; $len:expr]) => {{
        let mut g = vec![vec![]; $len];
        let ab = read_value!($next, [(usize1, usize1)]);
        for (a, b) in ab {
            g[a].push(b);
            g[b].push(a);
        }
        g
    }};
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

/*
 * Dijkstra's algorithm.
 * Verified by: AtCoder ABC164 (https://atcoder.jp/contests/abc164/submissions/12423853)
 */

struct Dijkstra {
    edges: Vec<Vec<(usize, i32)>>, // adjacent list representation
}

impl Dijkstra {
    fn new(n: usize) -> Self {
        Dijkstra { edges: vec![Vec::new(); n] }
    }
    fn add_edge(&mut self, from: usize, to: usize, cost: i32) {
        self.edges[from].push((to, cost));
    }
    /*
     * This function returns a Vec consisting of the distances from vertex source.
     */
    fn solve(&self, source: usize, inf: i32) -> Vec<i32> {
        let n = self.edges.len();
        let mut d = vec![inf; n];
        // que holds (-distance, vertex), so that que.pop() returns the nearest element.
        let mut que = std::collections::VecDeque::new();
        que.push_back((0, source));
        while let Some((cost, pos)) = que.pop_front() {
            let cost = -cost;
            if d[pos] <= cost {
                continue;
            }
            d[pos] = cost;
            for &(w, c) in &self.edges[pos] {
                let newcost = cost + c;
                if d[w] > newcost {
                    d[w] = newcost + 1;
                    if c == 0 {
                        que.push_front((-newcost, w));
                    } else {
                        que.push_back((-newcost, w));
                    }
                }
            }
        }
        return d;
    }
}

fn add_edge(l1: usize, x1: usize, y1: usize,
            l2: usize, x2: usize, y2: usize,
            dist: i32,
            h: usize, w: usize,
            dijk: &mut Dijkstra, acc: &[Vec<i32>]) {
    let (xr1, xr2) = if x1 > x2 {
        (x2, x1)
    } else {
        (x1, x2)
    };
    let (yr1, yr2) = if y1 > y2 {
        (y2, y1)
    } else {
        (y1, y2)
    };
    let count = acc[xr2 + 1][yr2 + 1] - acc[xr2 + 1][yr1] - acc[xr1][yr2 + 1]
        + acc[xr1][yr1];
    assert!(count >= 0);
    if count != 0 {
        return;
    }
    dijk.add_edge(l1 * h * w + x1 * w + y1, l2 * h * w + x2 * w + y2, dist);
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (let _ = write!(out,$($format)*););
    }
    input! {
        h: usize, w: usize, k: usize,
        xy1: (usize1, usize1),
        xy2: (usize1, usize1),
        c: [chars; h],
    }
    let mut acc = vec![vec![0; w + 1]; h + 1];
    for i in 0..h {
        for j in 0..w {
            acc[i + 1][j + 1] = acc[i][j + 1] + acc[i + 1][j] - acc[i][j]
                + (if c[i][j] == '@' { 1 } else { 0 });
        }
    }
    let mut p = 1;
    while k > (1 << p) - 1 {
        p += 1;
    }
    p -= 1;
    let rem = k + 1 - (1 << p);
    // h * w * (1 + p)
    let mut dijk = Dijkstra::new(h * w * (1 + 2 * p));
    for i in 0..h {
        for j in 0..w {
            for dir in 0..2 {
                let nxt = if p == 0 { 0 } else { 1 + dir };
                add_edge(0, i, j, nxt, i, j, 1, h, w, &mut dijk, &acc);
                if dir == 0 {
                    if i >= rem {
                        add_edge(0, i, j, nxt, i - rem, j, 1, h, w, &mut dijk, &acc);
                    }
                    if i + rem < h {
                        add_edge(0, i, j, nxt, i + rem, j, 1, h, w, &mut dijk, &acc);
                    }
                } else {
                    if j >= rem {
                        add_edge(0, i, j, nxt, i, j - rem, 1, h, w, &mut dijk, &acc);
                    }
                    if j + rem < w {
                        add_edge(0, i, j, nxt, i, j + rem, 1, h, w, &mut dijk, &acc);
                    }
                }
                for l in 1..p + 1 {
                    let ld = 1 + 2 * (l - 1) + dir;
                    let nxt = (l + 1) % (p + 1);
                    let nxt = if nxt == 0 { 0 } else { 1 + 2 * (nxt - 1) + dir };
                    add_edge(ld, i, j, nxt, i, j, 0, h, w, &mut dijk, &acc);
                    let d = 1 << (l - 1);
                    if dir == 0 {
                        if i >= d {
                            add_edge(ld, i, j, nxt, i - d, j, 0, h, w, &mut dijk, &acc);
                        }
                        if i + d < h {
                            add_edge(ld, i, j, nxt, i + d, j, 0, h, w, &mut dijk, &acc);
                        }
                    } else {
                        if j >= d {
                            add_edge(ld, i, j, nxt, i, j - d, 0, h, w, &mut dijk, &acc);
                        }
                        if j + d < w {
                            add_edge(ld, i, j, nxt, i, j + d, 0, h, w, &mut dijk, &acc);
                        }
                    }
                }
            }
        }
    }
    let v1 = xy1.0 * w + xy1.1;
    let v2 = xy2.0 * w + xy2.1;
    const INF: i32 = 1 << 26;
    let sol = dijk.solve(v1, INF);
    puts!("{}\n", if sol[v2] >= INF { -1 } else { sol[v2] });
}