#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;
}

*/

Measured memory_bytes: 11068