#include<iostream>
#include<cstring> #include<algorithm> #include<cstdio> #include<cmath> #include<string> #include<iomanip> using namespace std; const int MAX = 10010; int findroot(int n) { int sum = MAX;; while(sum>=10) { sum = 0; while(n) { sum += n%10; n /= 10; } n = sum; } return sum; } int main() { int n; while(cin>>n && n) { int ans = findroot(n); for(int i=1;i<n;i++) ans = findroot(ans*n); cout<<ans<<endl; } return 0; }