fun main(args:Array<String>?): Unit {
    val n = readLine()!!.trim().toInt()
    val (a, b) = readLine()!!.trim().split(' ').map(String::toInt)
    val (c, d) = readLine()!!.trim().split(' ').map(String::toInt)

    println(
            (1 .. n).map{w -> Section((a - 1) % w, (a - 1) / w).manhattanDistance(Section((b - 1) % w, (b - 1) / w)) + Section((c - 1) % w, (c - 1) / w).manhattanDistance(Section((d - 1) % w, (d - 1) / w))}.min()!!
    )
}
data class Section(val x: Int, val y: Int) {
    fun manhattanDistance(from: Section): Int = Math.abs(x - from.x) + Math.abs(y - from.y)
}
