(async () => {
    let input = '';
    for await (const chunk of process.stdin) input += chunk;
    let lines = input.split(/\n/);
    const [h, w] = lines[0].split(/\s/).map(v => +v);
    const [ch, cw] = lines[1].split(/\s/).map(v => v - 1);
    const [dh, dw] = lines[2].split(/\s/).map(v => v - 1);
    lines = lines.slice(3, h + 3);
    const counts = lines.map(line => line.split('').slice(0, w).map(c => c == '#' ? -1 : Infinity));

    const search = (x, y, count) => {
        if(x < 0)return;
        if(x >= h)return;
        if(y < 0)return;
        if(y >= w)return;
        if(counts[x][y] > count){
            counts[x][y] = count;
            search(x, y);
        }else{
            return;
        }

        search(x - 1, y, count);
        search(x + 1, y, count);
        search(x, y - 1, count);
        search(x, y + 1, count);
        for(let i = -2; i <= 2; i++){
            for(let j = -2; j <= 2; j++){
                // if(Math.abs(i) + Math.abs(j) < 2) continue;
                search(x + i, y + j, count + 1);
            }
        }
    }
    search(ch, cw, 0);
    if(!counts[dh] || counts[dh][dw] == Infinity){
        console.log(-1);
    }else{
        console.log(counts[dh][dw]);
    }

})();
