func main() {
    let hw = readLine()!.split(separator: " ").map{Int($0)!}
    let (h, w) = (hw[0], hw[1])
    let cArray = readLine()!.split(separator: " ").map{Int($0)!}
    let ch = cArray[0]
    let cw = cArray[1]
    let dArray = readLine()!.split(separator: " ").map{Int($0)!}
    let dh = dArray[0]
    let dw = dArray[1]
    var maze = [Int]()
    for _ in 0..<h {
        let line = readLine()!.map{$0 == "#" ? 1 : 0}
        maze += line
    }
    let walk = [(01,00), (-1,00), (00,01), (00,-1)]
    
    let warp = [(-2,02), (-2,01), (-2,00), (-2,-1), (-2,-2),
                (-1,02), (-1,01),          (-1,-1), (-1,-2),
                (00,02),                            (00,-2),
                (01,02), (01,01),          (01,-1), (02,-2),
                (02,02), (02,01), (02,00), (02,-1), (02,-2)
                ]
    
    var dist = 0
    var set1 = Set<Int>()
    func indexing(a: Int, b: Int) -> Int {
        return w*(a-1) + (b-1)
    }
    let start = indexing(a: ch, b: cw)
    let goal = indexing(a: dh, b: dw)
    set1.insert(start)
    var set2 = Set<Int>() // walked
    var set3 = Set<Int>() // warped
    while true {
        let one = set1.popFirst()!
        set2.insert(one)
        if one == goal {
            break
        }
        for each in walk {
            let x = one / w + each.0
            let y = one % w + each.1
            if x >= 0 && x < h && y >= 0 && y < w {
                if !set2.contains(one + w*each.0 + each.1) && maze[one + w*each.0 + each.1] == 0 {
                    set1.insert(one + w*each.0 + each.1)
                }
            }
        }
        if set1.isEmpty {
            dist += 1
            set1 = set2
            for item in set2 {
                for each in warp {
                    let x = item / w + each.0
                    let y = item % w + each.1
                    if x >= 0 && x < h && y >= 0 && y < w {
                        if !set1.contains(item + w*each.0 + each.1) && maze[item + w*each.0 + each.1] == 0 {
                            set3.insert(item + w*each.0 + each.1)
                        }
                    }
                }
            }
            if set3.isEmpty {
                dist = -1
                break
            }
            set1 = set3
            set2 = Set<Int>()
            set3 = Set<Int>()
        }
    }
    print(dist)
}
main()
