// 3中颜色的n个珠子 问有多少种不同数量 旋转和翻转后相同算一种
// 先考虑旋转 转i格 循环节 gcd(i,n)
// 翻转的话 n为奇数 每种翻转循环节 n/2+1
// n为偶数 n/2的循环节为 n/2 n/2的循环节为 (n-2)/2 +2
// 最后用 polya定理
#include <iostream>
#include <stdio.h>
using namespace std;
#define LL long long
LL Pow(LL a,LL b)
{
LL t=1;
for(;b;b>>=1)
{
if(b&1) t=t*a;
a=a*a;
}
return t;
}
int gcd(int a,int b)
{
int r;
while(r=a%b){a=b;b=r;}
return b;
}
int main()
{
int i,n;
LL ans;
//printf("%lld ",Pow(2,10));
while(scanf("%d",&n),~n)
{
if(!n){printf("0
");continue;}
ans=0;
for(i=1;i<=n;i++)
ans+=Pow(3,gcd(i,n));
// printf("%lld ",ans);
if(n&1)
ans+=n*Pow(3,n/2+1);
else
{
ans+=n/2*Pow(3,n/2);
ans+=n/2*Pow(3,n/2+1);
}
printf("%lld
",ans/n/2);
}
return 0;
}