#include<bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n;
void rot(int &x, int &y){
	int nx = y, ny = n - x - 1;
	x = nx; y = ny;
}
int main(){
	cin >> n;
	int x1, y1, z1, x2, y2, z2, upper = inf;
	cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
	/*
	n = 1000;
	x1 = rand() % n;
	y1 = rand() % n;
	z1 = rand() % n;
	x2 = rand() % n;
	y2 = rand() % n;
	z2 = rand() % n;
	*/
	
	//for A*
	set<pair<pi, int>> goal;
	goal.insert(mp(mp(x2, y2), z2));
	while(1){
		bool update = 0;
		for(auto i : goal){
			int x = i.first.first, y = i.first.second, z = i.second;
			rep(it, 12){
				if(it < 4)  rot(x, y);
				if(it < 8)  rot(y, z);
				if(it < 12) rot(z, x);
				if(goal.count(mp(mp(x, y), z))) continue;
				update = 1;
				goal.insert(mp(mp(x, y), z));
			}
		}
		if(!update) break;
	}
	vector<pair<pi, int>> goalv(all(goal));
	auto estimate = [&](int x, int y, int z){
		//return 0;
		int mn = inf;
		for(const auto &i : goalv) mn = min(mn,
			abs(x - i.first.first) + abs(y - i.first.second) + abs(z - i.second));
		return mn;
	};
	upper = abs(x2 - x1) + abs(y2 - y1) + abs(z2 - z1);
	
	//rep(i, 8) goal.insert(mp(mp(i & 1 ? 0 : n - 1, i & 2 ? 0 : n - 1), i & 4 ? 0 : n - 1));
	
	
	//dbg(goal.size());
	
	unordered_set<ll> v;//x, y, z
	priority_queue<pair<ll, ll>> q; //estcost+cost, cost, x, y, z
	
#define PUSH(x, y, z, c) if(!v.count(x*(ll)n*n + y*(ll)n + z)){\
	q.push(mp( - (c), x*(ll)n*n + y*(ll)n + z));}
	
	PUSH(x1, y1, z1, 0);
	
	int cnt = 0;
	
	while(!q.empty()){
		int c =  (-q.top().first), y = q.top().second / n % n;
		int x = q.top().second / n / n,      z = q.top().second % n; q.pop();
		
		if(v.count(x*(ll)n*n + y*(ll)n + z)) continue;
		v.insert(x*(ll)n*n + y*(ll)n + z);
		
		/*
		if(++cnt % 10000 == 0){
			cerr<<c<<" "<<x<<" "<<y<<" "<<z<<"  goal: "<<x2<<" "<<y2<<" "<<z2<<endl;
			//dbg(estimate(x, y, z));
		}
		*/
		upper = min(upper, c + abs(x - x2) + abs(y - y2) + abs(z - z2));
		
		if(x == x2 && y == y2 && z == z2){
			//dbg(cnt);
			cout << c << endl;
			return 0;
		}
		/*
		rep(d, 6){
			const int dx[] = {-1, 0, 1, 0, 0, 0};
			const int dy[] = {0, -1, 0, 1, 0, 0};
			const int dz[] = {0, 0, 0, 0, 1, -1};
			int nx = x + dx[d], ny = y + dy[d], nz = z + dz[d];
			if(nx < 0 || nx >= n || ny < 0 || ny >= n || nz < 0 || nz >= n) continue;
			PUSH(nx, ny, nz, c + 1);
		}
		*/
		for(auto i : goal) rep(j, 3){
			int nx = x, ny = y, nz = z;
			if(j == 0) nx = i.first.first;
			if(j == 1) ny = i.first.second;
			if(j == 2) nz = i.second;
			PUSH(nx, ny, nz, c + abs(nx - x) + abs(ny - y) + abs(nz - z));
		}
		
		int nx = x, ny = y, nz = z;
		rep(i, 3){
			rot(nx, ny);
			if(i != 1) PUSH(nx, ny, nz, c + (z != z2 ? 1 : 3));
		}
		nx = x; ny = y; nz = z;
		rep(i, 3){
			rot(nx, nz);
			if(i != 1) PUSH(nx, ny, nz, c + (y != y2 ? 1 : 3));
		}
		nx = x; ny = y; nz = z;
		rep(i, 3){
			rot(ny, nz);
			if(i != 1) PUSH(nx, ny, nz, c + (x != x2 ? 1 : 3));
		}
	}
	return 0;
}

Measured memory_bytes: 3496