package yukiguni; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); int a = sc.nextInt(); int si = sc.nextInt(); int sj = sc.nextInt(); int b = sc.nextInt(); int gi = sc.nextInt(); int gj = sc.nextInt(); char[][] m = new char[h][]; for(int i=0;i q = new ArrayDeque<>(); q.offer(a << 16 | si << 8 | sj); dist[si][sj][a] = 0; while(!q.isEmpty()) { int p = q.poll(); int cs = p >>> 16; int ci = p >>> 8 & 0xff; int cj = p & 0xff; for(int i=0;i<4;i++) { int ni = ci + DI[i]; int nj = cj + DJ[i]; if (ni < 0 || ni >= h || nj < 0 || nj >= w) { continue; } int ns = cs + (m[ni][nj] == '*' ? 1 : -1); if (ns > max || ns <= 0 || dist[ni][nj][ns] <= dist[ci][cj][cs] + 1) { continue; } dist[ni][nj][ns] = dist[ci][cj][cs] + 1; q.offer(ns << 16 | ni << 8 | nj); } } return dist[gi][gj][b] >= 1 << 29 ? -1 : dist[gi][gj][b]; } }