#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<iomanip>
#include<bitset>
#include<utility>
#include<list>
#include<map>
#include<set>
#include<cstring>
#include<sstream>
#include<cctype>
using namespace std;
const double EPS=1e-5;
const double INF=1e9;
typedef complex<double>P;
namespace std{
    bool operator<(const P& a,const P& b){
        return real(a)!=real(b)?real(a)<real(b):imag(a)<imag(b);
    }
}
#define EQ(a,b) (fabs((a)-(b))<EPS)
#define EQV(a,b) (EQ(real(a),real(b))&&EQ(imag(a),imag(b)))
double cross(const P &a,const P &b){
    return imag(conj(a)*b);
}
double dot(const P &a,const P &b){
    return real(conj(a)*b);
}

struct L:public vector<P>{
    L(const P &a,const P &b){
        push_back(a);push_back(b);
    }
    L(){}
};

typedef vector<P>G;

struct C{
    P p;double r;
    C(const P &p_,double r_):p(p_),r(r_){}
    C(){}
};

int ccw(P a,P b,P c){
    b-=a;c-=a;
    if(cross(b,c)>0)return 1;///counter clockwise
    if(cross(b,c)<0)return -1;///clockwise
    if(dot(b,c)<0)return 2;///c--a--b on line
    if(norm(b)<norm(c))return -2;///a--b--c on line
    return 0;
}
bool orthogonal(P &p,P &q){
    return EQ(dot(p,q),0.0);
}
bool intersectLL(const L &l,const L &m){
    return abs(cross(l[1]-l[0],m[1]-m[0]))>EPS||///non-parallel
        abs(cross(l[1]-l[0],m[0]-l[0]))<EPS;///same line
}
bool intersectLS(const L &l,const L &s){
    return cross(l[1]-l[0],s[0]-l[0])*///s[0] is left of l
        cross(l[1]-l[0],s[1]-l[0])<EPS;///s[1] is right of l
}
bool intersectLP(const L &l,const P &p){
    return abs(cross(l[1]-p,l[0]-p))<EPS;
}
bool intersectSS(const L &s,const L &t){
    return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0&&
        ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0;
}
bool intersectSP(const L &s,const P &p){
    return abs(abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]))<EPS;
    ///triangle inequality
}
P projection(const L &l,const P &p){
    double t=dot(p-l[0],l[0]-l[1])/norm(l[0]-l[1]);
    return l[0]+t*(l[0]-l[1]);
}
P reflection(const L &l,const P &p){
    return p+(projection(l,p)-p)*2.0;
}
double distanceLP(const L &l,const P &p){
    return abs(p-projection(l,p));
}
double distanceLL(const L &l,const L &m){
    return intersectLL(l,m)?0:distanceLP(l,m[1]);
}
double distanceLS(const L &l,const L &s){
    if(intersectLS(l,s))return 0;
    return min(distanceLP(l,s[0]),distanceLP(l,s[1]));
}
double distanceSP(const L &s,const P &p){
    const P r=projection(s,p);
    if(intersectSP(s,r))return abs(r-p);
    return min(abs(s[0]-p),abs(s[1]-p));
}
double distanceSS(const L &s,const L &t){
    if(intersectSS(s,t))return 0;
    return min(min(distanceSP(s,t[0]),distanceSP(s,t[1])),
            min(distanceSP(t,s[0]),distanceSP(t,s[1])));
}
P crossPoint(const L &l,const L &m){
    double A=cross(l[1]-l[0],m[1]-m[0]);
    double B=cross(l[1]-l[0],l[1]-m[0]);
    if(abs(A)<EPS&&abs(B)<EPS)return m[0];///same line
    ///if(abs(A)<EPS)assert(false);
    return m[0]+B/A*(m[1]-m[0]);

}

P extreme(const G &g,const L &l){
    int k=0;
    for(int i=1;i<g.size();i++){
        if(dot(g[i],l[1]-l[0])>dot(g[k],l[1]-l[0]))k=i;
    }
    return g[k];
}

G andrewScan(G s){
    G u,l;
    if(s.size()<3)return s;
    sort(s.begin(),s.end());
    u.push_back(s[0]);
    u.push_back(s[1]);

    l.push_back(s[s.size()-1]);
    l.push_back(s[s.size()-2]);

    for(int i=2;i<s.size();i++){
        for(int n=u.size();n>=2&&ccw(u[n-2],u[n-1],s[i])!=-1;n--){
            u.pop_back();
        }
        u.push_back(s[i]);
    }

    for(int i=s.size()-3;i>=0;i--){
        for(int n=l.size();n>=2&&ccw(l[n-2],l[n-1],s[i])!=-1;n--){
            l.pop_back();
        }
        l.push_back(s[i]);
    }
    reverse(l.begin(),l.end());
    for(int i=u.size()-2;i>=1;i--)l.push_back(u[i]);
    return l;
}
/*
IN 2
ON 1
OUT 0
*/
int contains(G g,P p){
    int n=g.size();
    bool x=false;
    for(int i=0;i<n;i++){
        P a=g[i]-p,b=g[(i+1)%n]-p;
        if(abs(cross(a,b))<EPS&&dot(a,b)<EPS)return 1;
        if(imag(a)>imag(b))swap(a,b);
        if(imag(a)<EPS&&EPS<imag(b)&&cross(a,b)>EPS)x=!x;
    }
    return (x?2:0);
}

int main(){
    int N;
    scanf("%d",&N);
    int mi=1e9;
    int a,b,c,d;
    scanf("%d%d%d%d",&a,&b,&c,&d);
    a--;b--;c--;d--;
    for(int w=1;w<=N;w++){
        int dist1=(abs(b/w-a/w)+abs(b%w-a%w));
        int dist2=(abs(d/w-c/w)+abs(d%w-c%w));
        mi=min(mi,dist1+dist2);
    }
    printf("%d\n",mi);
    return 0;
}

Measured memory_bytes: 1196