#include<stdio.h> int ncase,n,p; int main() { while (scanf("%d",&n),n) { p=1; for (int i=0; i<n; i++) { p*=n; p%=9; } if(p) printf("%d\n",p); else printf("9\n"); } }