import std.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;

void main() {
    int n;
    int a, b, c, d;
    readf("%d\n", &n);
    readf("%d %d\n", &a, &b); a--; b--;
    readf("%d %d\n", &c, &d); c--; d--;
    int ans = int.max;
    for (int w = 1; w <= n; w++) {
        int ax = a % w, ay = a / w;
        int bx = b % w, by = b / w;
        int cx = c % w, cy = c / w;
        int dx = d % w, dy = d / w;
        ans = min(ans, abs(ax - bx) + abs(ay - by) + abs(cx - dx) + abs(cy - dy));
    }
    ans.writeln;
}