
#include<iostream>
#include<queue>
#include<cstdlib>
#include<set>
#include<functional>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(int)(n);++i)

using namespace std;

enum triangle{DOWN=0,UP=1};

const int dx = 101;
const int dy = 101;
const int MAX_X = 2*dx;
const int MAX_Y = 2*dy;
const int offsets[2][3] = {{2,0,1},{1,2,0}};
const int cx[2][3] = { {1,-1,0}, {1,0,-1} };
const int cy[2][3] = { {0,0,1}, {0,-1,0} };

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

struct State{
  unsigned char x,y;
  char face,preface,from;
  char link[3];
  char cost;
  State (unsigned char x, unsigned char y, char face, char preface, char from):x(x),y(y),face(face),preface(preface),from(from){}
};

static bool vis[MAX_X][MAX_Y][20][20][3];

State nextState(const State &st, int moveType){
  int x = st.x;
  int y = st.y;
  int which = (abs(x)+abs(y)) % 2;
  int diff;
  State next(x+cx[which][moveType],y+cy[which][moveType],st.link[moveType],st.face,moveType);

  next.cost = st.cost + 1;
  rep(i,3) if( roll[next.face][i] == st.face ) diff = i;
  rep(i,3) next.link[(i+offsets[which][moveType])%3] = roll[next.face][ (diff+i) % 3 ];

  return next;
};

int solve(int gx, int gy, int gface)
{
  queue<State> Q;
  State init(dx,dy,0,0,0);
  
  rep(i,MAX_X)rep(j,MAX_Y)rep(k,20)rep(l,20)rep(m,3)vis[i][j][k][l][m]=false;
  rep(i,3)init.link[i]=roll[0][i];
  init.cost = 0;
  Q.push( init );

  while(!Q.empty()){
    State cur = Q.front(); Q.pop();
    if( cur.cost + abs(cur.x-gx) + abs(cur.y-gy) > 100 ) continue;
    if( cur.x == gx && cur.y == gy && cur.face == gface ){
      return cur.cost;
    }
    rep(i,3){
      State next = nextState( cur, i );
      if( vis[next.x][next.y][next.face][next.preface][next.from] ) continue;
      else vis[next.x][next.y][next.face][next.preface][next.from] = true;
      Q.push( next );
    }
  }
  return -1;
}

int main()
{
  int x,y,face;
  while(cin>>x>>y>>face){
    if( x == 0 && y == 0 && face == 0 ) break ;
    cout << solve(x+dx,y+dy,face) << endl;
  }
  return 0;
}