import java.io.BufferedReader import java.io.InputStream import java.io.InputStreamReader import java.io.PrintWriter import java.lang.StringBuilder import java.util.* fun PrintWriter.solve(sc: FastScanner) { val h = sc.nextInt() val w = sc.nextInt() val sy = sc.nextInt() - 1 val sx = sc.nextInt() - 1 val gy = sc.nextInt() - 1 val gx = sc.nextInt() - 1 val map = Array(h) { Array(w) { '.' } } for (i in 0 until h) { val s = sc.nextLine() for (j in 0 until w) { map[i][j] = s[j] } } val visited = Array(h) { Array(w) { false } } val que: Queue> = ArrayDeque() que.add(Triple(sy, sx, 0)) val d = arrayOf(1, 0, -1, 0, 1) while (que.count() > 0) { val (i, j, step) = que.poll() if (i == gy && j == gx) { println(step) return } for (k in 0 until 4) { val ny = i + d[k] val nx = j + d[k + 1] if (ny in 0 until h && nx in 0 until w && !visited[ny][nx] && map[ny][nx] == '.') { visited[ny][nx] = true que.add(Triple(ny, nx, step + 1)) } } } } fun main(args: Array) { val writer = PrintWriter(System.out, false) writer.solve(FastScanner(System.`in`)) writer.flush() } class FastScanner(s: InputStream) { private var st = StringTokenizer("") private val br = BufferedReader(InputStreamReader(s)) fun next(): String { while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) return st.nextToken() } fun nextInt() = next().toInt() fun nextLong() = next().toLong() fun nextLine() = br.readLine() fun nextDouble() = next().toDouble() fun ready() = br.ready() }