Problem:
Problem E: Driving an Icosahedral Rover

After decades of fruitless efforts, one of the expedition teams of ITO (Intersolar Tourism Organization) finally found a planet that would surely provide one of the best tourist attractions
within a ten light-year radius from our solar system. The most attractive feature of the planet,
besides its comfortable gravity and calm weather, is the area called Mare Triangularis. Despite
the name, the area is not covered with water but is a great plane. Its unique feature is that it is
divided into equilateral triangular sections of the same size, called trigons. The trigons provide
a unique impressive landscape, a must for tourism. It is no wonder the board of ITO decided
to invest a vast amount on the planet.

Despite the expected secrecy of the staff, the Society of Astrogeology caught this information
in no time, as always. They immediately sent their president's letter to the Institute of Science
and Education of the Commonwealth Galactica claiming that authoritative academic inspections
were to be completed before any commercial exploitation might damage the nature.

Fortunately, astrogeologists do not plan to practice all the possible inspections on all of the
trigons; there are far too many of them. Inspections are planned only on some characteristic
trigons and, for each of them, in one of twenty different scientific aspects.

To accelerate building this new tourist resort, ITO's construction machinery team has already succeeded in putting their brand-new
invention in practical use. It is a rover vehicle of the shape of an icosahedron, a regular polyhedron with twenty faces of equilateral triangles.
The machine is customized
so that each of the twenty faces exactly fits each of the trigons.
Controlling the high-tech gyromotor installed inside its body, the rover can roll onto one of the three trigons neighboring the one its bottom is
on.

Figure E.1: The Rover on Mare Triangularis

Each of the twenty faces has its own function. The set of equipments installed on the bottom
face touching the ground can be applied to the trigon it is on. Of course, the rover was meant to accelerate construction of the luxury hotels to host rich interstellar travelers, but, changing
the installed equipment sets, it can also be used to accelerate academic inspections.

You are the driver of this rover and are asked to move the vehicle onto the trigon specified by
the leader of the scientific commission with the smallest possible steps. What makes your task
more difficult is that the designated face installed with the appropriate set of equipments has to
be the bottom. The direction of the rover does not matter.

Figure E.2:The Coordinate System

Figure E.3: Face Numbering

The trigons of Mare Triangularis are given two-dimensional coordinates as shown in Figure E.2.
Like maps used for the Earth, the x axis is from the west to the east, and the y axis is from the
south to the north. Note that all the trigons with its coordinates (x , y) has neighboring trigons
with coordinates (x - 1 , y) and (x + 1 , y). In addition to these, when x + y is even, it has a
neighbor (x , y + 1); otherwise, that is, when x + y is odd, it has a neighbor (x , y - 1).

Figure E.3 shows a development of the skin of the rover. The top face of the development
makes the exterior. That is, if the numbers on faces of the development were actually marked
on the faces of the rover, they should been readable from its outside. These numbers are used
to identify the faces.

When you start the rover, it is on the trigon (0,0) and the face 0 is touching the ground. The
rover is placed so that rolling towards north onto the trigon (0,1) makes the face numbered 5
to be at the bottom.

As your first step, you can choose one of the three adjacent trigons, namely those with coordinates (-1,0), (1,0),
and (0,1), to visit. The bottom will be the face numbered 4, 1, and 5,
respectively. If you choose to go to (1,0) in the first rolling step, the second step can bring
the rover to either of (0,0), (2,0), or (1,-1). The bottom face will be either of 0, 6, or 2,
correspondingly. The rover may visit any of the trigons twice or more, including the start and the goal trigons, when appropriate.

The theoretical design section of ITO showed that the rover can reach any goal trigon on the
specified bottom face within a finite number of steps.

Input

The input consists of a number of datasets. The number of datasets does not exceed 50.

Each of the datasets has three integers x, y, and n in one line, separated by a space. Here, (x,y)
specifies the coordinates of the trigon to which you have to move the rover, and n specifies the
face that should be at the bottom.

The end of the input is indicated by a line containing three zeros.

Output

The output for each dataset should be a line containing a single integer that gives the minimum
number of steps required to set the rover on the specified trigon with the specified face touching
the ground. No other characters should appear in the output.

You can assume that the maximum number of required steps does not exceed 100. Mare Triangularis
is broad enough so that any of its edges cannot be reached within that number of
steps.

Sample Input

