#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

struct P{
  ll x,y,z;
  P(ll a,ll b,ll c):x(a),y(b),z(c){}
};
bool operator<(const P& a,const P& b){
  if(a.x!=b.x)return a.x < b.x;
  else if(a.y!=b.y)return a.y < b.y;
  else return a.z < b.z;
}

ll n;
ll x[2],y[2],z[2];
map<P,int> dist;
map<P,bool> vis;

ll calc(ll sx,ll sy,ll sz){
  queue<P> que;
  que.push(P(sx,sy,sz));
  dist.clear(); vis.clear();
  dist[P(sx,sy,sz)]=0;
  vis[P(sx,sy,sz)]=true;
  ll res=INF;
  while(que.size()){
    P p=que.front(); que.pop();
    {

      if(p.z!=z[1]&&!vis[P(n-1-p.y,p.x,p.z)]){ // xy
        dist[P(n-1-p.y,p.x,p.z)]=dist[p]+1;
        que.push(P(n-1-p.y,p.x,p.z));
        vis[P(n-1-p.y,p.x,p.z)]=true;
      }
      if(p.z!=z[1]&&!vis[P(p.y,n-1-p.x,p.z)]){ // xy
        dist[P(p.y,n-1-p.x,p.z)]=dist[p]+1;
        que.push(P(p.y,n-1-p.x,p.z));
        vis[P(p.y,n-1-p.x,p.z)]=true;
      }

      if(p.y!=y[1]&&!vis[P(n-1-p.z,p.y,p.x)]){ // xz
        dist[P(n-1-p.z,p.y,p.x)]=dist[p]+1;
        que.push(P(n-1-p.z,p.y,p.x));
        vis[P(n-1-p.z,p.y,p.x)]=true;
      }
      if(p.y!=y[1]&&!vis[P(p.z,p.y,n-1-p.x)]){ // xz
        dist[P(p.z,p.y,n-1-p.x)]=dist[p]+1;
        que.push(P(p.z,p.y,n-1-p.x));
        vis[P(p.z,p.y,n-1-p.x)]=true;
      }

      if(p.x!=x[1]&&!vis[P(p.x,n-1-p.z,p.y)]){ // yz
        dist[P(p.x,n-1-p.z,p.y)]=dist[p]+1;
        que.push(P(p.x,n-1-p.z,p.y));
        vis[P(p.x,n-1-p.z,p.y)]=true;
      }
      if(p.x!=x[1]&&!vis[P(p.x,p.z,n-1-p.y)]){ // yz
        dist[P(p.x,p.z,n-1-p.y)]=dist[p]+1;
        que.push(P(p.x,p.z,n-1-p.y));
        vis[P(p.x,p.z,n-1-p.y)]=true;
      }
    }
  }
  for(auto p : dist){
    minch(res,abs(p.fi.x-x[1])+abs(p.fi.y-y[1])+abs(p.fi.z-z[1])+p.se);
  }
  return res;
}

int main(){
  cin>>n;
  rep(i,2){
    cin>>x[i]>>y[i]>>z[i];
  }
  ll res=INF;
  for(ll xx=x[0]-1;xx<=x[0]+1;xx++){
    for(ll yy=y[0]-1;yy<=y[0]+1;yy++){
      for(ll zz=z[0]-1;zz<=z[0]+1;zz++){
        if(xx<0||xx>=n||yy<0||yy>=n||zz<0||zz>=n)continue;
        //dbg(calc(xx,yy,zz)); dbg(xx); dbg(yy); dbg(zz);
        minch(res,calc(xx,yy,zz)+abs(xx-x[0])+abs(yy-y[0])+abs(zz-z[0]));
      }
    }
  }
  cout<<res<<endl;
  return 0;
}

