#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define LL long long
using namespace std;
int c,s;
inline LL gcd(LL a,LL b){
return b==0?a:gcd(b,a%b);
}
inline LL power(int a,int b){
LL ans=1;
while(b){
if(b&1)
ans=ans*a;
a=a*a,b>>=1;
}
return ans;
}
inline LL polya(void){
LL sum=0;
for(int i=1;i<=s;i++)
sum+=power(c,gcd(s,i));
if(s&1)
sum+=s*power(c,(s>>1)+1);
else
for(int i=1;i<=s;i++){
if(i&1)
sum+=power(c,(s>>1)+1);
else
sum+=power(c,s>>1);
}
return sum/2/s;
}
signed main(void){
c=3;
while(scanf("%d",&s)&&s!=-1)
printf("%lld\n",s==0?0:polya());
return 0;
}
by >_< neighthorn