//  [typescript]
import * as fs from 'fs';

// main
function main(input: string[]) {
    // param
    var h: number;
    var w: number;
    var ch: number;
    var cw: number;
    var dh: number;
    var dw: number;
    var shw: string[][];
    var ans;
    // init
    [h, w] = input.shift().split(" ").map(x => Number(x));
    [ch, cw] = input.shift().split(" ").map(x => Number(x) - 1);
    [dh, dw] = input.shift().split(" ").map(x => Number(x) - 1);
    shw = [];
    for (var hx = 0; hx < h; hx++) {
        shw[hx] = ("##" + input[hx] + "##").split("");
    }
    h += 4;
    w += 4;
    ch += 2;
    cw += 2;
    dh += 2;
    dw += 2;
    shw.unshift(new Array(w).fill("#"));
    shw.unshift(new Array(w).fill("#"));
    shw.push(new Array(w).fill("#"));
    shw.push(new Array(w).fill("#"));
    // console.debug(h, w, ch, cw, dh, dw);
    // console.debug(shw.map(x => x.join("")).reduce(((x, y) => x + "\n" + y)));
    // solve
    var patn = [
        [-1, +0],
        [+0, -1],
        [+1, +0],
        [+0, +1]
    ];
    var patw = [
        [-2, -2],
        [-2, -1],
        [-2, +0],
        [-2, +1],
        [-2, +2],
        [-1, -2],
        [-1, -1],
        [-1, +1],
        [-1, +2],
        [+0, -2],
        [+0, +2],
        [+1, -2],
        [+1, -1],
        [+1, +1],
        [+1, +2],
        [+2, -2],
        [+2, -1],
        [+2, +0],
        [+2, +1],
        [+2, +2]
    ];
    var quen = [[ch, cw]];
    var quep = [];
    var warp = 0;
    loop: {
        while (true) {
            // normal
            while (0 < quen.length) {
                var cur = quen.pop();
                shw[cur[0]][cur[1]] = warp.toString();
                if (cur == [dh, dw]) break loop;
                quep.push(cur)
                for (var patnx = 0; patnx < patn.length; patnx++) {
                    var s = [cur[0] + patn[patnx][0], cur[1] + patn[patnx][1]];
                    var c = shw[s[0]][s[1]];
                    if (c == ".") quen.push(s);
                }
            }
            // warp
            while (0 < quep.length) {
                var cur = quep.pop();
                for (var patwx = 0; patwx < patw.length; patwx++) {
                    var s = [cur[0] + patw[patwx][0], cur[1] + patw[patwx][1]];
                    var c = shw[s[0]][s[1]];
                    if (c == ".") quen.push(s), shw[s[0]][s[1]] = "_";
                }
            }
            if (quen.length == 0) break loop;
            warp++;
        }
    }
    var s = [dh, dw];
    var c = shw[s[0]][s[1]];
    ans = (c == ".") ? -1 : c;
    console.debug(h, w, ch, cw, dh, dw);
    console.debug(shw.map(x => x.join("")).reduce(((x, y) => x + "\n" + y)));
    // answer
    console.log(ans);
}

// entrypoint
function entrypoint() {
    try {
        var input = fs.readFileSync("/dev/stdin", "utf8").trim().split("\n");
        console.debug = function () { };
        main(input);
    }
    catch (ex) {
        console.log("file='%s'", __filename.split("\\").slice(-1)[0]);
        const lines: string[] = [];
        // @ts-ignore
        const reader = require('readline').createInterface({
            // @ts-ignore
            input: process.stdin,
            // @ts-ignore
            output: process.stdout
        });
        reader.on('line', function (line: string) {
            lines.push(line);
        });
        reader.on('close', function () {
            input = lines;
            console.log("input=%o", input);
            console.time();
            main(input);
            console.timeEnd();
        });
    }
}
entrypoint();