Problem: 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.

<image> |  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.

<image> |  <image> |  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.

Example

Input

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


Output

6
10
9
30
47
100

Solution (in CPP):

Solution:
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

const int f[20][3]={{1,4,5},{2,0,6},{3,1,7},{4,2,8},{0,3,9},{0,10,11},{1,11,12},{2,12,13},{3,13,14},{4,14,10},{5,9,15},{6,5,16},{7,6,17},{8,7,18},{9,8,19},{10,19,16},{11,15,17},{12,16,18},{13,17,19},{14,18,15}};

int dp[202][202][20][3];

void A_up(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void A_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void A_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_down(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void B_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void init(){
    static int x,y,n,p,n0,p0;
    static queue<int> Q;
    memset(dp,-1,sizeof(dp));
    dp[100][100][0][2]=0;
    while(!Q.empty())Q.pop();
    Q.push(100);Q.push(100);Q.push(0);Q.push(2);
    while(!Q.empty()){
        x=Q.front();Q.pop();
        y=Q.front();Q.pop();
        n=Q.front();Q.pop();
        p=Q.front();Q.pop();
        if(dp[x][y][n][p]==100)return;
        if((x+y)%2){
            n0=n,p0=p;
            B_down(n0,p0);
            if(dp[x][y-1][n0][p0]==-1){
                dp[x][y-1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y-1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
        else{
            n0=n,p0=p;
            A_up(n0,p0);
            if(dp[x][y+1][n0][p0]==-1){
                dp[x][y+1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y+1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
    }
}

bool solve(){
    static int x,y,n;
    if(scanf("%d%d%d",&x,&y,&n)!=3||((x|y|n)==0))return false;
    x+=100;
    y+=100;
    static int ans,i;
    ans=100;
    for(i=0;i<3;i++)
        if(dp[x][y][n][i]!=-1&&(ans>dp[x][y][n][i]))
            ans=dp[x][y][n][i];
    printf("%d\n",ans);
    return true;
}

int main(){
    init();
    while(solve());
    return 0;
}
/*
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

const int f[20][3]={{1,4,5},{2,0,6},{3,1,7},{4,2,8},{0,3,9},{0,10,11},{1,11,12},{2,12,13},{3,13,14},{4,14,10},{5,9,15},{6,5,16},{7,6,17},{8,7,18},{9,8,19},{10,19,16},{11,15,17},{12,16,18},{13,17,19},{14,18,15}};

int dp[202][202][20][3];

void A_up(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void A_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void A_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_down(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void B_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void init(){
    static int x,y,n,p,n0,p0;
    static queue<int> Q;
    memset(dp,-1,sizeof(dp));
    dp[100][100][0][2]=0;
    while(!Q.empty())Q.pop();
    Q.push(100);Q.push(100);Q.push(0);Q.push(2);
    while(!Q.empty()){
        x=Q.front();Q.pop();
        y=Q.front();Q.pop();
        n=Q.front();Q.pop();
        p=Q.front();Q.pop();
        if(dp[x][y][n][p]==100)return;
        if((x+y)%2){
            n0=n,p0=p;
            B_down(n0,p0);
            if(dp[x][y-1][n0][p0]==-1){
                dp[x][y-1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y-1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
        else{
            n0=n,p0=p;
            A_up(n0,p0);
            if(dp[x][y+1][n0][p0]==-1){
                dp[x][y+1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y+1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
    }
}

bool solve(){
    static int x,y,n;
    if(scanf("%d%d%d",&x,&y,&n)!=3||((x|y|n)==0))return false;
    x+=100;
    y+=100;
    static int ans,i;
    ans=100;
    for(i=0;i<3;i++)
        if(dp[x][y][n][i]!=-1&&(ans>dp[x][y][n][i]))
            ans=dp[x][y][n][i];
    printf("%d\n",ans);
    return true;
}

int main(){
    init();
    while(solve());
    return 0;
}
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

const int f[20][3]={{1,4,5},{2,0,6},{3,1,7},{4,2,8},{0,3,9},{0,10,11},{1,11,12},{2,12,13},{3,13,14},{4,14,10},{5,9,15},{6,5,16},{7,6,17},{8,7,18},{9,8,19},{10,19,16},{11,15,17},{12,16,18},{13,17,19},{14,18,15}};

int dp[202][202][20][3];

void A_up(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void A_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void A_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_down(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void B_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void init(){
    static int x,y,n,p,n0,p0;
    static queue<int> Q;
    memset(dp,-1,sizeof(dp));
    dp[100][100][0][2]=0;
    while(!Q.empty())Q.pop();
    Q.push(100);Q.push(100);Q.push(0);Q.push(2);
    while(!Q.empty()){
        x=Q.front();Q.pop();
        y=Q.front();Q.pop();
        n=Q.front();Q.pop();
        p=Q.front();Q.pop();
        if(dp[x][y][n][p]==100)return;
        if((x+y)%2){
            n0=n,p0=p;
            B_down(n0,p0);
            if(dp[x][y-1][n0][p0]==-1){
                dp[x][y-1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y-1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
        else{
            n0=n,p0=p;
            A_up(n0,p0);
            if(dp[x][y+1][n0][p0]==-1){
                dp[x][y+1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y+1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
    }
}

bool solve(){
    static int x,y,n;
    if(scanf("%d%d%d",&x,&y,&n)!=3||((x|y|n)==0))return false;
    x+=100;
    y+=100;
    static int ans,i;
    ans=100;
    for(i=0;i<3;i++)
        if(dp[x][y][n][i]!=-1&&(ans>dp[x][y][n][i]))
            ans=dp[x][y][n][i];
    printf("%d\n",ans);
    return true;
}

int main(){
    init();
    while(solve());
    return 0;
}

*/