#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
using P=pair<int,int>;
using PP=pair<P,int>;

int H,W,K;
int index(int a,int b){return a*W+b;}
struct node{
    vector<int> to;
    vector<int> cost;
    P min_cost;
    node(){
        min_cost=make_pair(INF,INF);
    }
};
void dijkstra(int start_index,vector<node> &nodes){
    priority_queue<PP,vector<PP>,greater<PP>> q;
    nodes[start_index].min_cost=P(1,0);
    q.push(PP(P(0,0),start_index));
    while (q.size()!=0)
    {
        PP p=q.top();
        q.pop();
        int v=p.second;
        if(nodes[v].min_cost<p.first)continue;
        for (int i = 0; i < nodes[v].to.size(); i++){
            int to=nodes[v].to[i];
            int tmp_cost=nodes[v].min_cost.first+nodes[v].cost[i];
            int direct=p.first.second;
            if(v/(H*W)==to/(H*W))direct++;
            else direct=0;
            if(direct>K){
                tmp_cost++;
                direct=1;
            }
            if(nodes[to].min_cost>P(tmp_cost,direct)){
                nodes[to].min_cost=P(tmp_cost,direct);
                q.push(PP(nodes[to].min_cost,to));
            }
        }
    }

}

int main() {
    cin>>H>>W>>K;
    int R=H*W;  int D=2*H*W; int L=3*H*W;
    vector<node> nodes(H*W*4);
    int x,y;cin>>x>>y;
    int S=index(x-1,y-1);
    cin>>x>>y;
    int G=index(x-1,y-1);
    vector<bool> lotus(H*W,false);
    for (int i = 0; i < H*W; i++){
        char c; cin>>c;
        if(c=='@')lotus[i]=true;
    }
    for (int i = 0; i < H; i++){
        for (int j = 0; j < W; j++){
            int pos=index(i,j);
            if(lotus[pos])continue;
            for (int k = 0; k < 4; k++){
                int right=k+1;if(right==4)right=0;
                nodes[pos+k*H*W].to.push_back(pos+right*H*W);
                nodes[pos+right*H*W].to.push_back(pos+k*H*W);
                nodes[pos+k*H*W].cost.push_back((pos!=S)*1);
                nodes[pos+right*H*W].cost.push_back((pos!=S)*1);
            }
            int next_pos;
            if(i!=0){
                next_pos=index(i-1,j);
                if(!lotus[next_pos]){
                    nodes[pos].to.push_back(next_pos);
                    nodes[pos].cost.push_back(0);
                }
            }
            if(i!=H-1){
                next_pos=index(i+1,j);
                if(!lotus[next_pos]){
                    nodes[pos+D].to.push_back(next_pos+D);
                    nodes[pos+D].cost.push_back(0);
                }
            }
            if(j!=0){
                next_pos=index(i,j-1);
                if(!lotus[next_pos]){
                    nodes[pos+L].to.push_back(next_pos+L);
                    nodes[pos+L].cost.push_back(0);
                }
            }
            if(j!=W-1){
                next_pos=index(i,j+1);
                if(!lotus[next_pos]){
                    nodes[pos+R].to.push_back(next_pos+R);
                    nodes[pos+R].cost.push_back(0);
                }
            }
        }
    }
    dijkstra(S,nodes);
    int ans=INF;
    for (int i = 0; i < 4; i++){
        ans=min(ans,nodes[G+i*R].min_cost.first);
    }
    if(ans==INF)ans=-1;
    cout<<ans<<endl;
    
}

Measured memory_bytes: 470768