#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;

const int M = 0x7FFF;
const int tate = 220;
const int yoko = 220;

struct ITEM{
	int x;
	int y;
	int n;
	int d;
	int th;
};

int main()
{
	static int all[yoko][tate][20][3];
	static int ans[yoko][tate][20];
	pair<int, int> henka[20][3];
	henka[0][0] = pair<int, int>(5, 0);
	henka[0][1] = pair<int, int>(1, 2);
	henka[0][2] = pair<int, int>(4, 1);
	henka[1][0] = pair<int, int>(6, 0);
	henka[1][1] = pair<int, int>(2, 2);
	henka[1][2] = pair<int, int>(0, 1);
	henka[2][0] = pair<int, int>(7, 0);
	henka[2][1] = pair<int, int>(3, 2);
	henka[2][2] = pair<int, int>(1, 1);
	henka[3][0] = pair<int, int>(8, 0);
	henka[3][1] = pair<int, int>(4, 2);
	henka[3][2] = pair<int, int>(2, 1);
	henka[4][0] = pair<int, int>(9, 0);
	henka[4][1] = pair<int, int>(0, 2);
	henka[4][2] = pair<int, int>(3, 1);
	henka[5][0] = pair<int, int>(0, 0);
	henka[5][1] = pair<int, int>(10, 0);
	henka[5][2] = pair<int, int>(11, 0);
	henka[6][0] = pair<int, int>(1, 0);
	henka[6][1] = pair<int, int>(11, 0);
	henka[6][2] = pair<int, int>(12, 0);
	henka[7][0] = pair<int, int>(2, 0);
	henka[7][1] = pair<int, int>(12, 0);
	henka[7][2] = pair<int, int>(13, 0);
	henka[8][0] = pair<int, int>(3, 0);
	henka[8][1] = pair<int, int>(13, 0);
	henka[8][2] = pair<int, int>(14, 0);
	henka[9][0] = pair<int, int>(4, 0);
	henka[9][1] = pair<int, int>(14, 0);
	henka[9][2] = pair<int, int>(10, 0);
	henka[10][0] = pair<int, int>(15, 0);
	henka[10][1] = pair<int, int>(5, 0);
	henka[10][2] = pair<int, int>(9, 0);
	henka[11][0] = pair<int, int>(16, 0);
	henka[11][1] = pair<int, int>(6, 0);
	henka[11][2] = pair<int, int>(5, 0);
	henka[12][0] = pair<int, int>(17, 0);
	henka[12][1] = pair<int, int>(7, 0);
	henka[12][2] = pair<int, int>(6, 0);
	henka[13][0] = pair<int, int>(18, 0);
	henka[13][1] = pair<int, int>(8, 0);
	henka[13][2] = pair<int, int>(7, 0);
	henka[14][0] = pair<int, int>(19, 0);
	henka[14][1] = pair<int, int>(9, 0);
	henka[14][2] = pair<int, int>(8, 0);
	henka[15][0] = pair<int, int>(10, 0);
	henka[15][1] = pair<int, int>(19, 2);
	henka[15][2] = pair<int, int>(16, 1);
	henka[16][0] = pair<int, int>(11, 0);
	henka[16][1] = pair<int, int>(15, 2);
	henka[16][2] = pair<int, int>(17, 1);
	henka[17][0] = pair<int, int>(12, 0);
	henka[17][1] = pair<int, int>(16, 2);
	henka[17][2] = pair<int, int>(18, 1);
	henka[18][0] = pair<int, int>(13, 0);
	henka[18][1] = pair<int, int>(17, 2);
	henka[18][2] = pair<int, int>(19, 1);
	henka[19][0] = pair<int, int>(14, 0);
	henka[19][1] = pair<int, int>(18, 2);
	henka[19][2] = pair<int, int>(15, 1);

	for(int t=0; t<tate; t++){
		for(int y=0; y<yoko; y++){
			for(int i=0; i<20; i++){
				for(int j=0; j<3; j++){
					all[y][t][i][j] = M;
				}
			}
		}
	}

	queue<struct ITEM> q;
	struct ITEM buf;
	buf.x = yoko/2; buf.y=tate/2; buf.d=0; buf.n=0; buf.th=0;
	q.push(buf);
	int count = 0;
	while(!q.empty()){
		struct ITEM now = q.front();
		q.pop();
		if(now.x < 0) continue;
		if(now.y < 0) continue;
		if(now.x >= yoko) continue;
		if(now.y >= tate) continue;
		int taosu;
		if(now.d < all[now.x][now.y][now.n][now.th]){
			// ツ更ツ新
			all[now.x][now.y][now.n][now.th] = now.d;

			// ツ湘」ツ可コツ暗堋督ョ
			buf = now;
			if((now.x+now.y+yoko/2+tate/2)%2==0) buf.y++;
			else                                 buf.y--;
			buf.d++;
			taosu = (3-now.th)%3;
			buf.n  =  henka[now.n][taosu].first;
			buf.th = (henka[now.n][taosu].second+now.th)%3;
			q.push(buf);

			// ツ債カツ暗堋督ョ
			buf = now;
			buf.x--;
			buf.d++;
			if((now.x+now.y+yoko/2+tate/2)%2==0) taosu = (3+1-now.th)%3;
			else                                 taosu = (3+2-now.th)%3;
			buf.n  =  henka[now.n][taosu].first;
			buf.th = (henka[now.n][taosu].second+now.th)%3;
			q.push(buf);

			// ツ右ツ暗堋督ョ
			buf = now;
			buf.x++;
			buf.d++;
			if((now.x+now.y+yoko/2+tate/2)%2==0) taosu = (3+2-now.th)%3;
			else                                 taosu = (3+1-now.th)%3;
			buf.n  =  henka[now.n][taosu].first;
			buf.th = (henka[now.n][taosu].second+now.th)%3;
			q.push(buf);

			count++;
		}
	}
	for(int t=0; t<tate; t++){
		for(int y=0; y<yoko; y++){
			for(int i=0; i<20; i++){
				int aaa = M;
				for(int j=0; j<3; j++){
					aaa = min(aaa, all[y][t][i][j]);
				}
				ans[y][t][i] = aaa;
			}
		}
	}
	/*
	for(int i=0; i<20; i++){
		cout<<"num="<<i<<endl;
		for(int t=tate/2+5; t>=tate/2-5; t--){
			for(int y=yoko/2-10; y<=yoko/2+10; y++){
				if(ans[y][t][i] == M){
					printf("  _");
				} else if(y==yoko/2&&t==tate/2) {
					printf("  #");
				} else {
			 		printf("%3d", ans[y][t][i]);
				}
			}
			cout<<endl;
		}
		cout<<endl;
	}
	cout<<endl;
	*/

	int X, Y, N;
	while(cin>>X>>Y>>N){
		if(!X&&!Y&&!N) break;
		cout<<ans[yoko/2-X][tate/2+Y][N]<<endl;
	}
	
	return 0;
}

Measured memory_bytes: 16000