# include <iostream>
# include <cstdio>
# include <algorithm>
using namespace std;
int main(){
int n,a[10009];
int i,t;
while(scanf("%d",&n),n!=0){
int t = 1;
for(i=0;i<n;i++){
t=(n*t-1)%9+1;//-1的目的当时9的时候,输出9
}
printf("%d\n",t);
}
return 0;
}
# include <iostream>
# include <cstdio>
# include <algorithm>
using namespace std;
int main(){
int n,a[10009];
int i,t;
while(scanf("%d",&n),n!=0){
int t = 1;
for(i=0;i<n;i++){
t=(n*t-1)%9+1;//-1的目的当时9的时候,输出9
}
printf("%d\n",t);
}
return 0;
}