0 0 1
3 5 2
-4 1 3
13 -13 2
-32 15 9
-50 50 0
0 0 0

Output for the Sample Input

6
10
9
30
47
100

Solution in C++11 (judge verdict: Accepted)

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef pair<int,int> P;
struct state{
	P p;
	int b,m;
	bool operator < (const state& l) const{
		if(l.p!=p) return l.p<p;
		if(l.b!=b) return l.b<b;
		return l.m<m;
	}
};
map<state,int> d;
P trans[20][20][3];
void a(int a,int b,int c,int d,int e,int f){
	trans[a][b][0]=P(b,a);
	trans[a][b][1]=P(c,d);
	trans[a][b][2]=P(e,f);
}
int dx[3]={0,1,-1},dy[3]={1,0,0};
queue<state> que;
int main(){
	a(0,5,1,2,4,3);
	a(0,4,5,11,1,6);
	a(0,1,4,9,5,10);
	a(1,6,2,3,0,4);
	a(1,0,6,12,2,7);
	a(1,2,0,5,6,11);
	a(2,7,3,4,1,0);
	a(2,3,1,6,7,12);
	a(2,1,7,13,3,8);
	a(3,8,4,0,2,1);
	a(3,4,2,7,8,13);
	a(3,2,8,14,4,9);
	a(4,9,0,1,3,2);
	a(4,0,3,8,9,14);
	a(4,3,9,10,0,5);
	a(5,0,10,15,11,16);
	a(5,10,11,6,0,1);
	a(5,11,0,4,10,9);
	a(6,1,11,16,12,17);
	a(6,11,12,7,1,2);
	a(6,12,1,0,11,5);
	a(7,2,12,17,13,18);
	a(7,12,13,8,2,3);
	a(7,13,2,1,12,6);
	a(8,3,13,18,14,19);
	a(8,13,14,9,3,4);
	a(8,14,3,2,13,7);
	a(9,4,14,19,10,15);
	a(9,14,10,5,4,0);
	a(9,10,4,3,14,8);
	a(10,5,9,14,15,19);
	a(10,9,15,16,5,11);
	a(10,15,5,0,9,4);
	a(11,5,16,17,6,12);
	a(11,16,6,1,5,0);
	a(11,6,5,10,16,15);
	a(12,7,6,11,17,16);
	a(12,6,17,18,7,13);
	a(12,17,7,2,6,1);
	a(13,18,8,3,7,2);
	a(13,8,7,12,18,17);
	a(13,7,18,19,8,14);
	a(14,19,9,4,8,3);
	a(14,9,8,13,19,18);
	a(14,8,19,15,9,10);
	a(15,10,19,18,16,17);
	a(15,19,16,11,10,5);
	a(15,16,10,9,19,14);
	a(16,11,15,19,17,18);
	a(16,15,17,12,11,6);
	a(16,17,11,5,15,10);
	a(17,12,16,15,18,19);
	a(17,16,18,13,12,7);
	a(17,18,12,6,16,11);
	a(18,13,17,16,19,15);
	a(18,17,19,14,13,8);
	a(18,19,13,7,17,12);
	a(19,14,18,17,15,16);
	a(19,18,15,10,14,9);
	a(19,15,14,8,18,13);
	state start={P(0,0),0,5};
	que.push(start);
	d[start]=0;
	while(true){
		state s=que.front();
		if(d[s]>100) break;
		que.pop();
		P p=s.p;
		int k=((p.fs+p.sc+10000)%2==0?1:-1);
		//show(k);
		// show(p.fs);
		// show(p.sc);
		// show(s.b);
		// show(s.m);
		// cout<<endl;
		rep(i,3){
			P np=P(p.fs+dx[i]*k,p.sc+dy[i]*k);
			state nstate={np,trans[s.b][s.m][i].fs,trans[s.b][s.m][i].sc};
			if(d.find(nstate)!=d.end()) continue;
			d[nstate]=d[s]+1;
			// show(np.fs);
			// show(np.sc);
			// show(nstate.b);
			que.push(nstate);
		}
	}
	while(true){
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		if(x==0&&y==0&&z==0) break;
		int ret=1e5;
		rep(i,20){
			if(d.find({P(x,y),z,i})==d.end()) continue;
			ret=min(ret,d[state{P(x,y),z,i}]);
		}
		printf("%d\n",ret);
	}
}