import java.io.*;
import java.util.*;

public class Main {
    private static final boolean N_CASE = false;

    private static final int[][] D = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

    private static class Node implements Comparable<Node> {
        int x, y;
        int d;

        public Node(int x, int y, int d) {
            this.x = x;
            this.y = y;
            this.d = d;
        }

        @Override
        public int compareTo(Node node) {
            return d - node.d;
        }
    }

    private boolean isValid(int H, int W, int x, int y) {
        return x >= 0 && x < H && y >= 0 && y < W;
    }

    private void solve() {
        int H = sc.nextInt();
        int W = sc.nextInt();
        int CH = sc.nextInt() - 1;
        int CW = sc.nextInt() - 1;
        int DH = sc.nextInt() - 1;
        int DW = sc.nextInt() - 1;

        String[] M = new String[H];
        for (int i = 0; i < H; i++) {
            M[i] = sc.next();
        }

        PriorityQueue<Node> q = new PriorityQueue<>();
        q.add(new Node(CH, CW, 0));

        boolean[][] vis = new boolean[H][W];
        while (!q.isEmpty()) {
            Node u = q.poll();
            if (vis[u.x][u.y]) {
                continue;
            }

            vis[u.x][u.y] = true;
            if (u.x == DH && u.y == DW) {
                out.println(u.d);
                return;
            }

            for (int[] d : D) {
                int dx = u.x + d[0];
                int dy = u.y + d[1];

                if (isValid(H, W, dx, dy) && !vis[dx][dy] && M[dx].charAt(dy) == '.') {
                    q.add(new Node(dx, dy, u.d));
                }
            }

            for (int x = u.x - 2; x <= u.x + 2; ++x) {
                for (int y = u.y - 2; y <= u.y + 2; ++y) {
                    if (isValid(H, W, x, y) && !vis[x][y] && M[x].charAt(y) == '.') {
                        q.add(new Node(x, y, u.d + 1));
                    }
                }
            }
        }

        out.println(-1);
    }

    private void run() {
        int T = N_CASE ? sc.nextInt() : 1;
        for (int t = 0; t < T; ++t) {
            solve();
        }
    }

    private static MyWriter out;
    private static MyScanner sc;

    private static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        private MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextInt();
            }
            return a;
        }

        int[][] nextIntArray(int n, int m) {
            int[][] a = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
            return a;
        }

        long[][] nextLongArray(int n, int m) {
            long[][] a = new long[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    a[i][j] = nextLong();
                }
            }
            return a;
        }

        List<Integer> nextList(int n) {
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                list.add(nextInt());
            }
            return list;
        }

        List<Long> nextLongList(int n) {
            List<Long> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                list.add(nextLong());
            }
            return list;
        }

        char[] nextCharArray(int n) {
            return sc.next().toCharArray();
        }

        char[][] nextCharArray(int n, int m) {
            char[][] c = new char[n][m];
            for (int i = 0; i < n; i++) {
                String s = sc.next();
                for (int j = 0; j < m; j++) {
                    c[i][j] = s.charAt(j);
                }
            }
            return c;
        }
    }

    private static class MyWriter extends PrintWriter {
        private MyWriter(OutputStream outputStream) {
            super(outputStream);
        }

        void printArray(int[] a) {
            for (int i = 0; i < a.length; ++i) {
                print(a[i]);
                print(i == a.length - 1 ? '\n' : ' ');
            }
        }

        void printlnArray(int[] a) {
            for (int v : a) {
                println(v);
            }
        }

        void printList(List<Integer> list) {
            for (int i = 0; i < list.size(); ++i) {
                print(list.get(i));
                print(i == list.size() - 1 ? '\n' : ' ');
            }
        }

        void printlnList(List<Integer> list) {
            list.forEach(this::println);
        }
    }

    private <T> List<List<T>> createGraph(int n) {
        List<List<T>> g = new ArrayList<>();
        for (int i = 0; i < n; ++i) {
            g.add(new ArrayList<>());
        }
        return g;
    }

    public static void main(String[] args) {
        out = new MyWriter(new BufferedOutputStream(System.out));
        sc = new MyScanner();
        new Main().run();
        out.close();
    }
}

Measured memory_bytes: 61484