#include
int main(void){
int a,b,i,n;
int c[100]={0,1,1};
while(scanf("%d%d%d",&a,&b,&n)==3&&(a!=0||b!=0||n!=0)){
for(i=3;i<100;i++){
c[i] = (a * c[i-1]+ b * c[i-2]) % 7;
if(c[i]==1&&c[i-1]==1)
break;
}
int t = n % (i- 2) ;
if(t == 0)
{
printf("%d\n",c[i - 2]) ;
}else
{
printf("%d\n",c[t]) ;
}
}
return 0;
}