import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int h = sc.nextInt(); int w = sc.nextInt(); int srr = sc.nextInt() - 1; int scc = sc.nextInt() - 1; int grr = sc.nextInt() - 1; int gcc = sc.nextInt() - 1; char[][] field = new char[h][]; for (int i = 0; i < h; i++) { field[i] = sc.next().toCharArray(); } PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(srr, scc, 0, 0)); int[][][] costs = new int[h][w][6]; for (int[][] arr1 : costs) { for (int[] arr2 : arr1) { Arrays.fill(arr2, Integer.MAX_VALUE); } } while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.r][p.c][p.type] <= p.value || field[p.r][p.c] == '#') { continue; } costs[p.r][p.c][p.type] = p.value; if (p.r > 0 && field[p.r - 1][p.c] == '.') { queue.add(p.add(-1, 0)); } if (p.r < h - 1 && field[p.r + 1][p.c] == '.') { queue.add(p.add(1, 0)); } if (p.c > 0 && field[p.r][p.c - 1] == '.') { queue.add(p.add(0, -1)); } if (p.c < w - 1 && field[p.r][p.c + 1] == '.') { queue.add(p.add(0, 1)); } } if (costs[grr][gcc][0] == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(costs[grr][gcc][0]); } } static class Path implements Comparable { int r; int c; int type; int value; public Path(int r, int c, int type, int value) { this.r = r; this.c = c; this.type = type; this.value = value; } public int compareTo(Path another) { return value - another.value; } public Path add(int x, int y) { int next; if (x == -1) { if (type < 4) { next = (type + 3) % 4; } else { next = type; } } else if (x == 1) { if (type < 4) { next = (type + 1) % 4; } else { next = type; } } else if (y == -1) { if (type == 1 || type == 3) { next = type; } else if (type == 0) { next = 4; } else if (type == 2) { next = 5; } else if (type == 4) { next = 2; } else { next = 0; } } else { if (type == 1 || type == 3) { next = type; } else if (type == 0) { next = 5; } else if (type == 2) { next = 4; } else if (type == 4) { next = 0; } else { next = 2; } } return new Path(r + x, c + y, next, value + 1); } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }